Need help with Code (Input Functionality)

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

···

---------------------------------------

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?

Thanks!

-SS
--
Posted via http://www.ruby-forum.com/.

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
.

Let me know if anything need further ?

Thanks
P.Raveendran

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 2010-02-11, Michael Brodeur <mikejoebro@yahoo.com> wrote:

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

---------------------------------------

The result I get in terminal is:

---------------------------------------

Your name is (name input)

--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

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

Hey Guys!

Thank you so much for the fix! :slight_smile:

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

···

--
Posted via http://www.ruby-forum.com/.

Seebs wrote:

  

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

---------------------------------------

The result I get in terminal is:

---------------------------------------

Your name is (name input)
    
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

···

On 2010-02-11, Michael Brodeur <mikejoebro@yahoo.com> wrote:

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 ?

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 2010-02-11, Justin Collins <justincollins@ucla.edu> wrote:

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

--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

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.

···

On Thu, Feb 11, 2010 at 7:19 AM, Raveendran .P <jazzezravi@gmail.com> wrote:

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

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

Jesus.

Best,

···

On Thu, Feb 11, 2010 at 7:19 AM, Raveendran .P <jazzezravi@gmail.com> > wrote:

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.