Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "sys.stdout.write()" equivalent in Ruby?

As seen in Python, what is the sys.stdout.write() equivalent in Ruby?

like image 493
matt jack Avatar asked Jul 16 '10 13:07

matt jack


People also ask

What is SYS stdout write?

A built-in file object that is analogous to the interpreter's standard output stream in Python. stdout is used to display output directly to the screen console. Output can be of any form, it can be output from a print statement, an expression statement, and even a prompt direct for input.

What is stdout in Ruby?

In Ruby, $stdout represents the current standard output and a global variable. It means we can also use it to output data.

How do I print output in Ruby?

Ruby has another three methods for printing output. In the example, we present the p , printf and putc methods. The p calls the inspect method upon the object being printed. The method is useful for debugging.


2 Answers

In Ruby, you can access standard out with $stdout or STDOUT. So you can use the write method like this:

$stdout.write 'Hello, World!' 

or equivalently:

STDOUT.write 'Hello, World!' 

$stdout is a actually a global variable whose default value is STDOUT.

You could also use puts, but I think that is more analogous to python's print.

like image 70
E.M. Avatar answered Sep 22 '22 21:09

E.M.


puts (or print if you don't want a newline (\n) automatically appended).

like image 27
Jordan Running Avatar answered Sep 23 '22 21:09

Jordan Running