Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 126 Vote(s) - 3.68 Average
  • 1
  • 2
  • 3
  • 4
  • 5
One liner in Ruby for displaying a prompt, getting input, and assigning to a variable?

#1
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?
Reply

#2
I found the [Inquirer][1] 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'


[1]:

[To see links please register here]

Reply

#3
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? "
Reply

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

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

Reply

#5
Following @Bryn's lead:

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

#6
Check out [highline][1]:

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

[1]:

[To see links please register here]

Reply

#7
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: "

Reply

#8
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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through