1

I think I have a correct array going for rails?

@lv = {'apple' => ['tags', 'red'], 'name' => ['more tags', 'taggers']} 

I was wondering how I can display certain parts through a loop. For instance, how would I only display apple and name?

<% @lv.each do |me| %> <%= me %> <% end %> 

This just displays the whole @lv message, and doesn't only display apple and name. And then I'd like to be able to get only the tagged values of specific ones, so say if I need to get tagged value of apple, it should only display tags and red How do I do this with rails?

Thanks!

2
  • In regards to the second part of your question, what does "the tagged values of specific ones" mean? Is the tags item in the array special in some way? Commented Apr 14, 2013 at 6:08
  • @Teeg I want to be able to output each tag, if I its something like key[0] or something? Not sure how it is, but if its something like PHP, I'd call the first array and then it'll output the values from the first array key which is apple (using a loop with key[0] or something), does that make more sense? Commented Apr 14, 2013 at 6:12

1 Answer 1

1

Your @lv variable is a hash, so using .each will only give you a combined key-value pair as the block parameter (that's what me ends up being). Instead, use each_pair; that way you can get separated variables for the keys and the values. Like so:

<% @lv.each_pair do |key, value| %> <%= key %> <% end %> 

Edit

This is in response to your comment in the question as well. The key will end up being just the apple, or name, part of your hash. The value parameter is whatever is pointed to by the key, which in this case is the actual array of items (which I think is what you're calling tags). For example, your hash contains two key-value pairs, and as we iterate over them, in the first loop key = apple, and value=['tags', 'red']. To output that array of values, you could do it a couple of different ways:

Loop over the tag array

<% @lv.each_pair do |key, value| %> <%= key %> <%= value.each do |tag| %> <%= tag %> <%= end %> <% end %> 

As a comma separated string:

....looping code <%= value.join(", ") %> 

Or just spit it out as-is in array notation:

....looping code <%= value %> 

Or if you just wanted a specific element in the value array, then yes you can just do value[0], or value[1]...etc.

Let me know whether that is not what you are asking.

Sign up to request clarification or add additional context in comments.

6 Comments

I want to be able to output the key in some kind of link. So if user clicks on apple, the tags should show up. In other words, how do I just output the tags that are specific to the names only (apple or name)?
This is exactly what I'm asking! Thank you very much!
There's one question: what if I just want to iterate through apple instead of name? Can I just do @lv[0].each_pair? or would the [0] be here: @lv.each_pair do |key[0], value|?
@andrewliu No you would reference the specific key by name, so you'd do @lv[:apple].each do |apple_values| .....
@andrewliu You cannot, no. They key is a single word. If you have a deeper structure like {:apple=>{:more_apples=>"some value"}} then you'd reference it like @lv[:apple][:more_apples].
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.