4

I'm iterating through an array or hash, and I'd like to get values based on index.

@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

I'm not sure how to get the value from @lv. Lets say I want to get apple:

Is there something like @lv[0] should equal apple? Because

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

right?

So I can

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

What is the correct syntax on doing this?

Thanks!

1 Answer 1

6
@lv.keys[0]   # => "apple"
@lv.values[0] # => ["round", "red"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.