Need help with Code (Input Functionality)

M

Michael Brodeur

When I run the following code:

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}."
 
S

Seebs

When I run the following code:

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}."

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.

-s
 
R

Raveendran Perumalsamy

Hi,


Did you heard about chomp method earlier?

If no then please go thru the link -->
http://rubylearning.com/satishtalim/getting_input.html


NOTE:

Please try this code :


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
 
J

Justin Collins

Seebs said:
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.

-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 <enter>,
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.

-Justin
 
R

Raveendran .P

Hi,

The exact solution for above(first post) issue


Code:

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.


Please update if any other info needed ?
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Here is an explanation of why you get what you do, and examples of how to
resolve it.

# user enters "josh" and presses return. The return remains in the string,
as a newline
# meaning it should end in "\n", a character which, when output, indicates
the text after it
# should be placed on the next line.
puts 'What is your name?'
name = gets
puts "I received #{name.inspect}" # will output something like 'I received
"josh\n"'

puts 'What is your city?'
city = gets
puts "I received #{city.inspect}"

puts 'What is your favorite color?'
color = gets
puts "I received #{color.inspect}"

# so we when we interpolate name, the string becomes "Your name is josh\n."
# where "josh\n" was submitted by the user
# this displays as:
# Your name is josh
# .
#
# notice the "\n" became a newline when displayed to the screen, so the
period after it
# was displayed on the next line

# the second issue, is that individual arguments to puts are each placed on
their own line
# so you have "Your name is josh\n." that is the first argument, so puts
places a newline
# and we move to the next line before we output "You live in Wichita\n." And
since that is
# it's own argument, another newline goes after it. And then the last
argument is output, also
# with it's own newline.
puts "Your name is #{name}.", "You live in #{city}.", "Your favorite color
is #{color}."

# thus this becomes:
# "Your name is josh\n.\nYou live in Wichita\n.\nYour favorite color is
black\n.\n"
#
# which outputs like this
# Your name is josh
# .
# You live in Wichita
# .
# Your favorite color is black
# .
#

# To resolve the issues, you must do two things:
# 1. use the String method chomp to remove any newlines
# ie: gets.chomp, see documentation and examples at
http://ruby-doc.org/core/classes/String.html#M000819
#
# 2. Either join all the strings together to avoid newlines between them, as
in
# "Your name is #{name}. You live in #{city}. Your favorite color is
#{color}."
# or use a function that doesn't place newlines after each argument such as
print:
# print "Your name is #{name}.", "You live in #{city}.", "Your favorite
color is #{color}."
 
S

Seebs

Really? Because you shouldn't be (unless we have a different idea of
what the expected output should be.)

I was expecting to see output from all of them, but if I understood the
post correctly (maybe I didn't), only the first one printed.

-s
 
M

Michael Brodeur

Hey Guys!

Thank you so much for the fix! ^_^

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.

Thanks again!

-SS
 
J

Jesús Gabriel y Galán

The exact solution for above(first post) issue


Code:

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}."

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.

Jesus.
 
M

Marnen Laibow-Koser

Jesús Gabriel y Galán said:
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.

Best,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,185
Messages
2,570,993
Members
47,586
Latest member
DLWKathie7

Latest Threads

Top