Hi all, im new to this forum and to Ruby, I started learning it a few
days ago so I could use it with RPG Maker VX Ace. Now im just practicing
but i have run into a small problem that is annoying, I am using
notepad++ to write the code because the Ruby console annoys me. here is
my code. the first name appears on the same line but the last name is
always on the second line.
class GetInput
def getName
puts "Please enter your first name"
first_name = gets
puts "Now your last name"
last_name = gets
print "Thank you ", first_name, " ", last_name
end
end
Hi all, im new to this forum and to Ruby, I started learning it a few
days ago so I could use it with RPG Maker VX Ace. Now im just practicing
but i have run into a small problem that is annoying, I am using
notepad++ to write the code because the Ruby console annoys me. here is
my code. the first name appears on the same line but the last name is
always on the second line.
class GetInput
def getName
puts "Please enter your first name"
first_name = gets
gets includes the return. If you `p first_name` here you can see it. Use `gets.chomp` to remove it before you print.
puts "Now your last name"
last_name = gets
print "Thank you ", first_name, " ", last_name
Use interpolation instead:
puts "Thank you #{first_name} #{last_name}"
···
On Aug 16, 2013, at 00:32 , Chay Hawk <lists@ruby-forum.com> wrote:
Excerpts from Chay Hawk's message of 2013-08-16 09:32:03 +0200:
Hi all, im new to this forum and to Ruby, I started learning it a few
days ago so I could use it with RPG Maker VX Ace. Now im just practicing
but i have run into a small problem that is annoying, I am using
notepad++ to write the code because the Ruby console annoys me. here is
my code. the first name appears on the same line but the last name is
always on the second line.
class GetInput
def getName
puts "Please enter your first name"
first_name = gets
puts "Now your last name"
last_name = gets
print "Thank you ", first_name, " ", last_name
end
end
getinputobj = GetInput.new
getinputobj.getName
gets
That's because the string returned by gets includes the newline (you can
see this using String#inspect). When you print first_name, then, it'll
put the newline character between the first name and last name, which
means the latter will be in a line by itself. To avoid this, you can use
String#strip, which removes all whitespaces sourrounding the string: