[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby in cmd

maung

6/15/2007 6:09:00 PM

I am new to Ruby and am trying to run a piece of code from Programming
Ruby in cmd. When i run in cmd >ruby Song.rb, I don't get any output.
But I can get the result I expected when I run in irb. Can anyone help
me on this?

class Song
def initialize(name , artist, duartion)
@name = name
@artist = artist
@duration = duartion
end

def to_s
"Song: #{system @name}--#{@artist} (#{@duration})"
end

def name
@name
end

def artist
@artist
end

def duration
@duration
end
end


aSong = Song.new("NAF", "AS", 300)
aSong.to_s
aSong.artist
aSong.name
aSong.duration

--
Posted via http://www.ruby-....

4 Answers

Daniel Kempkens

6/15/2007 6:12:00 PM

0

maung schrieb:
> I am new to Ruby and am trying to run a piece of code from Programming
> Ruby in cmd. When i run in cmd >ruby Song.rb, I don't get any output.
> But I can get the result I expected when I run in irb. Can anyone help
> me on this?
>
> class Song
> def initialize(name , artist, duartion)
> @name = name
> @artist = artist
> @duration = duartion
> end
>
> def to_s
> "Song: #{system @name}--#{@artist} (#{@duration})"
> end
>
> def name
> @name
> end
>
> def artist
> @artist
> end
>
> def duration
> @duration
> end
> end
>
>
> aSong = Song.new("NAF", "AS", 300)
> aSong.to_s
> aSong.artist
> aSong.name
> aSong.duration
>
You need to put a puts/print in front of everything you want to output.
So for example:
puts aSong.to_s

Chris Hulan

6/15/2007 6:16:00 PM

0

On Jun 15, 2:09 pm, maung <eina...@yahoo.com> wrote:
....
> aSong = Song.new("NAF", "AS", 300)
> aSong.to_s
> aSong.artist
> aSong.name
> aSong.duration
>
To see the output, you have to put it out 9^)

try:
puts aSong.to_s
puts aSong.artist
puts aSong.name
puts aSong.duration

Adriano Ferreira

6/15/2007 6:17:00 PM

0

On 6/15/07, maung <einaung@yahoo.com> wrote:
> I am new to Ruby and am trying to run a piece of code from Programming
> Ruby in cmd. When i run in cmd >ruby Song.rb, I don't get any output.
> But I can get the result I expected when I run in irb. Can anyone help
> me on this?

As there is no print/puts/etc. statement, this non-output is expected
from the file you wrote when run as a script.

irb is a read-eval-print loop. By default, it prints the output of the
expressions and that's why you see the output in this case.

> class Song
> def initialize(name , artist, duartion)
> @name = name
> @artist = artist
> @duration = duartion
> end
>
> def to_s
> "Song: #{system @name}--#{@artist} (#{@duration})"
> end
>
> def name
> @name
> end
>
> def artist
> @artist
> end
>
> def duration
> @duration
> end
> end
>
>
> aSong = Song.new("NAF", "AS", 300)
> aSong.to_s
> aSong.artist
> aSong.name
> aSong.duration
>
> --
> Posted via http://www.ruby-....
>
>

Maung Augn

6/15/2007 6:20:00 PM

0

Adriano Ferreira wrote:
> On 6/15/07, maung <einaung@yahoo.com> wrote:
>> I am new to Ruby and am trying to run a piece of code from Programming
>> Ruby in cmd. When i run in cmd >ruby Song.rb, I don't get any output.
>> But I can get the result I expected when I run in irb. Can anyone help
>> me on this?
>
> As there is no print/puts/etc. statement, this non-output is expected
> from the file you wrote when run as a script.
>
> irb is a read-eval-print loop. By default, it prints the output of the
> expressions and that's why you see the output in this case.

That's explained it. Thanks a lot. It saved me a lot of time.

Maung

--
Posted via http://www.ruby-....