gets.chomp without moving to a new line - ruby

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?"

Related

Ruby- gets issue

I'm currently in the process of learning ruby, and I don't know if I'm doing something wrong or the compiler is, but this code:
puts "Name?"
name = gets
puts "Welcome " + name
Outputs:
#blank line waiting for input, if gotten input
Prints input, Name? And Welcome Name
I want it to do something like python's input("Name? ")
You can write your own Python equivalent input method:
def input(prompt)
print(prompt) # Output prompt
$stdout.flush # Flush stdout buffers to ensure prompt appears
gets.chomp # Get user input, remove final newline with chomp
end
Now we can try it:
name = input('What is your name? ')
puts "Welcome #{name}"
For more information on the methods used here. See these:
IO.flush
String.chomp

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. :)

setting variables equal to 'gets' in Ruby

I want to write a program that asks for two strings from the user and searches for one within the other, but I'm having some trouble making it work. The following returns "not" even when the given character is present within the string. Does anyone know what I'm doing wrong?
puts 'Enter the string that you would like to search'
content = gets
puts 'What character would you like to find?'
query = gets
if content.include? query
puts "here"
else
puts "not"
end
gets returns the string from the user including the newline character '\n' at the end. If the user enters "Hello world" and "Hello", then the strings really are:
"Hello World\n"
"Hello\n"
That makes it obvious, why your code does not find a match.
Use chomp to remove that newline characters from the end of the string.
puts 'Enter the string that you would like to search'
content = gets.chomp
puts 'What character would you like to find?'
query = gets.chomp
if content.include?(query)
puts "here"
else
puts "not"
end

Resources