As seen in Python, what is the sys.stdout.write()
equivalent in Ruby?
Asked
Active
Viewed 2.4k times
29
-
1it will be good if you explain what `sys.stdout.write()` does in python – Salil Jul 16 '10 at 13:15
-
@Salil: It writes a string to the standard output. – Philipp Jul 16 '10 at 13:23
4 Answers
51
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
.

E.M.
- 4,498
- 2
- 23
- 30
0
Here is a one liner for both writing and reading to/from stdin and stdout.
$ ruby -e '$stdout.write "hi\n" '
hi
$ echo "somestring" | ruby -e 'p $stdin.read'
"somestring\n"
$ echo "somestring" | ruby -e 'puts $stdin.read'
somestring

z atef
- 7,138
- 3
- 55
- 50