开发者

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

As seen in Py开发者_JAVA百科thon, what is the sys.stdout.write() equivalent in Ruby?


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.


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


puts "Hello, world!"

or print - because it buffered.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜