29

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

Rimian
  • 36,864
  • 16
  • 117
  • 117
matt jack
  • 821
  • 2
  • 8
  • 9

4 Answers4

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
7

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

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
6
puts "Hello, world!"

or print - because it buffered.

philant
  • 34,748
  • 11
  • 69
  • 112
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