benny
3/22/2006 8:26:00 PM
>> 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?
benny
> Try map instead
> (here I use map! to change this array, rather than creating a new one):
>
> ##########################################
> require 'test/unit'
> class TOY_CASE<Test::Unit::TestCase
>
> def test_toy_test
>
> aoa = [[1,2,3],[4,5,6]]
>
> aoa.each do |arr|
> arr.map! do |item|
> item.to_s
> end
> end
>
> assert_equal [["1","2","3"],["4","5","6"]], aoa
> end #def
> end #class
> ##########################################
>
> Notice too I changed the way your assertion works, since it would fail
> on the second array if tested against ["1","2","3"].
>