-1
puts "Example of each"
  x = [1,2,3]
  a = x.each{ |i|
    i+1
  }

  puts a.inspect
  puts x.inspect

puts "Example of map"
  b = x.map{ |i|
    i+1
  }

  puts b.inspect
  puts x.inspect

puts "Example of collect"
  c = x.collect{  |i|
    i+1
  }

  puts c.inspect
  puts x.inspect  

Output

Example of each
[1, 2, 3]
[1, 2, 3]
Example of map
[2, 3, 4]
[1, 2, 3]
Example of collect
[2, 3, 4]
[1, 2, 3]

Here we see each block returns the same value passed to it irrespective of the operation inside it. And the map and collect seems to be same. So basically what is the difference between map and collect?

Nikhil Mohadikar
  • 1,201
  • 15
  • 9
  • 1
    There is a proper explanation http://stackoverflow.com/questions/5254732/difference-between-map-and-collect-in-ruby – Mr.D Aug 12 '16 at 07:25
  • Please follow the link : http://rubyinrails.com/2014/01/25/ruby-difference-between-collect-and-map/ From the explanation in the above link you will get to know that; 1. In Ruby difference between collect and map does not exist. 2. C level implementation of these methods shows that they both are same. 3. So, collect is an alias to Map. – Sam Aug 12 '16 at 09:37

1 Answers1

4

Absolutely nothing, it's an alias.

Ursus
  • 29,643
  • 3
  • 33
  • 50