テストを通る最初の要素(の位置)を取得する
a = [1,2,2,3,4,6,1,3,7,8,9] p a.index(3); #=> 3 p a.index(5); #=> nil # bで始まる単語を探す b = %w[red green blue white black] p b.find() {|x| x=~/\Ab/} #=> "blue"
1.9以降では
p b.index {|x| x=~/\Ab/}
こういう書き方で,位置を取れるらしい.うちのrubyは1.8.xだ.
a = [1,2,2,3,4,6,1,3,7,8,9] p a.index(3); #=> 3 p a.index(5); #=> nil # bで始まる単語を探す b = %w[red green blue white black] p b.find() {|x| x=~/\Ab/} #=> "blue"
1.9以降では
p b.index {|x| x=~/\Ab/}
こういう書き方で,位置を取れるらしい.うちのrubyは1.8.xだ.