Using print and puts for output

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 M.
MSc (DIC)
+44 7739 171 531

John M. 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

On 12/13/07, John M. [email protected] 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.denhaven2.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs