Ruby .gets doesn't work

ε祈祈猫儿з 提交于 2019-12-04 17:07:11

Here's how I understand it. gets and puts are instance methods of IO, and the default IOs are $stdout and $stdin.

Calls to gets/puts will only be effective if the translator is capable of handling stdout/in e.g. IRB

If you run a ruby file from bash it works too.

io_test.rb

puts gets

in bash

ruby io_test.rb

Then it will "put" into stdout whatever it "gets" from stdin.

Try using $stdin.gets instead of just a plain gets, this will force the input to come from stdin

If you want to run code within ST2, check out the SublimeREPL plugin, available through Package Control. While you can use IRB, its main Ruby interface is through pry, which is a lot more powerful. You can use it as a classic REPL (think Clojure or LISP), and you can also transfer your code from one tab into the running REPL in another tab by selection, line range, or block.

Interestingly, your code above works in IRB for me, but not pry for some reason - it's reading my $EDITOR environment variable, which is set to subl -w but failing with Errno::ENOENT: No such file or directory - subl -w. Strange...

At any rate, I highly highly recommend SublimeREPL, as it's a really powerful tool, and is self-contained within ST2, so you don't have to keep flipping back and forth to your terminal, saving and reloading your programs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!