ni...">

Why all my puts returns => nil?

I know this may seem like a very simple question, but it really bothers me that my puts continue to generate "=> nil", and I dialed for the answer, but could not find it. Thanks.

puts 'blink' * 4 blink blink blink blink => nil

+6
source share
3 answers

Because this is the return value of puts :

puts (obj, ...) → nil

Writes these objects to ios, as with I / O printing #. Writes a record separator (usually a new line) after it already ends with a new line sequence. If called with an array argument, writes each element on a new line. If called without arguments, displays one record separator.

source: http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-puts

Also, I assume it is only in irb ? because the calls to puts do not display the return value in normal applications.

+8
source

You can use p instead of put s.

p prints and then returns a value.

+5
source

Hunter McMillen's answer is correct.

However, if you want the replacement puts to actually return a value other than nil, I created a gem called reputation .

 reputs 'blink ' *4 blink blink blink blink => "blink blink blink blink " 
+1
source

All Articles