Forum: Ruby why doesn't "to_s" DWIM?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris McMahon (Guest)
on 2006-03-22 18:18
(Received via mailing list)
How come to_s doesn't work here?

####################################
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.each do |item|
		item = item.to_s
		end
		assert_equal(["1","2","3"],arr)
end


end #def
end #class
##########################################
unknown (Guest)
on 2006-03-22 18:25
(Received via mailing list)
Hi --

On Thu, 23 Mar 2006, Chris McMahon wrote:

>
> ##########################################
Inside the inner each block, you're just doing a local (re)assignment
to "item".  There's no connection to the object that used to be in
item, except that that object happens to be part of the rhs.

You can achieve what you want with this:

   aaa.each do |arr|
     arr.map! do |item|
       item.to_s
     end
   end

That will go through each inner array and replace the current item
with what's in the block (namely, item.to_s).


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Farrel L. (Guest)
on 2006-03-22 18:27
(Received via mailing list)
You might want to look at Array#map

arr.map do |item|
  item.to_s
end
Ross B. (Guest)
on 2006-03-22 18:28
(Received via mailing list)
On Thu, 2006-03-23 at 01:18 +0900, Chris McMahon wrote:
> 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. 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"].
Peter H. (Guest)
on 2006-03-22 18:29
(Received via mailing list)
I'm not really sure what you are expecting to happen here. aoa.each will
product two results, first [1,2,3] and then [4,5,6]. And you expect the
assert_equal to pass both times?
Chris McMahon (Guest)
on 2006-03-22 20:14
(Received via mailing list)
Thanks, I knew it had to be something like that.
benny (Guest)
on 2006-03-22 22:29
(Received via mailing list)
>> 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
benny (Guest)
on 2006-03-22 22:48
(Received via mailing list)
benny wrote:

> them? - first level objects?
so this would work also:

aoa = [[1,2,3],[4,5,6]]

aoa.each do |arr|
  arr.each_index do |i|
    arr[i] = arr[i].to_s
  end
end

p aoa
Ross B. (Guest)
on 2006-03-23 00:10
(Received via mailing list)
On Thu, 2006-03-23 at 05:28 +0900, benny wrote:
> >> end #def
>
> 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'.
This topic is locked and can not be replied to.