better_input 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/README.md +86 -16
- data/Rakefile +0 -0
- data/better_input-0.1.0.gem +0 -0
- data/better_input-0.1.1.gem +0 -0
- data/lib/better_input/version.rb +2 -2
- data/lib/better_input.rb +6 -1
- data/sig/better_input.rbs +0 -0
- metadata +17 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91f6acc7fd325931d683a59962503b1f3fc6dec3a829bb1752b88dcac0e27032
|
4
|
+
data.tar.gz: 17f6a25540c3643f6bf1173db8189489327286749b221accbcbee451cff4cbc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 475f4143667b67e376c2188641d217fde1f3afdca6b14694c4ae51dd3ab226b805ef1753f29ac2297d0a93a681d322d35e7eca1ba242d55d9647d0fa219fbce4
|
7
|
+
data.tar.gz: c49f044e3f1799a619f0d15771a6084d9a8fb2a3ad6692cf1ab520079d124c43474a2d703e1f9ff81b2b0bd15b1f51371ae4bf0d7bf2be9607554154572b7e58
|
data/CHANGELOG.md
CHANGED
File without changes
|
data/README.md
CHANGED
@@ -1,31 +1,101 @@
|
|
1
|
-
#
|
1
|
+
# Ruby Better Input
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/better_input`. To experiment with that code, run `bin/console` for an interactive prompt.
|
3
|
+
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.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
To install this gem just use:
|
8
|
+
|
9
|
+
```shell
|
10
|
+
gem install better_input
|
11
|
+
```
|
10
12
|
|
11
|
-
Install the gem and add to the application's Gemfile by executing:
|
12
13
|
|
13
|
-
|
14
|
+
## Usage
|
15
|
+
Normally to handle user input in ruby you would do something like this:
|
16
|
+
```ruby
|
17
|
+
puts "What is your age?"
|
18
|
+
age = gets.chomp
|
19
|
+
```
|
20
|
+
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:
|
21
|
+
```ruby
|
22
|
+
puts "What is your age?"
|
14
23
|
|
15
|
-
|
24
|
+
age = nil
|
25
|
+
loop do
|
26
|
+
input = gets.chomp
|
27
|
+
if input.match?(/^\d+$/)
|
28
|
+
age = input.to_i
|
29
|
+
break
|
30
|
+
else
|
31
|
+
puts "Please enter a valid integer for your age."
|
32
|
+
end
|
33
|
+
end
|
16
34
|
|
17
|
-
|
35
|
+
puts "User response: #{age}"
|
36
|
+
```
|
37
|
+
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:
|
38
|
+
```ruby
|
39
|
+
require 'better_input'
|
18
40
|
|
19
|
-
|
41
|
+
age = Bi.input("Digite um número: ", type: "int", show_response: true)
|
42
|
+
# In a single line we asked the question and stored it in a variable,
|
43
|
+
# we made sure it was the type we wanted and we even made the answer appear!
|
44
|
+
```
|
45
|
+
```ruby
|
46
|
+
#Another examples:
|
20
47
|
|
21
|
-
|
48
|
+
noquestion = Bi.input()
|
49
|
+
retype = Bi.input(show_response: true)
|
50
|
+
onlyfloat = Bi.input(type: float)
|
22
51
|
|
23
|
-
|
52
|
+
name = Bi.input("What is your name?")
|
53
|
+
puts "Your name is #{name}"
|
54
|
+
```
|
55
|
+
Anyway, use your creativity, now receiving constant user input for CLI has become less laborious!
|
24
56
|
|
25
|
-
|
57
|
+
## Function before becoming a gem
|
58
|
+
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.
|
59
|
+
```ruby
|
60
|
+
def input(question = nil, show_response: false, type: "string")
|
61
|
+
unless question.nil?
|
62
|
+
puts question
|
63
|
+
end
|
26
64
|
|
27
|
-
|
65
|
+
loop do
|
66
|
+
var = gets.chomp
|
28
67
|
|
29
|
-
|
68
|
+
if type == "int"
|
69
|
+
begin
|
70
|
+
var = Integer(var)
|
71
|
+
rescue ArgumentError
|
72
|
+
puts "Err: Please, type a (integer) number."
|
73
|
+
next
|
74
|
+
end
|
75
|
+
elsif type == "float"
|
76
|
+
begin
|
77
|
+
var = Float(var)
|
78
|
+
rescue ArgumentError
|
79
|
+
puts "Err: Please, type a (float) number."
|
80
|
+
next
|
81
|
+
end
|
82
|
+
elsif type == "bool"
|
83
|
+
if var.downcase == "true"
|
84
|
+
var = true
|
85
|
+
elsif var.downcase == "false"
|
86
|
+
var = false
|
87
|
+
else
|
88
|
+
puts "Err: Please, type 'true' or 'false'."
|
89
|
+
next
|
90
|
+
end
|
91
|
+
else
|
92
|
+
var = var
|
93
|
+
end
|
30
94
|
|
31
|
-
|
95
|
+
if show_response
|
96
|
+
puts "User Response: #{var}"
|
97
|
+
end
|
98
|
+
return var
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
data/Rakefile
CHANGED
File without changes
|
Binary file
|
Binary file
|
data/lib/better_input/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.1.
|
1
|
+
module Bi
|
2
|
+
VERSION = "0.1.2"
|
3
3
|
end
|
data/lib/better_input.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module Bi
|
2
2
|
def self.input(question = nil, show_response: false, type: "string")
|
3
3
|
unless question.nil?
|
4
4
|
puts question
|
@@ -31,6 +31,10 @@ module BetterInput
|
|
31
31
|
puts "Err: Please, type 'true' or 'false'."
|
32
32
|
next
|
33
33
|
end
|
34
|
+
when "string"
|
35
|
+
var = String(var)
|
36
|
+
else
|
37
|
+
puts "Err: Undefined type."
|
34
38
|
end
|
35
39
|
|
36
40
|
if show_response
|
@@ -40,3 +44,4 @@ module BetterInput
|
|
40
44
|
end
|
41
45
|
end
|
42
46
|
end
|
47
|
+
|
data/sig/better_input.rbs
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,17 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: better_input
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Barros
|
7
|
+
- Barros Flavio
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
13
|
+
description: |
|
14
|
+
This is an improved input function for Ruby.
|
15
|
+
|
16
|
+
Using this gem, you can easily get user input with real-time type validation, ensuring that the data provided is as expected, among other things.
|
17
|
+
|
18
|
+
If you have any questions, check the documentation: https://github.com/barrosflavio/ruby_better_input
|
19
|
+
|
20
|
+
Features:
|
21
|
+
- Ask, receive and check input in a single line
|
22
|
+
- Support for types such as integer, float and boolean.
|
23
|
+
- Input validation with clear error messages.
|
24
|
+
- Intuitive and easy-to-use function.
|
15
25
|
email:
|
16
26
|
- flaviomarbs@gmail.com
|
17
27
|
executables: []
|
@@ -21,6 +31,8 @@ files:
|
|
21
31
|
- CHANGELOG.md
|
22
32
|
- README.md
|
23
33
|
- Rakefile
|
34
|
+
- better_input-0.1.0.gem
|
35
|
+
- better_input-0.1.1.gem
|
24
36
|
- lib/better_input.rb
|
25
37
|
- lib/better_input/version.rb
|
26
38
|
- sig/better_input.rbs
|