Created
January 6, 2014 06:30
-
-
Save kidach1/8279049 to your computer and use it in GitHub Desktop.
[Ruby] 便利な組み込みクラスのメソッド達(Enumerable編) ref: http://qiita.com/kidachi_/items/a00558cfb0a6a3e23f4b
This file contains 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
> %w(Ruby Python Java).each_with_index do |value, key| | |
> puts "#{key}: #{value}" | |
> end | |
0: Ruby | |
1: Python | |
2: Java |
This file contains 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
> %w(Ruby Python Java).reverse { |val| puts val } | |
=> ["Java", "Python", "Ruby"] |
This file contains 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
# 要素が全て整数ならtrue | |
> [1, 10, 100].all? {|v| v.is_a?(Integer)} | |
=> true | |
# 要素が全て文字列ならtrue | |
> [1, 10, 100].all? {|v| v.is_a?(String)} | |
=> false | |
> ["Ruby", "Python", "Java"].all? {|v| v.is_a?(String)} | |
=> true |
This file contains 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
# 正規表現にマッチする要素を取得 | |
> %w(Ruby Python Java).grep(/y/) | |
=> ["Ruby", "Python"] | |
# 指定の型にマッチする要素を取得 | |
> ["Ruby", "Python", "Java", 1, 10, 100].grep(String) | |
=> ["Ruby", "Python", "Java"] | |
> ["Ruby", "Python", "Java", 1, 10, 100].grep(Integer) | |
=> [1, 10, 100] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).detect { |s| s.include?("y") } | |
=> "Ruby" |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).select { |s| s.include?("y") } | |
=> ["Ruby", "Python"] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).reject { |s| s.include?("y") } | |
=> ["Java", "C", "Smalltalk", "Brainfuck"] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).take(3) | |
=> ["Ruby", "Python", "Java"] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).drop(3) | |
=> ["C", "Smalltalk", "Brainfuck"] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).take_while { |s| s.include?("y") } | |
=> ["Ruby", "Python"] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).drop_while { |s| s.include?("y") } | |
=> ["Java", "C", "Smalltalk", "Brainfuck"] |
This file contains 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].inject(0) { |result, num| result + num } | |
=> 10 # 0 +1 + 2 + 3 + 4 |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).each_slice 2 do |a, b| | |
> p [a, b] | |
> end | |
["Ruby", "Python"] | |
["Java", "C"] | |
["Smalltalk", "Brainfuck"] | |
> %w(Ruby Python Java C Smalltalk Brainfuck).each_slice 4 do |a, b, c, d| | |
> p [a, b, c, d] | |
> end | |
["Ruby", "Python", "Java", "C"] | |
["Smalltalk", "Brainfuck", nil, nil] # 帳尻が合わなかった部分はnil |
This file contains 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].inject { |result, num| result + num } | |
=> 10 # 1 + 2 + 3 + 4 |
This file contains 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].inject(:+) | |
=> 10 | |
> [1, 2, 3, 4].inject(:*) | |
=> 24 |
This file contains 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
> %w(Ruby Python Java).each_with_object({}) do |name, result| | |
> p result | |
> result[name] = name.length | |
> end | |
{} | |
{"Ruby"=>4} | |
{"Ruby"=>4, "Python"=>6} | |
=> {"Ruby"=>4, "Python"=>6, "Java"=>4} |
This file contains 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
> ["Ruby", 1.0, "Python", 1000, 2.5, "Java"].group_by { |v| v.class } | |
=> {String=>["Ruby", "Python", "Java"], Float=>[1.0, 2.5], Fixnum=>[1000]} |
This file contains 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
> ["Ruby", 1.0, "Python", 1000, 2.5, "Java"].partition { |v| v.is_a?(String) } | |
=> [["Ruby", "Python", "Java"], [1.0, 1000, 2.5]] |
This file contains 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
> range = (1..5) | |
> range.max | |
=> 5 | |
> range.min | |
=> 1 | |
> range.minmax | |
=> [1, 5] |
This file contains 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
> lang = %w(Ruby Python Java C) | |
> lang.min_by { |s| s.length } | |
=> "C" | |
> lang.max_by { |s| s.length } | |
=> "Python" | |
> lang.minmax_by { |s| s.length } | |
=> ["C", "Python"] |
This file contains 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
> lang = %w(Ruby Python Java C) | |
# アルファベット順 | |
> lang.sort | |
=> ["C", "Java", "Python", "Ruby"] | |
# 要素の長さ(1) | |
> lang.sort { |a, b| a.length <=> b.length } | |
=> ["C", "Java", "Ruby", "Python"] | |
# 要素の長さ(2) | |
> lang.sort_by { |str| str.length } | |
=> ["C", "Java", "Ruby", "Python"] |
This file contains 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
> %w(Ruby Python Java C Smalltalk Brainfuck).each_cons 2 do |a, b| | |
> p [a, b] | |
> end | |
["Ruby", "Python"] | |
["Python", "Java"] | |
["Java", "C"] | |
["C", "Smalltalk"] | |
["Smalltalk", "Brainfuck"] |
This file contains 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
> %w(Ruby Python Java).map { |s| s.upcase } | |
=> ["RUBY", "PYTHON", "JAVA"] |
This file contains 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
> %w(Ruby Python Java).each {|s| p s.upcase} | |
"RUBY" | |
"PYTHON" | |
"JAVA" | |
=> ["Ruby", "Python", "Java"] |
This file contains 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
> %w(Ruby Python Java).each {|s| p s.upcase} | |
"RUBY" | |
"PYTHON" | |
"JAVA" | |
=> ["Ruby", "Python", "Java"] |
This file contains 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
> [true, true, true].all? | |
=> true | |
> [true, true, false].all? | |
=> false |
This file contains 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
> [false, false, false].none? | |
=> true | |
> [true, true, false].none? | |
=> false |
This file contains 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
> [true, true, false].any? | |
=> true | |
> [false, false, false].any? | |
=> false |
This file contains 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
> [false, false, true].one? | |
=> true | |
> [false, false, false].one? | |
=> false | |
> [false, true, true].one? | |
=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment