[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using print and puts for output

John Maclean

12/13/2007 7:34:00 PM


## Thu 13 Dec 2007 07:22:13 PM GMT
# just play with an array and see what you can do with it
a = `uname -a`.to_a
print a.class
puts a.class
__END__
what's the difference bewteen these two methods of printing? Both say
the same thing. Also how can I find out for myself with ri? ri IO.print
<< is that it? ri IO.puts << is that it?
--

Regards,

John Maclean
MSc (DIC)
+44 7739 171 531

2 Answers

Sebastian Hungerecker

12/13/2007 8:02:00 PM

0

John Maclean wrote:
> what's the difference bewteen [print and puts]?

puts adds a newline at the end if there isn't one already. print doesn't.


> Also how can I find out for myself with ri? ri IO.print
> << is that it? ri IO.puts << is that it?

Actually it's Kernel#print and Kernel#puts, but those just call IO#print and
IO#puts on stdout (I assume).


HTH,
Sebastian
--
NP: Moonsorrow - Kuin Ikuinen
Jabber: sepp2k@jabber.org
ICQ: 205544826

Rick DeNatale

12/13/2007 8:12:00 PM

0

On 12/13/07, John Maclean <info@jayeola.org> wrote:
>
> ## Thu 13 Dec 2007 07:22:13 PM GMT
> # just play with an array and see what you can do with it
> a = `uname -a`.to_a
> print a.class
> puts a.class
> __END__
> what's the difference bewteen these two methods of printing? Both say
> the same thing. Also how can I find out for myself with ri? ri IO.print
> << is that it? ri IO.puts << is that it?

Puts is defined in terms of print, and handles multiple arguments and
elements ending with a separator somewhat differently

shadowfax:~/ssanta rick$ qri io#puts
---------------------------------------------------------------- IO#puts
ios.puts(obj, ...) => nil
------------------------------------------------------------------------
Writes the given objects to ios as with IO#print. Writes a record
separator (typically a newline) after any that do not already end
with a newline sequence. If called with an array argument, writes
each element on a new line. If called without arguments, outputs a
single record separator.

$stdout.puts("this", "is", "a", "test")

produces:

this
is
a
test

shadowfax:~/ssanta rick$ qri Kernel#print
----------------------------------------------------------- Kernel#print
print(obj, ...) => nil
------------------------------------------------------------------------
Prints each object in turn to $stdout. If the output field
separator ($,) is not nil, its contents will appear between each
field. If the output record separator ($\) is not nil, it will be
appended to the output. If no arguments are given, prints $_.
Objects that aren't strings will be converted by calling their
to_s method.

print "cat", [1,2,3], 99, "\n"
$, = ", "
$\ = "\n"
print "cat", [1,2,3], 99

produces:

cat12399
cat, 1, 2, 3, 99


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...