IO output problem

K

Kevin

I run one of examples of Programming Ruby, code as follows:
------------
class Sone
....
end

song = Sone.new(.., .., ..)
song.inspect
song.to_s
 
D

dblack

Hi --

I run one of examples of Programming Ruby, code as follows:
------------
class Sone
...
end

song = Sone.new(.., .., ..)
song.inspect
song.to_s

inspect and to_s just produce strings. To see them, you have to print
them :) In fact, there are some handy methods for this:

p song # automatically calls inspect
puts song # automatically calls to_s

Also, if you are working in irb (the interactive Ruby terminal),
you'll see the value of every expression you type in. So in irb, if
your expression evaluates to a string, you'll see the string without
having to explicitly ask for it to be printed.


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

Daniel Finnie

First, Song is misspelled to Sone, it seems.

To answer your question, use puts song.to_s to display output. Puts is
technically a method of Kernel, but you can use it as just puts anywhere
in your Ruby program.

Gets does (kind of) the opposite of puts and "gets" what a user at the
command line typed.

Dan
 
K

Kevin

I understand now, thank you both of you
Hi --



inspect and to_s just produce strings. To see them, you have to print
them :) In fact, there are some handy methods for this:

p song # automatically calls inspect
puts song # automatically calls to_s

Also, if you are working in irb (the interactive Ruby terminal),
you'll see the value of every expression you type in. So in irb, if
your expression evaluates to a string, you'll see the string without
having to explicitly ask for it to be printed.


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
X

x1

It's been a while since I've read that example but for some reason, I
remember thinking it was just an example to show how Ruby's object
orientation worked... not a real functional example. Perhaps it was
and I completely missed it. Dunno..
 
D

dblack

Hi --

To answer your question, use puts song.to_s to display output.

Or just puts song. to_s will be called automatically:

irb(main):001:0> o = Object.new
=> #<Object:0xb7f06840>
irb(main):002:0> def o.to_s; "Here I am!"; end
=> nil
irb(main):003:0> puts o
Here I am!

:)


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,220
Messages
2,571,128
Members
47,744
Latest member
FrederickM

Latest Threads

Top