From: "David A. Black" Date: 2009-09-16T04:41:56+09:00 Subject: Re: .map.with_object(3){|v|v+3} #=> 3 Is this a bug? Hi -- On Wed, 16 Sep 2009, 7stud -- wrote: > The next call in the method chain is map(): > >>>> [1,2,3].each.with_object(3).map{|v|v+3} > > map takes the value(s) it is sent, converts the value(s), then stores > the converted value(s) in an array, then the array is returned as the > final result of the call to map(): > > result = [1,2,3].each.with_object(3).map do |v, w| > puts "#{v} #{w}" > v + 3 > end > > p result > > --output:-- > 1 3 > 2 3 > 3 3 > [4, 5, 6] > > That is why you get the result: > >>>> [1,2,3].each.with_object(3).map{|v|v+3} >> => [4, 5, 6] Except.... >> [1,2,3].each.with_object("hi!").map{|v| v + 3 } => [4, 5, 6] The map is only binding one of the arguments, and the argument to with_object is being ignored. (The 3 is hard-coded in the block.) To make use of it in the map, you'd have to do: [1,2,3].each.with_object(3).map{|v,w| v + w } David -- David A. Black, Director Ruby Power and Light, LLC (http://www.rubypal.com) Ruby/Rails training, consulting, mentoring, code review Book: The Well-Grounded Rubyist (http://www.manning.com/black2)