Hello. I’ve been working through learning Ruby and Cloud 9 (an IDE). I understand how to write code in the editor and preview it in Bash (the terminal), but I can’t figure out how to enter input. For instance, when I write puts “What’s your name?” in the editor, how/where do I enter input to answer the question (or basically collect user input)? The terminal only prints out my puts statement, but I can’t give input there. What am I missing? Thank you.
Exemplary code:
puts "What's your name?"
name = gets
puts "Hello #{name}"
After executing this script you just need to write the name in the bash window and press enter, that’s all.
Should I be in irb, or just the shell?
It does not matter. I have created ubuntu based workspace on the Cloud9. I created a simple ruby script:
factoradic:~/workspace $ cat script.rb
puts "What\'s your name?"
name = gets
puts "Hello #{name}"
Now, you can simply run this script:
factoradic:~/workspace $ ruby script.rb
What's your name?
Maciej
Hello Maciej
or you can enter irb
and load this script:
factoradic:~/workspace $ irb
2.3.0 :001 > load './script.rb'
What's your name?
Maciej
Hello Maciej
=> true
2.3.0 :002 > quit