Last active
August 29, 2015 14:14
-
-
Save shu0115/b8812d14ee1053514f46 to your computer and use it in GitHub Desktop.
Ruby便利メソッドまとめ:Array/Hash/Enumerator ref: http://qiita.com/shu_0115/items/5f08a6d58d1acc46fbad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, nil, 2, nil, 3, nil].compact | |
# => [1, 2, 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3].concat([4, 5, 6]) | |
# => [1, 2, 3, 4, 5, 6] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['a', 'b', 'c'].sample | |
# => "c" | |
['a', 'b', 'c'].sample(2) | |
# => ["b", "a"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].select{ |x| x >= 3 } | |
# => [3, 4, 5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].shuffle | |
# => [3, 5, 1, 2, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].slice(0, 3) | |
# => [1, 2, 3] | |
[1, 2, 3, 4, 5].slice(2..4) | |
# => [3, 4, 5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['e', 'd', 'a', 'b', 'c'].sort | |
# => ["a", "b", "c", "d", "e"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2, 1, 5, 1, 3, 2, 4, 3].uniq | |
# => [2, 1, 5, 3, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.each{ |key, value| puts "#{key} : #{value}" } | |
---------- | |
a : 1 | |
b : 2 | |
c : 3 | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.each_key{ |key| p key } | |
---------- | |
:a | |
:b | |
:c | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.each_value{ |value| p value } | |
---------- | |
1 | |
2 | |
3 | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.has_key?(:b) | |
# => true | |
{ a: 1, b: 2, c: 3 }.has_key?(:z) | |
# => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 2, 3]; p a.concat([4, 5, 6]); p a; | |
---------- | |
[1, 2, 3, 4, 5, 6] | |
[1, 2, 3, 4, 5, 6] | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.has_value?(3) | |
# => true | |
{ a: 1, b: 2, c: 3 }.has_value?(30) | |
# => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.keys | |
# => [:a, :b, :c] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ a: 1, b: 2, c: 3 }.values | |
# => [1, 2, 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['a', 'b', 'c', 'd', 'e'].each.with_index(1){ |value, index| puts "#{index} : #{value}" } | |
---------- | |
1 : a | |
2 : b | |
3 : c | |
4 : d | |
5 : e | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[nil, false].none? | |
# => true | |
[1, 2, 3, 4, 5].none?{ |x| x < 0 } | |
# => true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].any?{ |x| x >= 5 } | |
# => true | |
[1, 2, 3, 4, 5].any?{ |x| x > 5 } | |
# => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].one?{ |x| x == 4 } | |
# => true | |
[1, 2, 3, 4, 5].one?{ |x| x <= 4 } | |
# => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].count | |
# => 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].each_slice(2){ |x| p x } | |
---------- | |
[1, 2] | |
[3, 4] | |
[5] | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 2, 3]; p (a + [4, 5, 6]); p a; | |
---------- | |
[1, 2, 3, 4, 5, 6] | |
[1, 2, 3] | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].each_cons(2){ |x| p x } | |
---------- | |
[1, 2] | |
[2, 3] | |
[3, 4] | |
[4, 5] | |
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1..10).partition{ |x| x % 3 == 0 } | |
# => [[3, 6, 9], [1, 2, 4, 5, 7, 8, 10]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].take(3) | |
# => [1, 2, 3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1..10).group_by{ |x| x % 3 } | |
# => {1=>[1, 4, 7, 10], 2=>[2, 5, 8], 0=>[3, 6, 9]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].min | |
# => 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].max | |
# => 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1, 2, 3, 4, 5].minmax | |
# => [1, 5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 2, 3, 2, 1] | |
a.delete(1) | |
# => 1 | |
p a | |
# => [2, 3, 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [0, 1, 2, 3, 4, 5] | |
a.delete_if{ |x| x % 2 == 0 } | |
# => [1, 3, 5] | |
p a | |
# => [1, 3, 5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['a', 'b', 'c'].include?('a') | |
# => true | |
['a', 'b', 'c'].include?('z') | |
# => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['a', 'b', 'c'].index('c') | |
# => 2 | |
['a', 'b', 'c'].index('z') | |
# => nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['a', 'b', 'c'].join('-') | |
# => "a-b-c" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['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