On 18 May 2012 04:57, Christopher D. <li...@ruby-forum.com> wrote:
> ary = [0,1,1,1,1,3]
> ary.map! { |num| %w(zero one two three)[num] }
>
> I just don't get it, how does it know which value to write out?

you're taking the numbers 0,1,1,1,1,3 and for each of them you're
populating an array with the values from another array at the index
that corresponds to the number you're operating on at the time.
It doesn't "know" which number to write out, you're telling it to go
get whatever element is at the index.

> If I
> change the array number say to 10 instead of three and change the word
> "three" to "ten" in the block it comes as nil...

If you change one of them to "10", then the map is not going to find
an element in your "%w(zero one two three)" array at index 10, so it
returns nil.

> is there something
> special about 1..3 that I am missing?

No, but it would probably be best to familiarise yourself with the
Enumerable methods' documentation. Or maybe go through the Ruby koans
to experiment.

BTW What on earth are you trying to achieve with this code?

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to