Hi,
I am confused with the working of a.map and a.map! on the array
collection.
irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> a.map {|n| n+1}
=> [2, 3, 4, 5]
irb(main):003:0> puts a.inspect
[1, 2, 3, 4]
=> nil
irb(main):004:0> a.map! {|n| n+1}
=> [2, 3, 4, 5]
irb(main):005:0> puts a.inspect
[2, 3, 4, 5]
=> nil
irb(main):006:0>
on 2013-01-12 08:54
on 2013-01-12 09:00
Subject: what is the difference between a.map and a.map? Date: Sat 12 Jan 13 04:54:43PM +0900 Quoting Arup Rakshit (lists@ruby-forum.com): > I am confused with the working of a.map and a.map! on the array > collection. The documentation is quite clear: $ ri Array#map ... ... Invokes the given block once for each element of self. Creates a new array containing the values returned by the block. ... ... $ ri Array#map! ... ... Invokes the given block once for each element of self, replacing the element with the value returned by the block. ... ... Methods ending with ! usually modify the original object. Carlo
on 2013-01-12 11:07
You'll find this symbol on quite a few methods in Ruby, in general there will only be a method with "!" (modify the object in place) if there's also one which returns a new object without changing the original. Look up "Ruby Bang Methods" for more info.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.