puts ‘What is your name?’
name = gets
puts ‘What is your city?’
city = gets
puts ‘What is your favorite color?’
color = gets
puts “Your name is #{name}.”, “You live in #{city}.”, “Your favorite
color is #{color}.”
The result I get in terminal is:
Your name is (name input)
.
You live in (city input)
.
Your favorite color is (color input)
.
(Please not that each variable has been assigned and entered by the
user)
I’m wondering if the terminal is ALSO accepting the “\n” keystroke as
part of the input? And if that’s why the period is being pushed down a
line, how do I fix this problem?
puts ‘What is your name?’
name = gets
puts ‘What is your city?’
city = gets
puts ‘What is your favorite color?’
color = gets
puts “Your name is #{name}.”, “You live in #{city}.”, “Your favorite
color is #{color}.”
Now the answer looks
C:\Desktop>ruby d.rb
What is your name?
f
What is your city?
g
What is your favorite color?
h
Your name is f
.You live in g
.Your favorite
color is h
.
Hmm, that’s odd. My quick trial produces the “expected” output.
I would guess that, for reasons too subtle for me to guess, you’re
ending
up with
(puts “Your name is #{name}.”), (“You live in #{city.}”), (“Your
favorite
color is #{color}.”)
rather than a single expression (a puts with three arguments), but I
can’t
imagine why.
puts ‘What is your name?’
name = gets
puts ‘What is your city?’
city = gets
puts ‘What is your favorite color?’
color = gets
print “Your name is #{name.chomp}.”, “You live in #{city.chomp}.”, “Your
Favorite color is #{color.chomp}.”
C:\Desktop>ruby d.rb
What is your name?
My Name
What is your city?
My City
What is your favorite color?
My Color
Your name is My Name.You live in My City.Your favorite color is My
Color.
puts “Your name is #{name}.”, “You live in #{city}.”, "Your favorite
-s
Really? Because you shouldn’t be (unless we have a different idea of
what the expected output should be.)
The issue is that gets will return the EOL character, which is what is
causing Michael’s output to have a return prior to the period in each
sentence.
In other words, you put in “Bob” as your name, followed by ,
which causes the program to received “Bob\n” as the input. As
P.Raveendran points out, you probably want to remove that, by using
chomp or strip.
I’m trying to make a side-scrolling JRPG (like Final Fantasy), so I want
the user to be able to use the keyboard to enter their name. Then I want
to use that name input as a variable to put into dialogue when talking
with NPCs.
i.e. “You defeated the enemy, ‘name’!”
So, I didn’t want the unintended ‘\n’ to get in the way.
On Thu, Feb 11, 2010 at 7:19 AM, Raveendran .P [email protected]
wrote:
color = gets
print “Your name is #{name.chomp}.”, “You live in #{city.chomp}.”, “Your
Favorite color is #{color.chomp}.”
I think it’s better to do this:
puts ‘What is your name?’
name = gets.chomp
puts ‘What is your city?’
city = gets.chomp
puts ‘What is your favorite color?’
color = gets.chomp
print “Your name is #{name}.”, “You live in #{city}.”, “Your Favorite
color is #{color}.”
Because you are probably using the variables in other places, and you
don’t want to be chomping every time you use them.
On Thu, Feb 11, 2010 at 7:19 AM, Raveendran .P [email protected]
wrote:
color = gets
print “Your name is #{name.chomp}.”, “You live in #{city.chomp}.”, “Your
Favorite color is #{color.chomp}.”
I think it’s better to do this:
puts ‘What is your name?’
name = gets.chomp
puts ‘What is your city?’
city = gets.chomp
puts ‘What is your favorite color?’
color = gets.chomp
print “Your name is #{name}.”, “You live in #{city}.”, “Your Favorite
color is #{color}.”
Because you are probably using the variables in other places, and you
don’t want to be chomping every time you use them.
Or use chomp! on first call, though that’s a bit less maintainable.