4

配列またはハッシュを反復処理しており、インデックスに基づいて値を取得したいと考えています。

@lv = {'apple' => ['round', 'red'], 'more names' => ['tags', 'more tags']}

@lv.each do |key, value|
   # if the value is from the index key and equal to key itself...
end

から値を取得する方法がわかりません@lv。私が取得したいとしましょうapple

@lv[0]アップルと等しいはずのようなものはありますか?なぜなら

[0] => apple
[1] => more names

右?

だから私はできる

@lv.each do |key, value|
   if @lv[0] == key
     # apple equals apple, then proceed
   end
end

これを行う際の正しい構文は何ですか?

ありがとう!

4

1 に答える 1

6
@lv.keys[0]   # => "apple"
@lv.values[0] # => ["round", "red"]
于 2013-04-15T06:02:36.360 に答える