From: Ryan Davis Date: 2012-01-25T17:59:17+09:00 Subject: Re: Print and puts. What inputs do they require? On Jan 25, 2012, at 00:50 , Kane Williams wrote: > 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. 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 ($\) 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.