Topic: Noob help: puts

so i have a situation where ruby gets multiple characteristics of a character. when i put the variables, there are unnecessary line breaks. here's my code...

#Beginning game file

#Welcome to my game. I==N00b

class Character
    attr_accessor(:name, :age, :gender, :birthplace)
end

c1=Character.new

print ("What's your character's name? ")
c1.name=gets()

print ("How old is your character? ")
c1.age=gets()

print ("Are you a boy or a girl? ")
c1.gender=gets()

print ("Where is your character from? ")
c1.birthplace=gets()


puts("#{c1.name.to_s} is a #{c1.age.to_i} year old #{c1.gender.to_s} hero from #{c1.birthplace.to_s}.")

This returns the following,

What's your character's name? Joe
How old is your character? 7
Are you a boy or a girl? Boy
Where is your character from? swaziland
fds

Joe
is a 7 year old Boy
hero from swaziland
.

How do I the text to be on one line like this.

Joe is a 7 year old Boy hero from swaziland.

The problem is probably when the user presses the enter button, the enter acts as a new line.

How can I fix this?

Thanks

Last edited by joemama (2012-06-05 21:43:50)

Re: Noob help: puts

Just use String#chomp method on every input string, for example:

print ("What's your character's name? ")
c1.name=gets().chomp

#do the same for the others attributes