better_input 0.1.0 → 0.1.1
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 +67 -17
- data/Rakefile +0 -0
- data/better_input-0.1.0.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 +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93cb64397d26be6f3a014af4249b2cfe7750a410c8c1c782a5aac699e53abe19
|
4
|
+
data.tar.gz: 418eaf6136a1b257e32bbbafbf18dd663b1e99445b5d814daaa4364637671eb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29240b360ab2846d3af8b2e2985842ef5959d31a582574b351593ef19de2b260b9b1da21b0cffe910b51f80347a8b5e27580163a639ca44ea875285593821a6f
|
7
|
+
data.tar.gz: 445e0d8b6e37660546692f5e57bad7c2a08e26d7bd4fe5acc4ab587a5a9ec11ea1b8883341b13a35d4e1371956a9ad43ddf4dcef777904f939fda266527ca800
|
data/CHANGELOG.md
CHANGED
File without changes
|
data/README.md
CHANGED
@@ -1,31 +1,81 @@
|
|
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
|
-
|
10
|
-
|
11
|
-
Install the gem and add to the application's Gemfile by executing:
|
12
|
-
|
13
|
-
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
7
|
+
To install this gem just use:
|
14
8
|
|
15
|
-
|
9
|
+
$ gem install better_input
|
16
10
|
|
17
|
-
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
18
11
|
|
19
12
|
## Usage
|
13
|
+
Normally to handle user input in ruby you would do something like this:
|
14
|
+
```ruby
|
15
|
+
puts "What is your age?"
|
16
|
+
age = gets.chomp
|
17
|
+
```
|
18
|
+
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:
|
19
|
+
```ruby
|
20
|
+
puts "What is your age?"
|
20
21
|
|
21
|
-
|
22
|
+
age = nil
|
23
|
+
loop do
|
24
|
+
input = gets.chomp
|
25
|
+
if input.match?(/^\d+$/)
|
26
|
+
age = input.to_i
|
27
|
+
break
|
28
|
+
else
|
29
|
+
puts "Please enter a valid integer for your age."
|
30
|
+
end
|
31
|
+
end
|
22
32
|
|
23
|
-
|
33
|
+
puts "Your age is #{age}."
|
34
|
+
```
|
35
|
+
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:
|
24
36
|
|
25
|
-
|
37
|
+
## Function before becoming a gem
|
38
|
+
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.
|
39
|
+
```ruby
|
40
|
+
def input(question = nil, show_response: false, type: "string")
|
41
|
+
unless question.nil?
|
42
|
+
puts question
|
43
|
+
end
|
26
44
|
|
27
|
-
|
45
|
+
loop do
|
46
|
+
var = gets.chomp
|
28
47
|
|
29
|
-
|
48
|
+
if type == "int"
|
49
|
+
begin
|
50
|
+
var = Integer(var)
|
51
|
+
rescue ArgumentError
|
52
|
+
puts "Err: Please, type a (integer) number."
|
53
|
+
next
|
54
|
+
end
|
55
|
+
elsif type == "float"
|
56
|
+
begin
|
57
|
+
var = Float(var)
|
58
|
+
rescue ArgumentError
|
59
|
+
puts "Err: Please, type a (float) number."
|
60
|
+
next
|
61
|
+
end
|
62
|
+
elsif type == "bool"
|
63
|
+
if var.downcase == "true"
|
64
|
+
var = true
|
65
|
+
elsif var.downcase == "false"
|
66
|
+
var = false
|
67
|
+
else
|
68
|
+
puts "Err: Please, type 'true' or 'false'."
|
69
|
+
next
|
70
|
+
end
|
71
|
+
else
|
72
|
+
var = var
|
73
|
+
end
|
30
74
|
|
31
|
-
|
75
|
+
if show_response
|
76
|
+
puts "User Response: #{var}"
|
77
|
+
end
|
78
|
+
return var
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
data/Rakefile
CHANGED
File without changes
|
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.1"
|
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,14 +1,14 @@
|
|
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.1
|
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
13
|
description: When working with CLI I found myself doing things that could be summarized
|
14
14
|
as this function, so I did it!
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- CHANGELOG.md
|
22
22
|
- README.md
|
23
23
|
- Rakefile
|
24
|
+
- better_input-0.1.0.gem
|
24
25
|
- lib/better_input.rb
|
25
26
|
- lib/better_input/version.rb
|
26
27
|
- sig/better_input.rbs
|