T
T
ToshiDono2015-12-27 22:13:08
ruby
ToshiDono, 2015-12-27 22:13:08

Why doesn't the gets method work in Sublime Text 3?

In irb the gets method works, but in Sublime Text 3 there is no response. chomp is not defined at all.
Here is an example code:

name = gets.chomp
print name

If you remove chomp, then an error will not occur in the console below, but the possibility of entering data will also not appear.
In general, how to enter data in Ruby using Sublime Text 3?
Maybe you need to include some library or something else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ToshiDono, 2015-12-29
@ToshiDono

By default, Sublime Text doesn't have a terminal, so when you run a build, it executes the entire script and simply displays the result of the entire execution in the build window. Interactive data entry from there is not supported.
However, there is SublimeREPL, a plugin for Sublime Text that adds the ability to execute code without leaving the editor. Installed like any other Sublime Text plugin (don't forget to restart ST after installing the plugin):
The plugin should pick up and use the current active version of ruby, including rvm and rbenv. Ruby requires the pry gem to be installed (there may be problems with the latest pry, in which case it is advised to install pry version 0.9.12.6):

$ gem install pry
# или
$ gem install pry -v 0.9.12.6

REPL запускается через меню команд ST, откроется в новой вкладке:
После того, как REPL запущен, с ним можно работать как с irb в терминале, а также передавать на выполнение файлы, строки, выделенные фрагменты кода (см. клавиатурные сокращения)
Есть проблема, которая касается конкретно gets - изначально это метод Kernel#gets, который будет работать по-разному, в зависимости от того, переданы аргументы (ARGV) скрипту или нет. Чтобы читать из стрима терминала, рекоммендуется указывать это явно:
name = STDIN.gets.chomp
# или
name = $stdin.gets.chomp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question