I am using “Programmers Notepad” to write Ruby code. I tried following
code, but gets() is not waiting to accept the input. The whole program
execution finishes when I run the program.
========= CODE ==========
Rubist R. [email protected] wrote:
I am using “Programmers Notepad” to write Ruby code. I tried following
code, but gets() is not waiting to accept the input. The whole program
execution finishes when I run the program.========= CODE ==========
puts ‘Enter your name’
name = gets()
puts ‘Hello #{name}’
Unless you are specifying a file name on the command line, you probably
want this to read from the console. For that, you need STDIN.gets.
Also, the string substitution in your third line will only happen if you
use double quotes. So, I think you want:
puts ‘Enter your name’
name = STDIN.gets
puts “Hello #{name}”
The code I have shown in my original post is from the book: “The Book of
Ruby”. I tried double quotes also, but it didn’t work.
On Wed, Nov 17, 2010 at 9:45 AM, Rubist R.
[email protected] wrote:
The code I have shown in my original post is from the book: “The Book of
Ruby”. I tried double quotes also, but it didn’t work.
STDIN is the default, so it’s optional. The double quotes are required
for interpolation to take place. If the variable name = ‘Someone’
With double quotes
puts “Hello #{name}”
Hello Someone
With single quotes
puts ‘Hello #{name}’
Hello #{name}
I think the problem is in the way you are running the script. What OS
are you running on and how are you invoking the script?
Regards,
Ammar
On Wed, Nov 17, 2010 at 8:45 AM, Rubist R.
[email protected] wrote:
The code I have shown in my original post is from the book: “The Book of
Ruby”. I tried double quotes also, but it didn’t work.
Can you show us a transcript of the console session? On what
operating systems do you work?
Kind regards
robert