What is the difference between a.map and a.map?

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>

···

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

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

···

Subject: what is the difference between a.map and a.map?
  Date: Sat 12 Jan 13 04:54:43PM +0900

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

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.

···

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