
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Whenever I tried to make an application that would be based on a command line interface (CLI) I found myself having to do some of the things included in this gem, especially when it came to "verbose", when I wanted to display a question to the user or even check if he entered the input I expected. Tired of always coding the same function I decided to make this gem.
To install this gem just use:
gem install better_input
Normally to handle user input in ruby you would do something like this:
puts "What is your age?"
age = gets.chomp
and then if you wanted a "verbose" input you would have to do another PUTS. And it gets even worse if you want to check if the input is of the expected type:
puts "What is your age?"
age = nil
loop do
input = gets.chomp
if input.match?(/^\d+$/)
age = input.to_i
break
else
puts "Please enter a valid integer for your age."
end
end
puts "User response: #{age}"
Although it requires other approaches like the one I did in the gem. here are some examples of how it works with inputs in ruby using the gem:
require 'better_input'
age = Bi.input("Digite um número: ", type: "int", show_response: true)
# In a single line we asked the question and stored it in a variable,
# we made sure it was the type we wanted and we even made the answer appear!
#Another examples:
noquestion = Bi.input()
retype = Bi.input(show_response: true)
onlyfloat = Bi.input(type: float)
name = Bi.input("What is your name?")
puts "Your name is #{name}"
Anyway, use your creativity, now receiving constant user input for CLI has become less laborious!
This was more or less the base that I ended up making, I made some small improvements that you can find in the files present in this repository before turning it into a gem.
def input(question = nil, show_response: false, type: "string")
unless question.nil?
puts question
end
loop do
var = gets.chomp
if type == "int"
begin
var = Integer(var)
rescue ArgumentError
puts "Err: Please, type a (integer) number."
next
end
elsif type == "float"
begin
var = Float(var)
rescue ArgumentError
puts "Err: Please, type a (float) number."
next
end
elsif type == "bool"
if var.downcase == "true"
var = true
elsif var.downcase == "false"
var = false
else
puts "Err: Please, type 'true' or 'false'."
next
end
else
var = var
end
if show_response
puts "User Response: #{var}"
end
return var
end
end
FAQs
Unknown package
We found that better_input demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.