rubies 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0cc6fa43f8e04b7185927a8f75630f63ca00eef
4
- data.tar.gz: a56edff98fc30aafec4de79d12a4358aae9d4bc2
3
+ metadata.gz: c7f7208605014f7baa1064b971df6b271c2e4f55
4
+ data.tar.gz: c6fe9b9c4ff3e47dcd152218cd2f9757e0e039be
5
5
  SHA512:
6
- metadata.gz: aba8cbd15fd9d11557bc51ea504883b169e95d0b05f2a708f4a3b9c6fecb88e447cf053150ba0c187b3767fe8bb2753ab3e9a84af4ec4b5b3a5f88c19ff547b9
7
- data.tar.gz: c93d2073f611f5e6032dd87e56e5a44c86f732c80f3d6f2933a9dd3637252dc764be57ccdc7e0a18f5ff285fe6d9ede76971b72a395a0d8aeaf2b5b97f66ff60
6
+ metadata.gz: 445faea496b2e14b9f2164688f8f5ba0eb7d23c8f268924200571d7cf2851689e66fafc49d7ec269b30c6f12e365ebdcd086acd9b5f137e8b29c0e0d5bb9545c
7
+ data.tar.gz: e404d80c9a31c0d2cf429d341d6044ad97505512d1f8a7c8168a88a8c9cac112de3f1da4672170165cb1bcf92ec344b44c3c49829b22848c29278f6f93be3dcb
data/README.md CHANGED
@@ -4,30 +4,15 @@ Newcomers to programming would benefit from practicing drills regularly until ba
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'rubies'
11
- ```
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
7
  $ gem install rubies
20
8
 
21
9
  ## Usage
22
10
 
23
- I'm currently working on getting this to run from the command line. For now, you can install the right dependencies: 'faker', 'colorize', 'awesome_print'. Then . . .
11
+ run `rubies` from the command line to start the drills.
24
12
 
25
- ```
26
- pry
27
- [1] pry(main)> require 'rubies'
28
- ```
13
+ type `NEW` to generate a new problem set.
29
14
 
30
- Then the gem runs.
15
+ type `exit` to exit the drills.
31
16
 
32
17
  ## Contributing
33
18
 
data/bin/rubies CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubies'
3
+ require_relative '../lib/rubies'
4
4
  @game = Game.new
5
5
  @game.game
@@ -1,3 +1,3 @@
1
1
  module Rubies
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/rubies.rb CHANGED
@@ -3,235 +3,239 @@ require 'colorize'
3
3
  require 'awesome_print'
4
4
  require 'faker'
5
5
  require 'pp'
6
-
7
- # module Rubies
8
- class Hash
9
- def deep_traverse(&block)
10
- stack = self.map { |k, v| [[k], v] }
11
- while not stack.empty?
12
- key, value = stack.pop
13
- yield(key, value)
14
- if value.is_a? Hash
15
- value.each { |k, v| stack.push [key.dup << k, v] }
16
- end
6
+ require 'pry'
7
+
8
+ class Hash
9
+ def deep_traverse(&block)
10
+ stack = self.map { |k, v| [[k], v] }
11
+ while not stack.empty?
12
+ key, value = stack.pop
13
+ yield(key, value)
14
+ if value.is_a? Hash
15
+ value.each { |k, v| stack.push [key.dup << k, v] }
17
16
  end
18
17
  end
19
18
  end
19
+ end
20
20
 
21
- class RandomHash < Hash
22
- def initialize
23
- @ds = Hash.new
24
- end
21
+ class RandomHash < Hash
22
+ def initialize
23
+ @ds = Hash.new
24
+ end
25
25
 
26
- def children
27
- array = Array.new
28
- rand(1..3).times do
29
- array << Faker::Name.first_name
30
- end
31
- array
26
+ def children
27
+ array = Array.new
28
+ rand(1..3).times do
29
+ array << Faker::Name.first_name
32
30
  end
31
+ array
32
+ end
33
33
 
34
- def has_kids?
35
- rand(2) == 1
36
- end
34
+ def has_kids?
35
+ rand(2) == 1
36
+ end
37
37
 
38
- def hash_one
39
- hash = Hash.new
40
- 10.times do
41
- name = Faker::Company.name
42
- bs = Faker::Company.bs
43
- hash[name] = bs
44
- end
45
- hash
38
+ def hash_one
39
+ hash = Hash.new
40
+ 10.times do
41
+ name = Faker::Company.name
42
+ bs = Faker::Company.bs
43
+ hash[name] = bs
46
44
  end
45
+ hash
46
+ end
47
47
 
48
- def hash_two
49
- hash = Hash.new
50
- 10.times do
51
- email = Faker::Internet.email
52
- num = rand(1..1000)
53
- hash[email] = num
54
- end
55
- hash
48
+ def hash_two
49
+ hash = Hash.new
50
+ 10.times do
51
+ email = Faker::Internet.email
52
+ num = rand(1..1000)
53
+ hash[email] = num
56
54
  end
55
+ hash
56
+ end
57
57
 
58
- def hash_three
59
- hash = Hash.new
60
- length = rand(1..5)
61
- count = 1
62
- while count <= length
63
- details = Hash.new
64
- name = Faker::Name.name
65
- phone = Faker::PhoneNumber.cell_phone
66
- company = Faker::Company.name
67
- details["phone"] = phone
68
- details["company"] = company
69
- details["children"] = children if has_kids?
70
- hash[name] = details
71
- count += 1
72
- end
73
- hash
58
+ def hash_three
59
+ hash = Hash.new
60
+ length = rand(1..5)
61
+ count = 1
62
+ while count <= length
63
+ details = Hash.new
64
+ name = Faker::Name.name
65
+ phone = Faker::PhoneNumber.cell_phone
66
+ company = Faker::Company.name
67
+ details["phone"] = phone
68
+ details["company"] = company
69
+ details["children"] = children if has_kids?
70
+ hash[name] = details
71
+ count += 1
74
72
  end
73
+ hash
74
+ end
75
75
 
76
- def generate
77
- pick = rand(1..3)
78
- case pick
79
- when 1
80
- @ds = hash_one
81
- when 2
82
- @ds = hash_two
83
- when 3
84
- @ds = hash_three
85
- end
86
- @ds
87
- end
76
+ def generate
77
+ @ds = [hash_one, hash_two, hash_three].sample
88
78
  end
79
+ end
89
80
 
90
- class RandomArray < Array
91
- def initialize
92
- @ds = Array.new
93
- end
81
+ class RandomArray < Array
82
+ def initialize
83
+ @ds = Array.new
84
+ end
94
85
 
95
- def mini_array
96
- (-1_000..1_000).sort_by { rand }.sample 3
97
- end
86
+ def mini_array
87
+ (-1_000..1_000).sort_by { rand }.sample 3
88
+ end
98
89
 
99
- def nesting_array
100
- rand(1..3).times do
101
- @ds << mini_array
102
- end
103
- @ds.each do |array|
104
- array << mini_array
105
- end
106
- @ds
90
+ def nesting_array
91
+ rand(1..3).times do
92
+ @ds << mini_array
107
93
  end
108
-
109
- def generate
110
- depth = rand(0..3)
111
- nesting_array.flatten(depth)
94
+ @ds.each do |array|
95
+ array << mini_array
112
96
  end
97
+ @ds
113
98
  end
114
99
 
115
- class Game
116
- def data_structure
117
- combo = Array.new
118
- rand(1..3).times do
119
- combo << RandomHash.new.hash_three
120
- end
121
- [RandomHash.new.generate, RandomArray.new.generate, combo].sample
100
+ def generate
101
+ depth = rand(0..3)
102
+ nesting_array.flatten(depth)
103
+ end
104
+ end
105
+
106
+ class Game #gamerun
107
+ def data_structure
108
+ combo = Array.new
109
+ rand(1..3).times do
110
+ combo << RandomHash.new.hash_three
122
111
  end
112
+ [RandomHash.new.generate, RandomArray.new.generate, combo].sample
113
+ end
123
114
 
124
- def all_values(ds)
125
- values = Array.new
126
- if ds.is_a? Hash
127
- values = ds.values
128
- elsif ds.flatten.first.is_a? Fixnum
129
- values = ds.flatten
130
- else
131
- ds.each do |hash|
132
- hash.deep_traverse{ |path, value| values << value }
133
- end
134
- values.each do |value|
135
- if value.is_a? Array
136
- value.each { |element| values << element }
137
- values.delete(value)
138
- end
115
+ def all_values(ds)
116
+ values = Array.new
117
+ if ds.is_a? Hash
118
+ values = ds.values
119
+ elsif ds.flatten.first.is_a? Fixnum
120
+ values = ds.flatten
121
+ else
122
+ ds.each do |hash|
123
+ hash.deep_traverse{ |path, value| values << value }
124
+ end
125
+ values.each do |value|
126
+ if value.is_a? Array
127
+ value.each { |element| values << element }
128
+ values.delete(value)
139
129
  end
140
130
  end
141
- values
142
131
  end
132
+ values
133
+ end
143
134
 
144
- def game
145
- puts "\e[H\e[2J"
146
- puts "
147
- .______ __ __ .______ __ _______ _______.
148
- | _ \\ | | | | | _ \\ | | | ____| / |
149
- | |_) | | | | | | |_) | | | | |__ | (----`
150
- | / | | | | | _ < | | | __| \\ \\
151
- | |\\ \\----.| `--' | | |_) | | | | |____.----) |
152
- | _| `._____| \\______/ |______/ |__| |_______|_______/
153
-
154
- ".colorize(:light_magenta)
155
- puts "==============================".colorize(:light_magenta)
156
- puts " LEGEND ".colorize(:light_magenta)
157
- puts "NEW : get a new data structure"
158
- # puts "RUN : executes any inputted code"
159
- # puts "EXIT: exit program"
160
- puts "==============================".colorize(:light_magenta)
161
- puts "Press enter to continue . . . "
162
-
163
- gets.chomp
164
- puts "\e[H\e[2J"
165
-
166
- num_correct = 0
167
- num_wrong = 0
168
- current = data_structure
169
- playing = true
170
- while playing == true
171
- correct = false
172
- answer = all_values(current).sample
173
- while correct == false
135
+ def game
136
+ puts "\e[H\e[2J"
137
+ puts "
138
+ .______ __ __ .______ __ _______ _______.
139
+ | _ \\ | | | | | _ \\ | | | ____| / |
140
+ | |_) | | | | | | |_) | | | | |__ | (----`
141
+ | / | | | | | _ < | | | __| \\ \\
142
+ | |\\ \\----.| `--' | | |_) | | | | |____.----) |
143
+ | _| `._____| \\______/ |______/ |__| |_______|_______/
144
+
145
+ ".colorize(:light_magenta)
146
+ puts "================================================================".colorize(:light_magenta)
147
+ puts " LEGEND ".colorize(:light_magenta)
148
+ puts " NEW : get a new data structure"
149
+ puts " EXIT: exit program"
150
+ puts "================================================================".colorize(:light_magenta)
151
+ puts "Press enter to continue . . . "
152
+
153
+ gets.chomp
154
+ puts "\e[H\e[2J"
155
+
156
+ num_correct = 0
157
+ num_wrong = 0
158
+ current = data_structure
159
+ playing = true
160
+ while playing == true
161
+ correct = false
162
+ answer = all_values(current).sample
163
+ while correct == false
164
+ puts
165
+ puts "We have some questions for you about this #{current.class.to_s.downcase}:".colorize(:light_blue)
166
+ puts "current = "
167
+ if current.is_a? Hash
168
+ ap current, index: false
169
+ elsif current.first.is_a? Array
170
+ PP.pp current
171
+ elsif current.first.is_a? Fixnum
172
+ PP.pp current
173
+ else
174
+ ap current, index: false
175
+ end
176
+ puts
177
+ puts "Write some ruby code to find the following value (or enter NEW for a new challenge): ".colorize(:light_blue)
178
+ puts answer.to_s
179
+ puts
180
+ print "[1] ruby_drills(main)> "
181
+ input = gets.chomp
182
+ input.gsub("\"", "\'")
183
+ # $/ = "RUN"
184
+ # input = STDIN.gets
185
+ # input = input.gsub("\n", ";").gsub("RUN", "")
186
+ if input == "NEW"
187
+ puts "\e[H\e[2J"
188
+ current = data_structure
189
+ break
190
+ elsif input == "EXIT"
174
191
  puts
175
- puts "We have some questions for you about this #{current.class.to_s.downcase}:".colorize(:light_blue)
176
- puts "current = "
177
- if current.is_a? Hash
178
- ap current, index: false
179
- elsif current.first.is_a? Array
180
- PP.pp current
181
- elsif current.first.is_a? Fixnum
182
- PP.pp current
183
- else
184
- ap current, index: false
185
- end
192
+ puts "==============================".colorize(:light_yellow)
193
+ puts "Number correct this session: ".colorize(:green) + num_correct.to_s
194
+ puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
195
+ puts "==============================".colorize(:light_yellow)
186
196
  puts
187
- puts "Write some ruby code to find the following value (or enter NEW for a new challenge): ".colorize(:light_blue)
188
- puts answer.to_s
189
197
  puts
190
- print "[1] ruby_drills(main)> "
191
- input = gets.chomp
192
- input.gsub("\"", "\'")
193
- # $/ = "RUN"
194
- # input = STDIN.gets
195
- # input = input.gsub("\n", ";").gsub("RUN", "")
196
- if input == "NEW"
197
- puts "\e[H\e[2J"
198
- current = data_structure
199
- break
198
+ puts "Thanks for using rubies!".colorize(:green)
199
+ exit
200
+ else
201
+ begin
202
+ routine = lambda { eval(input) }
203
+ output = routine.call
204
+ rescue NoMethodError => e
205
+ puts
206
+ puts "Sorry, that code resulted in an error:".colorize(:light_red)
207
+ puts "#{e}".colorize(:red)
208
+ rescue Exception => e
209
+ puts
210
+ puts "Sorry, that code resulted in an error:".colorize(:light_red)
211
+ puts "#{e}".colorize(:red)
200
212
  else
201
- begin
202
- output = eval(input)
203
- rescue StandardError => e
213
+ if answer != output
214
+ num_wrong += 1
215
+ puts "=> " + output.to_s
216
+ puts
217
+ puts "Sorry, that code is incorrect. ".colorize(:light_red)
204
218
  puts
205
- puts "Sorry, that code resulted in an error:".colorize(:light_red)
206
- puts "#{e}".colorize(:red)
219
+ puts "The right answer is . . . ".colorize(:light_red)
220
+ puts answer.to_s
221
+ puts "Try again!".colorize(:light_red)
207
222
  else
208
- if answer != output
209
- num_wrong += 1
210
- puts "=> " + output.to_s
211
- puts
212
- puts "Sorry, that code is incorrect. ".colorize(:light_red)
213
- puts
214
- puts "The right answer is . . . ".colorize(:light_red)
215
- puts answer.to_s
216
- puts "Try again!".colorize(:light_red)
217
- else
218
- num_correct += 1
219
- puts "=> " + output.to_s
220
- puts
221
- puts "Correct!".colorize(:green)
222
- correct = true
223
- end
223
+ num_correct += 1
224
+ puts "=> " + output.to_s
225
+ puts
226
+ puts "Correct!".colorize(:green)
227
+ correct = true
224
228
  end
225
229
  end
226
- puts "==============================".colorize(:light_yellow)
227
- puts "Number correct this session: ".colorize(:green) + num_correct.to_s
228
- puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
229
- puts "==============================".colorize(:light_yellow)
230
- puts "Press enter to continue . . . "
231
- gets.chomp
232
- puts "\e[H\e[2J"
233
230
  end
231
+ puts "==============================".colorize(:light_yellow)
232
+ puts "Number correct this session: ".colorize(:green) + num_correct.to_s
233
+ puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
234
+ puts "==============================".colorize(:light_yellow)
235
+ puts "Press enter to continue . . . "
236
+ gets.chomp
237
+ puts "\e[H\e[2J"
234
238
  end
235
239
  end
236
240
  end
237
- # end
241
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vikram Ramakrishnan