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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bf1091645c1a560391ce297d8782434ef4cab23082ace9dd66130713ba444c8
4
- data.tar.gz: 887189e5e74a48ba84004c097964af27cd8558b8ee45c2a66dc3888e6fadf376
3
+ metadata.gz: 93cb64397d26be6f3a014af4249b2cfe7750a410c8c1c782a5aac699e53abe19
4
+ data.tar.gz: 418eaf6136a1b257e32bbbafbf18dd663b1e99445b5d814daaa4364637671eb9
5
5
  SHA512:
6
- metadata.gz: 38f8acda9933caa9d733c9b9e109fd39dd58b2ad75f4825cb38501eb642a46af66a6767fa1a4a990a2dc73ca76cb3608a309595f7eb84cc7baeccb3f8a2a2e79
7
- data.tar.gz: cd5675272a7f21a79c493375af3185221eada8c2b474c3570487885ae637e93dfcecf2ebe23870c319dc79d6b97524d8220d8934ed85176e45c3bb47af4ecfed
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
- # BetterInput
1
+ # Ruby Better Input
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
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
- If bundler is not being used to manage dependencies, install the gem by executing:
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
- TODO: Write usage instructions here
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
- ## Development
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
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+ loop do
46
+ var = gets.chomp
28
47
 
29
- ## Contributing
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/better_input.
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
@@ -1,3 +1,3 @@
1
- module BetterInput
2
- VERSION = "0.1.0"
1
+ module Bi
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/better_input.rb CHANGED
@@ -1,4 +1,4 @@
1
- module BetterInput
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - Barros FLavio
7
+ - Barros Flavio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-06 00:00:00.000000000 Z
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