osdir.com
mailing list archive

Subject: Re: print vs puts behaviour for overloaded Array#to_s - msg#00784

List: lang.ruby.general

Date: Prev Next Index Thread: Prev Next Index
>>>>> "S" == Steve Hill <stephen.hill@xxxxxxxxxxxx> writes:

S> Any help would be appreciated.

Because test is an Array, and #puts do something different with an
Array. it make something like your method test#to_s (i.e. it execute a
loop)


Guy Decoux










Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

print vs puts behaviour for overloaded Array#to_s

Hi, I've come across the following feature when using an overloaded to_s method on an array. A simple example should explain it best : class Foo attr_reader :a, :b def initialize @a=rand(10) @b=rand(10) end def to_s ": #{a} #{b} :" end def diff a-b end end test=Array.new def test.to_s str="" self.each { |i| str+="| #{i.diff.to_s} " } str end (1..5).each { test << Foo.new } puts test puts test.to_s print test,"\n" print test.to_s,"\n" This gives the following results: : 3 2 : : 3 6 : : 0 1 : : 9 4 : : 7 1 : | 1 | -3 | -1 | 5 | 6 | 1 | -3 | -1 | 5 | 6 | 1 | -3 | -1 | 5 | 6 So why does puts no pick up the overladed to_s function defined for test, whereas print does? Any help would be appreciated. Steve

Next Message by Date: click to view message preview

RE: Coding challenge (on Ruby Garden)

David Naseby wrote: >> From: George Ogata [mailto:g_ogata@xxxxxxxxxxxxxxxx] >> This bit looks strange to me, though: >> >> if (arr_of_arr.length - (arr_of_arr - arr).length) > arr.length >> ^^^^^^^^^^^^^^^^ > > Of course, you are right.. this line should be > if (arr_of_arr.length - (arr_of_arr.delete_if{|a| a == arr}) > arr.length > > with this change, this latest failure passes. Can't wait for the next one > ;) > > David Hope I didn't keep you waiting too long... ;) one_in_each([1,2,3,4],[[1,3,4],[1,2,3,4],[2,3,4],[]]) should be false.

Previous Message by Thread: click to view message preview

print vs puts behaviour for overloaded Array#to_s

Hi, I've come across the following feature when using an overloaded to_s method on an array. A simple example should explain it best : class Foo attr_reader :a, :b def initialize @a=rand(10) @b=rand(10) end def to_s ": #{a} #{b} :" end def diff a-b end end test=Array.new def test.to_s str="" self.each { |i| str+="| #{i.diff.to_s} " } str end (1..5).each { test << Foo.new } puts test puts test.to_s print test,"\n" print test.to_s,"\n" This gives the following results: : 3 2 : : 3 6 : : 0 1 : : 9 4 : : 7 1 : | 1 | -3 | -1 | 5 | 6 | 1 | -3 | -1 | 5 | 6 | 1 | -3 | -1 | 5 | 6 So why does puts no pick up the overladed to_s function defined for test, whereas print does? Any help would be appreciated. Steve

Next Message by Thread: click to view message preview

Getting the file name from a File::Stat object?

Is there a way to get the file name from a File::Stat object? Thanks in advance. -- Lloyd Zusman ljz@xxxxxxxxxx
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by