Skip to content

Instantly share code, notes, and snippets.

@shu0115
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shu0115/b8812d14ee1053514f46 to your computer and use it in GitHub Desktop.
Save shu0115/b8812d14ee1053514f46 to your computer and use it in GitHub Desktop.
Ruby便利メソッドまとめ:Array/Hash/Enumerator ref: http://qiita.com/shu_0115/items/5f08a6d58d1acc46fbad
[1, nil, 2, nil, 3, nil].compact
# => [1, 2, 3]
[1, 2, 3].concat([4, 5, 6])
# => [1, 2, 3, 4, 5, 6]
['a', 'b', 'c'].sample
# => "c"
['a', 'b', 'c'].sample(2)
# => ["b", "a"]
[1, 2, 3, 4, 5].select{ |x| x >= 3 }
# => [3, 4, 5]
[1, 2, 3, 4, 5].shuffle
# => [3, 5, 1, 2, 4]
[1, 2, 3, 4, 5].slice(0, 3)
# => [1, 2, 3]
[1, 2, 3, 4, 5].slice(2..4)
# => [3, 4, 5]
['e', 'd', 'a', 'b', 'c'].sort
# => ["a", "b", "c", "d", "e"]
[2, 1, 5, 1, 3, 2, 4, 3].uniq
# => [2, 1, 5, 3, 4]
{ a: 1, b: 2, c: 3 }.each{ |key, value| puts "#{key} : #{value}" }
----------
a : 1
b : 2
c : 3
----------
{ a: 1, b: 2, c: 3 }.each_key{ |key| p key }
----------
:a
:b
:c
----------
{ a: 1, b: 2, c: 3 }.each_value{ |value| p value }
----------
1
2
3
----------
{ a: 1, b: 2, c: 3 }.has_key?(:b)
# => true
{ a: 1, b: 2, c: 3 }.has_key?(:z)
# => false
a = [1, 2, 3]; p a.concat([4, 5, 6]); p a;
----------
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
----------
{ a: 1, b: 2, c: 3 }.has_value?(3)
# => true
{ a: 1, b: 2, c: 3 }.has_value?(30)
# => false
{ a: 1, b: 2, c: 3 }.keys
# => [:a, :b, :c]
{ a: 1, b: 2, c: 3 }.values
# => [1, 2, 3]
['a', 'b', 'c', 'd', 'e'].each.with_index(1){ |value, index| puts "#{index} : #{value}" }
----------
1 : a
2 : b
3 : c
4 : d
5 : e
----------
[1, 2, 3, 4, 5].all?
# => true
[1, 2, 3, 4, 5, nil].all?
# => false
[1, 2, 3, 4, 5].all?{ |x| x < 10 }
# => true
[1, 2, 3, 4, 5].all?{ |x| x < 5 }
# => false
[nil, false].none?
# => true
[1, 2, 3, 4, 5].none?{ |x| x < 0 }
# => true
[1, 2, 3, 4, 5].any?{ |x| x >= 5 }
# => true
[1, 2, 3, 4, 5].any?{ |x| x > 5 }
# => false
[1, 2, 3, 4, 5].one?{ |x| x == 4 }
# => true
[1, 2, 3, 4, 5].one?{ |x| x <= 4 }
# => false
[1, 2, 3, 4, 5].count
# => 5
[1, 2, 3, 4, 5].each_slice(2){ |x| p x }
----------
[1, 2]
[3, 4]
[5]
----------
a = [1, 2, 3]; p (a + [4, 5, 6]); p a;
----------
[1, 2, 3, 4, 5, 6]
[1, 2, 3]
----------
[1, 2, 3, 4, 5].each_cons(2){ |x| p x }
----------
[1, 2]
[2, 3]
[3, 4]
[4, 5]
----------
(1..10).partition{ |x| x % 3 == 0 }
# => [[3, 6, 9], [1, 2, 4, 5, 7, 8, 10]]
[1, 2, 3, 4, 5].take(3)
# => [1, 2, 3]
(1..10).group_by{ |x| x % 3 }
# => {1=>[1, 4, 7, 10], 2=>[2, 5, 8], 0=>[3, 6, 9]}
[1, 2, 3, 4, 5].min
# => 1
[1, 2, 3, 4, 5].max
# => 5
[1, 2, 3, 4, 5].minmax
# => [1, 5]
a = [1, 2, 3, 2, 1]
a.delete(1)
# => 1
p a
# => [2, 3, 2]
a = [0, 1, 2, 3, 4, 5]
a.delete_if{ |x| x % 2 == 0 }
# => [1, 3, 5]
p a
# => [1, 3, 5]
['a', 'b', 'c'].include?('a')
# => true
['a', 'b', 'c'].include?('z')
# => false
['a', 'b', 'c'].index('c')
# => 2
['a', 'b', 'c'].index('z')
# => nil
['a', 'b', 'c'].join('-')
# => "a-b-c"
['a', 'b', 'c'].reverse
# => ["c", "b", "a"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment