[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

with and without Song#to_s again

Li Chen

12/30/2006 4:08:00 PM

Hi all,

I posted a similar question a short time ago. But I
don't think the replies help me understand the
behaviors. So I post a short version( sorry if it
bothers you again).

I have the following script. I don't call Song#to_s in
my script but I can print all the attributes of a
song. Why is that?

Thanks,

Li

#########
class Song
def initialize(name,artist,duration)
@name=name
@artist=artist
@duration=duration
end
#attr_reader :name, :artist, :duration

def to_s
"#@name\t#@artist\t\t#@duration"
end
end


s=Song.new('song1','author1','20')
puts s

######output
>ruby ruby20c.rb
song1 author1 20
>Exit code: 0


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail...

9 Answers

Vincent Fourmond

12/30/2006 4:16:00 PM

0

chen li wrote:
> #########
> class Song
> def initialize(name,artist,duration)
> @name=name
> @artist=artist
> @duration=duration
> end
> #attr_reader :name, :artist, :duration
>
> def to_s
> "#@name\t#@artist\t\t#@duration"
> end
> end
>
>
> s=Song.new('song1','author1','20')
> puts s
>
> ######output
>> ruby ruby20c.rb
> song1 author1 20
>> Exit code: 0

As s is not a String (that is not s.is_a? String), it is converted
into string by puts. That exactly means your method to_s is called for
doing so.

Vince
--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Gavin Kistner

12/30/2006 4:37:00 PM

0

chen li wrote:
> I have the following script. I don't call Song#to_s in
> my script but I can print all the attributes of a
> song. Why is that?

chen li, you can find this answer yourself.
1) Open a console and type:
ri Kernel.puts

2) See that it says that it's the same as $stdout.puts. Type:
ri IO.puts

3) See that it says that it does the same thing as print does. Type:
ri IO.print

4) See that it tells you that it calls the to_s method of any objects
that aren't strings.

Deaf Power

6/22/2007 12:05:00 AM

0


On Thu, 21 Jun 2007 06:41:59 -0700, Trace <tracey12_12@yahoo.com>
wrote:

>Why hasn't anyone stated what a lefttard is?
>
>
>Post your definitions here:

Use your dictionary.

http://dictionary.reference.com/brows...

"No results found for lefttard."



--
Impeach Bush!
http://www.gnn.tv/articles/2791/Rep_McKinney_Files_Articles_of_I...


Phlip

6/22/2007 1:35:00 AM

0

Trace wrote:

> Why hasn't anyone stated what a lefttard is?
>
> Post your definitions here:

Someone who recognizes and opposes Fascism, which is criminal collusion
between business and government.

For example, during World War II didn't our government have systems to
prevent military contractors from lobbying ("bribing") the government?

We don't seem to have these checks today. Instead, we have the head of a
defense contractor installed as our Vice President.

A leftard is someone who recognizes this insurmountable conflict of interest
for what it really is: Premeditated crimes against humanity.

--
Phlip
http://flea.sourceforge.net/Pigleg...


roneal

6/22/2007 2:13:00 AM

0


"Trace" <tracey12_12@yahoo.com> wrote in message
news:1182433319.834976.250080@o61g2000hsh.googlegroups.com...
> Why hasn't anyone stated what a lefttard is?
>
>
> Post your definitions here:
>

Re: "Why hasn't anyone stated what a lefttard is?"

I did. Read my post of 6/20/07.

RO



WWJBD

7/6/2007 12:28:00 AM

0

On Thu, 05 Jul 2007 16:39:53 -0700, "Paul Duca
(tomservo56954@comcast.net)" <tomservo56954@comcast.net> getting
another dose of reality

Slang for the liberal/socialist/progressive moron.
Next question

.

7/8/2007 11:42:00 PM

0

WWJBD@ticking_clock.com wrote:
> On Thu, 05 Jul 2007 16:39:53 -0700, "Paul Duca
> (tomservo56954@comcast.net)" <tomservo56954@comcast.net> getting
> another dose of reality
>
> Slang for the liberal/socialist/progressive moron.
> Next question

See also: right wing fuckwit: descriptive language for right wing suckasses
who go on the internet to try to defend their goddam criminal Republican
Party.

Hertz_Donut

7/9/2007 1:56:00 AM

0


"." <.@.com> wrote in message news:46917642$1@nntp.zianet.com...
> WWJBD@ticking_clock.com wrote:
>> On Thu, 05 Jul 2007 16:39:53 -0700, "Paul Duca
>> (tomservo56954@comcast.net)" <tomservo56954@comcast.net> getting
>> another dose of reality
>>
>> Slang for the liberal/socialist/progressive moron.
>> Next question
>
> See also: right wing fuckwit: descriptive language for right wing
> suckasses
> who go on the internet to try to defend their goddam criminal Republican
> Party.

The idiot that posted this is a perfect definition of a leftard...

Too much of a coward to post his name.
Cannot spell.
Cannot form a grammatically correct sentence.
Makes wild claims that are completely unfounded.
Third grade education.

Honu



.

7/10/2007 7:27:00 AM

0

Hertz_Donut wrote:
> "." <.@.com> wrote in message news:46917642$1@nntp.zianet.com...
>> WWJBD@ticking_clock.com wrote:
>>> On Thu, 05 Jul 2007 16:39:53 -0700, "Paul Duca
>>> (tomservo56954@comcast.net)" <tomservo56954@comcast.net> getting
>>> another dose of reality
>>>
>>> Slang for the liberal/socialist/progressive moron.
>>> Next question
>> See also: right wing fuckwit: descriptive language for right wing
>> suckasses
>> who go on the internet to try to defend their goddam criminal Republican
>> Party.
>
> The idiot that posted this is a perfect definition of a leftard...
>
> Too much of a coward to post his name.
> Cannot spell.
> Cannot form a grammatically correct sentence.
> Makes wild claims that are completely unfounded.
> Third grade education.
>
> Honu
>
>
>
Suck a dick. I'm not posting the goddam lies YOU are, Shithead.