Simple ruby: adding strings / results from get? -
Getting the full name
puts' Hello, and what's your name? 'Name = gets.chomp says' your first name is '+ name +'? What a beautiful name! 'What is your second name?' The name = gets.chomp puts' Your second name is' + name + '. 'What is your last name?' Name = gets.chomp ', then your last name is:' + name + '.' I am unable to get the full name at the end.
puts 'your full name:' name + name + name '.' ^ Whenever I try to do it, it seems wrong.
Do I have to make a variable after each name?
It seems that you have trouble in the concept of a variable.
There is a condition in a variable memory. Store any kind of data. You can see the data using variables by using the name of the variable. Think of it as a box with labels, for simplicity, suppose you can put the same thing in the box.
When you enter the line name = gets.chomp , then what you are saying is basically "store this information, enter the user box There is a condition in the box memory that the label is how you can find it and retrieve the stored value.
Then, when you write a second time, then you are doing the very same thing. You enter that user's information (his second name) in the box labeled name . However, you did not just add it to the info box. Then, when you try to get the full name in the bridge, then your full name is: 'name + name + name'. ' you are actually getting the same value from the same "box" three times.
So, what you need is three different "box", or in the programming terminology, three variables. If you use a different variable for each name part, then you get something like the code below Will get:
puts 'hello, and what's your name?' First_name = gets.chomp puts' your first name is' + first_name + '? What a beautiful name! 'What is your second name?' Second_name = gets.chomp puts' Your second name is' + second_name + '. 'What is your last name?' Last_name = gets.chomp puts 'So your last name is:' + last_name + '.' Puts 'your full name:' + first_name + second_name + last_name + '.' There are ways to put more complex things inside the variable, like collection of things, instead of single things you will reach it soon.
Comments
Post a Comment