# Using print and puts for output

**URL:** <https://www.ruby-forum.com/t/using-print-and-puts-for-output/123073>\
**Category:** Ruby\
**Created:** [December 13, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/using-print-and-puts-for-output/123073 "2007-12-13T20:44:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 13, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/using-print-and-puts-for-output/123073/1 "2007-12-13T20:44:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 13, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/using-print-and-puts-for-output/123073/2 "2007-12-13T21:04:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [December 13, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/using-print-and-puts-for-output/123073/3 "2007-12-13T21:16:06Z")

</div>

On 12/13/07, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)
