rubies 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -18
- data/bin/rubies +1 -1
- data/lib/rubies/version.rb +1 -1
- data/lib/rubies.rb +196 -192
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7f7208605014f7baa1064b971df6b271c2e4f55
|
4
|
+
data.tar.gz: c6fe9b9c4ff3e47dcd152218cd2f9757e0e039be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
15
|
+
type `exit` to exit the drills.
|
31
16
|
|
32
17
|
## Contributing
|
33
18
|
|
data/bin/rubies
CHANGED
data/lib/rubies/version.rb
CHANGED
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
class RandomHash < Hash
|
22
|
+
def initialize
|
23
|
+
@ds = Hash.new
|
24
|
+
end
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
34
|
+
def has_kids?
|
35
|
+
rand(2) == 1
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
77
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
81
|
+
class RandomArray < Array
|
82
|
+
def initialize
|
83
|
+
@ds = Array.new
|
84
|
+
end
|
94
85
|
|
95
|
-
|
96
|
-
|
97
|
-
|
86
|
+
def mini_array
|
87
|
+
(-1_000..1_000).sort_by { rand }.sample 3
|
88
|
+
end
|
98
89
|
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
-
|
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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
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 "
|
176
|
-
puts "
|
177
|
-
|
178
|
-
|
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
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
puts
|
198
|
-
|
199
|
-
|
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
|
-
|
202
|
-
|
203
|
-
|
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 "
|
206
|
-
puts
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
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
|
-
|
241
|
+
end
|