gets.chomp method excercise in learn to program book - ruby

I am doing excercise 5.6 out of "Learn to Program" for a class. I have the following:
puts 'What\'s your first name?'
first = gets.chomp
puts 'What\'s your middle name?'
middle = gets.chomp
puts 'What\'s your last name?'
last = gets.chomp
puts 'Hello, Nice to meet you first middle last'
And I have tried the following:
puts 'What is your first name?'
first = gets.chomp
puts 'What is your middle name?'
middle = gets.chomp
puts 'What is your last name?'
last = gets.chomp
puts 'Hello, Nice to meet you #{first} #{middle} #{last}'
When I get the last "puts" it won't get the first, middle and last name that I wrote in. It says, "Hello, nice to meet you first, middle, last....Instead of Joe John Smith for example. What am I doing wrong?
Thank you so much for your help! I really appreciate it!

When using interpolation, use double quotes " instead of single quotes '

Strings within single quotes ' do not replace variables with their values. Try using double quotes " like so:
puts "Hello, Nice to meet you #{first} #{middle} #{last}"
Single qoutes are nice if you want the string as you typed it, double quotes are useful when you want to replace variable names with their values.

You can also use %Q to have the same effect as you get with double quotes ":
x = 12
puts %Q(I need #{x} pens)
# >> I need 12 pens

Related

Why does the text printed after my variable appear on a new line?

puts "Hey what's your name?!!"
name = gets
puts "#outputNAME: #{name}"
puts "Hey #{name}! Howdy doin'!"
puts "Tell us 2 numbers that add up to your age!"
age1 = gets.to_i
age2 = gets.to_i
puts "Hey! You're #{age1 + age2} years old! Gotcha!"
puts "I'm totally new to Ruby on Rails, so thanks for running my first Ruby
Program. Please do leave an upvote and I'll appreciate that."
puts "If you think I need a downvote, please do state, in the comments
section, what I could have done to make my program better (my 1st program,
remember that #{name}). Also, check out my HTML pages, as I'm an ace at
HTML."
print "Thanks, once again #{name} and stay tuned (especially for my HTML
projects)!"
In this part, after #{name}, the sentence is continued on a new line, which I want not to happen. Is there any explanation as to what could be the error in my program, or is it an error in the language? Please help me, as I'm doing an individual project.
Thanks to whoever does find the solution to this problem.
it prints text to the new line after name, because you are accepting newline character in the following code.
name = gets
name = gets
raj
=> "raj\n"
you can remove such newline character by chomp method.
name = gets.chomp
raj
=> "raj"

No breaks in the output

Just started Ruby yesterday (for the first time). And struggling a bit. Please help.
Here's the program:
print "What's your name?"
name = gets.chomp
if name == "James"
print "Someone loves you!"
else
print "Try again #{name}!"
end
print "How old are you?"
age = gets.chomp
if age <= "25"
print "Boy, you are just a child"
elsif age >= "45"
print "Shame on you old man, craddle snacher!"
end
The output is:
enter image description here
So my concern is; why is it not beginning from a new line after "Try again Jack". I would like all the questions and answers to start at a fresh line. Please help!
PS: Just ignore the content of the program. That was just something to keep myself motivated. I don't really mean to be offensive.
2 options, print with explicit linebreaks ( \n , also works on Windows), or puts which adds a linebreak if the string does not already ends with one. These two examples result in the same output:
print "Hello\nworld\n"
puts "Hello
world"

How to display modified string?

I am creating a Daffy Duck speech converter (Very simple. Straight from CodeCademy) and I am having an issue with displaying the modified entry from the user.
Code:
puts "What would you like to convert to Daffy Duck language?"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
print #{user_input}
else puts "I couldn't find any 's' in your entry. Please try again."
end
It will change any 's' in your entry to a 'th', therefore, making it sound like a Daffy Duck once read aloud. When I enter it into the interpreter, it will not display the modified string. It will just display the original entry by the user.
EDIT:
Thanks to the users below, the code is fixed, and I added a notice to the user with converted text. Thanks guys!
A # outside a string starts a comment, so #{user_input} is ignored, i.e.
print #{user_input}
is equivalent to
print
You might wonder why a single print outputs the original input. This is because without arguments print will print $_. That's a global variable which is set by gets:
user_input = gets.chomp # assume we enter "foo"
user_input #=> "foo"
$_ #=> "foo\n"
Everything works as expected if you pass a string literal:
print "#{user_input}"
or simply
print user_input
Note that gsub! returns nil if no substitutions were performed, so you can actually use it in your if statement:
if user_input.gsub!(/s/, "th")
print user_input
else
puts "I couldn't find any 's' in your entry. Please try again."
end
You just need to add double quotes around the string interpolation. Otherwise your code was just returning the input.
puts "What would you like to convert to Daffy Duck language?"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
print "#{user_input}"
else
puts "I couldn't find any 's' in your entry. Please try again."
end
You don't even need interpolation, actually. print user_input works. Notice how StackOverflow was even syntax highlighting your code as a comment. :)

gets.chomp without moving to a new line

I understand about the \n that's automatically at the end of puts and gets, and how to deal with those, but is there a way to keep the display point (the 'cursor position', if you will) from moving to a new line after hitting enter for input with gets ?
e.g.
print 'Hello, my name is '
a = gets.chomp
print ', what's your name?'
would end up looking like
Hello, my name is Jeremiah, what's your name?
You can do this by using the (very poorly documented) getch:
require 'io/console'
require 'io/wait'
loop do
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready? # Process multi-char paste
break if ["\r", "\n", "\r\n"].include?(chars)
STDOUT.print chars
end
References:
http://ruby-doc.org/stdlib-2.1.0/libdoc/io/console/rdoc/IO.html#method-i-getch
http://ruby-doc.org/stdlib-2.1.0/libdoc/io/wait/rdoc/IO.html#method-i-ready-3F
Related follow-up question:
enter & IOError: byte oriented read for character buffered IO
Perhaps I'm missing something, but 'gets.chomp' works just fine does it not? To do what you want, you have to escape the apostrophe or use double-quotes, and you need to include what the user enters in the string that gets printed:
print 'Hello, my name is '
a = gets.chomp
print "#{a}, what's your name?"
# => Hello, my name is Jeremiah, what's your name?
Works for me. (Edit: Works in TextMate, not Terminal)
Otherwise, you could just do something like this, but I realise it's not quite what you were asking for:
puts "Enter name"
a = gets.chomp
puts "Hello, my name is #{a}, what's your name?"

Visible spaces with multiple gets on one line

I'm using Ruby, trying to make an interactive program. (Very new to this, I'm actually learning right now.)
Right now I have
puts 'Can I have your name please?
first = gets.chomp
puts 'Ok, so your first name is ' + first + '? How about your middle name?'
middle = gets.chomp
puts 'ok, so your middle name is ' + middle + '. And your last name?'
last = gets.chomp
puts 'ok, so your last name is ' + last + '?'
puts 'So your full name is ' + first + middle + last + '? That's a very nice name!
'
So far everything works but that last line. When the last line pops up it puts all 3 of the names together with no spaces in between. I've tried a combination of adding or deleting + or adding or deleting spaces or even ' However, nothing I do seems to work, it just stays the same. So how do I get some spaces in between my first middle and last name in that last line?
Thanks in advance for your help.... -John
Something like this
puts "So your full name is #{first} #{middle} #{last}? That's a very nice name!"
This is called string interpolation.
Use join:
[first, middle, last].join " "

Resources