Using print and puts for output

J

John Maclean

## 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
 
S

Sebastian Hungerecker

John said:
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
 
R

Rick DeNatale

## 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
 

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
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top