Print and puts. What inputs do they require?

print while gets != “42\n”

I saw this code snippet as the answer to the first question on codechef.

How does print know to output what “gets” receives? I thought you
required a string in the form of a variable or string literal in front
of print. Also, when replacing print with puts how come it doesn’t
output the numbers any more? I know that puts adds a newline to the end
of a string if it doesn’t exist, but from the example above this doesn’t
seem to be the only difference. Sorry for the newbish questions but I
would
appreciate the help! Thanks.

On Jan 25, 2012, at 00:50 , Kane W. wrote:

would
appreciate the help! Thanks.

Not newbish. You just don’t know where to look. In this case, ri is
your friend. Check it:

% ri gets
= .gets

(from ruby core)
=== Implementation from IO

ios.gets(sep_string=$/) => string or nil


Reads the next ``line’’ from the I/O stream; lines are separated by
sep_string. A separator of nil reads the entire contents,
and a zero-length separator reads the input a paragraph at a time (two
successive newlines in the input separate paragraphs). The stream must
be
opened for reading or an IOError will be raised. The line read in will
be
returned and also assigned to $_. Returns nil if called at end of file.

   File.new("testfile").gets   #=> "This is line one\n"
   $_                          #=> "This is line one\n"

% ri print

(from ruby core)
=== Implementation from IO

ios.print() => nil
ios.print(obj, …) => nil


Writes the given object(s) to ios. The stream must be opened for
writing. If the output record separator ($</code>) 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. With no
argument, prints the contents of the variable $
. Returns nil.

   $stdout.print("This is ", 100, " percent.\n")

produces:

   This is 100 percent.

How does print know to output what “gets” receives?
Also, when replacing print with puts how come it doesn’t
output the numbers any more?

To decipher Ryan’s answer, print with no arguments prints $, which
was just set by gets. And a solo puts doesn’t print $
; instead it
prints a newline.

I agree with Ryan that ri is your friend, and I wish that RVM
installed ri files by default instead of making you run “rvm docs
generate”.

Ah cool, thanks you guys. Your answers have helped a ton!

Also this question is completely off topic but is there anyway to search
to for symbols in google? Google ends up stripping a lot of these off,
and trying to increase my ruby vocabulary can be quite a challenge when
I can’t search for it. I tried to search for an answer to this
(ironically by google) and came upon a website called SymbolHound,
however its search engine isn’t as robust as googles. It’s but a minor
irrelevant question but an answer would be helpful ;).

Try this: http://www.zenspider.com/Languages/Ruby/QuickRef.html

– Matma R.

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