From: Ross Bamford Date: 2006-03-23T07:10:44+09:00 Subject: Re: why doesn't "to_s" DWIM? On Thu, 2006-03-23 at 05:28 +0900, benny wrote: > >> aoa = [[1,2,3],[4,5,6]] > >> > >> aoa.each do |arr| > >> arr.each do |item| > >> item = item.to_s > >> end > >> assert_equal(["1","2","3"],arr) > >> end > >> > >> > >> end #def > >> end #class > >> ########################################## > > > > Each just iterates the items, and the 'items' argument to the block is a > > new local variable - changing it has no lasting effect. > but remember: > > a = ["haha", "hihi", "hoho"] > a.each { |i| i.gsub!("h", "l") } > p a > > and even > > a = ["2","3", "4"] > a.each { |i| i.replace("#{i} times") } > p a > > in fact variables only point to the objects. so you might use them to modify > the object they are currently pointing to. if you got strings, you may > modify them (instead of creating new ones). but unfortunately that won't > work for numbers, since they are no real objects, but - how do we call > them? - first level objects? > True, this is why the updated code I and others posted mostly retained the outer 'each', and used map! on each inner array. I think the term for Fixnums, nil, true and false is 'immediate values'. -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk