I just don't get it, how does it know which value to write out? 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... is there something
special about 1..3 that I am missing?

ary = [0,1,1,1,1,3]
ary.map! { |num| %w(zero one two three)[num] }

p ary
["zero", "one", "one", "one", "one", "three"]

--
Posted via http://www.ruby-forum.com/.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Azizmb.in at May 18, 2012 at 4:52 am
    http://lmgtfy.com/?q=ruby+map+collect
    On Fri, May 18, 2012 at 9:27 AM, Christopher D. wrote:

    I just don't get it, how does it know which value to write out? 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... is there something
    special about 1..3 that I am missing?

    ary = [0,1,1,1,1,3]
    ary.map! { |num| %w(zero one two three)[num] }

    p ary
    ["zero", "one", "one", "one", "one", "three"]

    --
    Posted via http://www.ruby-forum.com/.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.

    --
    - Aziz M. Bookwala

    Website <http://azizmb.in/> | Twitter <https://twitter.com/azizbookwala> |
    Github <http://github.com/azizmb>

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at May 18, 2012 at 8:34 am

    On 18 May 2012 04:57, Christopher D. 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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Rafael Magaña at May 19, 2012 at 9:26 pm
    When you use ary = [0,1,1,1,1,10] map! ends up trying to do this:

    %w(zero one two ten)[10]


    So it's trying to get the tenth element of an array of only 4 elements,
    that's why it returns nil, the only way to make it work as you want is:

    %w(zero one two three four five six seven eight nine ten)[10] # => "ten"

    So an array of 11 elements.
    On Thursday, May 17, 2012 10:57:50 PM UTC-5, Ruby-Forum.com User wrote:

    I just don't get it, how does it know which value to write out? 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... is there something
    special about 1..3 that I am missing?

    ary = [0,1,1,1,1,3]
    ary.map! { |num| %w(zero one two three)[num] }

    p ary
    ["zero", "one", "one", "one", "one", "three"]

    --
    Posted via http://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1yTDXkzodi0J.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMay 18, '12 at 3:57a
activeMay 19, '12 at 9:26p
posts4
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase