SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: map and each

  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    map and each

    Hi, I'm learning ruby/rails and
    I don't understand the difference between map and each in the below examples. Specifically, why each doesn't work.

    Code:
    irb(main):152:0> %w[a b c].each { |char| char.upcase }
    => ["a", "b", "c"]
    
    irb(main):153:0> %w[a b c].map { |char| char.upcase }
    => ["A", "B", "C"]
    I could ignore it but it's bothering me and I think it might be fundamental later on.

    I tried googling it but the hits I found on the differences used examples I didn't understand and were over my head.

  2. #2
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind. I worked it out. The map method applies the result of the block to element in the array.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •