One liner in Ruby for displaying a prompt, getting input, and assigning to a variable?

RubyInput

Ruby Problem Overview


Often I find myself doing the following:

print "Input text: "
input = gets.strip

Is there a graceful way to do this in one line? Something like:

puts "Input text: #{input = gets.strip}"

The problem with this is that it waits for the input before displaying the prompt. Any ideas?

Ruby Solutions


Solution 1 - Ruby

I think going with something like what Marc-Andre suggested is going to be the way to go, but why bring in a whole ton of code when you can just define a two line function at the top of whatever script you're going to use:

def prompt(*args)
	print(*args)
	gets
end

name = prompt "Input name: "

Solution 2 - Ruby

Check out highline:

require "highline/import"
input = ask "Input text: "

Solution 3 - Ruby

One liner hack sure. Graceful...well not exactly.

input = [(print 'Name: '), gets.rstrip][1]

Solution 4 - Ruby

I know this question is old, but I though I'd show what I use as my standard method for getting input.

require 'readline'

def input(prompt="", newline=false)
  prompt += "\n" if newline
  Readline.readline(prompt, true).squeeze(" ").strip
end

This is really nice because if the user adds weird spaces at the end or in the beginning, it'll remove those, and it keeps a history of what they entered in the past (Change the true to false to not have it do that.). And, if ARGV is not empty, then gets will try to read from a file in ARGV, instead of getting input. Plus, Readline is part of the Ruby standard library so you don't have to install any gems. Also, you can't move your cursor when using gets, but you can with Readline.

And, I know the method isn't one line, but it is when you call it

name = input "What is your name? "

Solution 5 - Ruby

Following @Bryn's lead:

def prompt(default, *args)
  print(*args)
  result = gets.strip
  return result.empty? ? default : result
end

Solution 6 - Ruby

The problem with your proposed solution is that the string to be printed can't be built until the input is read, stripped, and assigned. You could separate each line with a semicolon:

$ ruby -e 'print "Input text: "; input=gets.strip; puts input'
Input text: foo
foo

Solution 7 - Ruby

I found the Inquirer gem by chance and I really like it, I find it way more neat and easy to use than Highline, though it lacks of input validation by its own.
Your example can be written like this

require 'inquirer'
inputs = Ask.input 'Input text'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJarsenView Question on Stackoverflow
Solution 1 - RubyBrynView Answer on Stackoverflow
Solution 2 - RubyMarc-André LafortuneView Answer on Stackoverflow
Solution 3 - RubyrbnewbView Answer on Stackoverflow
Solution 4 - RubyAddisonView Answer on Stackoverflow
Solution 5 - RubyTiago FernandezView Answer on Stackoverflow
Solution 6 - RubymaericsView Answer on Stackoverflow
Solution 7 - RubyRedithionView Answer on Stackoverflow