rubies 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/random_array.rb +24 -0
- data/lib/random_data_structure.rb +42 -0
- data/lib/random_hash.rb +72 -0
- data/lib/rubies/version.rb +1 -1
- data/lib/rubies.rb +134 -200
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50f1b9428e10561c1d4045b4b05b8d563bb587b4
|
4
|
+
data.tar.gz: b3b4f1554853a562ed1392ba52b389aceed4321b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a34de11e4923f8087bc892962ee38848931100205bfc88899b09dd60ea3f857461f55cf322b9c3e4adc6a930e87133cf5241c3686b390214e390ebeec88d5f59
|
7
|
+
data.tar.gz: 4d13444688caf6d19a6436026ee31a389f50e9c8c32b62f65717686547be028ae6b197b516d7d93bd70cb7af68b6a1428cebaf677a451c13d48ef37657afda2a
|
data/lib/random_array.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class RandomArray < Array
|
2
|
+
def initialize
|
3
|
+
@ds = Array.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def mini_array
|
7
|
+
(-1_000..1_000).sort_by { rand }.sample 3
|
8
|
+
end
|
9
|
+
|
10
|
+
def nesting_array
|
11
|
+
rand(1..3).times do
|
12
|
+
@ds << mini_array
|
13
|
+
end
|
14
|
+
@ds.each do |array|
|
15
|
+
array << mini_array
|
16
|
+
end
|
17
|
+
@ds
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate
|
21
|
+
depth = rand(0..3)
|
22
|
+
nesting_array.flatten(depth)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'random_array'
|
2
|
+
require_relative 'random_hash'
|
3
|
+
require 'faker'
|
4
|
+
|
5
|
+
class RandomDataStructure
|
6
|
+
attr_reader :data_structure
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@data_structure = Array.new
|
10
|
+
@all_values = Array.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate
|
14
|
+
combo = Array.new
|
15
|
+
rand(1..3).times do
|
16
|
+
combo << RandomHash.new.hash_three
|
17
|
+
end
|
18
|
+
random_hash = RandomHash.new.generate
|
19
|
+
random_array = RandomArray.new.generate
|
20
|
+
@data_structure = [combo, random_hash, random_array].sample
|
21
|
+
end
|
22
|
+
|
23
|
+
def all_values
|
24
|
+
values = Array.new
|
25
|
+
if @data_structure.is_a? Hash
|
26
|
+
values = @data_structure.values
|
27
|
+
elsif @data_structure.flatten.first.is_a? Fixnum
|
28
|
+
values = @data_structure.flatten
|
29
|
+
else
|
30
|
+
@data_structure.each do |hash|
|
31
|
+
hash.deep_traverse { |_path, value| values << value }
|
32
|
+
end
|
33
|
+
values.each do |value|
|
34
|
+
if value.is_a? Array
|
35
|
+
value.each { |element| values << element }
|
36
|
+
values.delete(value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
values
|
41
|
+
end
|
42
|
+
end
|
data/lib/random_hash.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
class Hash
|
2
|
+
def deep_traverse(&block)
|
3
|
+
stack = self.map { |k, v| [[k], v] }
|
4
|
+
while not stack.empty?
|
5
|
+
key, value = stack.pop
|
6
|
+
yield(key, value)
|
7
|
+
if value.is_a? Hash
|
8
|
+
value.each { |k, v| stack.push [key.dup << k, v] }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class RandomHash < Hash
|
15
|
+
def initialize
|
16
|
+
@ds = Hash.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def children
|
20
|
+
array = Array.new
|
21
|
+
rand(1..3).times do
|
22
|
+
array << Faker::Name.first_name
|
23
|
+
end
|
24
|
+
array
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_kids?
|
28
|
+
rand(2) == 1
|
29
|
+
end
|
30
|
+
|
31
|
+
def hash_one
|
32
|
+
hash = Hash.new
|
33
|
+
10.times do
|
34
|
+
name = Faker::Company.name
|
35
|
+
bs = Faker::Company.bs
|
36
|
+
hash[name] = bs
|
37
|
+
end
|
38
|
+
hash
|
39
|
+
end
|
40
|
+
|
41
|
+
def hash_two
|
42
|
+
hash = Hash.new
|
43
|
+
10.times do
|
44
|
+
email = Faker::Internet.email
|
45
|
+
num = rand(1..1000)
|
46
|
+
hash[email] = num
|
47
|
+
end
|
48
|
+
hash
|
49
|
+
end
|
50
|
+
|
51
|
+
def hash_three
|
52
|
+
hash = Hash.new
|
53
|
+
length = rand(1..5)
|
54
|
+
count = 1
|
55
|
+
while count <= length
|
56
|
+
details = Hash.new
|
57
|
+
name = Faker::Name.name
|
58
|
+
phone = Faker::PhoneNumber.cell_phone
|
59
|
+
company = Faker::Company.name
|
60
|
+
details["phone"] = phone
|
61
|
+
details["company"] = company
|
62
|
+
details["children"] = children if has_kids?
|
63
|
+
hash[name] = details
|
64
|
+
count += 1
|
65
|
+
end
|
66
|
+
hash
|
67
|
+
end
|
68
|
+
|
69
|
+
def generate
|
70
|
+
@ds = [hash_one, hash_two, hash_three].sample
|
71
|
+
end
|
72
|
+
end
|
data/lib/rubies/version.rb
CHANGED
data/lib/rubies.rb
CHANGED
@@ -1,240 +1,174 @@
|
|
1
1
|
# require "rubies/version"
|
2
2
|
require 'colorize'
|
3
3
|
require 'awesome_print'
|
4
|
-
require 'faker'
|
5
4
|
require 'pp'
|
5
|
+
require_relative 'random_array'
|
6
|
+
require_relative 'random_hash'
|
7
|
+
require_relative 'random_data_structure'
|
6
8
|
|
7
|
-
class
|
8
|
-
def deep_traverse(&block)
|
9
|
-
stack = self.map { |k, v| [[k], v] }
|
10
|
-
while not stack.empty?
|
11
|
-
key, value = stack.pop
|
12
|
-
yield(key, value)
|
13
|
-
if value.is_a? Hash
|
14
|
-
value.each { |k, v| stack.push [key.dup << k, v] }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class RandomHash < Hash
|
9
|
+
class Game
|
21
10
|
def initialize
|
22
|
-
@
|
11
|
+
@num_right = 0
|
12
|
+
@num_wrong = 0
|
13
|
+
@playing = true
|
23
14
|
end
|
24
15
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
16
|
+
def display_splash
|
17
|
+
puts "\e[H\e[2J"
|
18
|
+
puts "
|
19
|
+
.______ __ __ .______ __ _______ _______.
|
20
|
+
| _ \\ | | | | | _ \\ | | | ____| / |
|
21
|
+
| |_) | | | | | | |_) | | | | |__ | (----`
|
22
|
+
| / | | | | | _ < | | | __| \\ \\
|
23
|
+
| |\\ \\----.| `--' | | |_) | | | | |____.----) |
|
24
|
+
| _| `._____| \\______/ |______/ |__| |_______|_______/
|
25
|
+
|
26
|
+
".colorize(:light_magenta)
|
27
|
+
puts "
|
28
|
+
================================================================
|
29
|
+
LEGEND
|
30
|
+
NEW : get a new data structure
|
31
|
+
EXIT: exit program
|
32
|
+
================================================================
|
33
|
+
".colorize(:light_magenta)
|
34
|
+
puts "Press enter to continue . . . "
|
35
|
+
|
36
|
+
gets.chomp
|
37
|
+
puts "\e[H\e[2J"
|
31
38
|
end
|
32
39
|
|
33
|
-
def
|
34
|
-
|
40
|
+
def continuer
|
41
|
+
puts "Press enter to continue . . . "
|
42
|
+
gets.chomp
|
43
|
+
puts "\e[H\e[2J"
|
35
44
|
end
|
36
45
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
hash
|
46
|
+
def scoreboard(num_right, num_wrong)
|
47
|
+
puts
|
48
|
+
puts "==============================".colorize(:light_yellow)
|
49
|
+
puts "Number correct this session: ".colorize(:green) + num_right.to_s
|
50
|
+
puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
|
51
|
+
puts "==============================".colorize(:light_yellow)
|
45
52
|
end
|
46
53
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
length = rand(1..5)
|
60
|
-
count = 1
|
61
|
-
while count <= length
|
62
|
-
details = Hash.new
|
63
|
-
name = Faker::Name.name
|
64
|
-
phone = Faker::PhoneNumber.cell_phone
|
65
|
-
company = Faker::Company.name
|
66
|
-
details["phone"] = phone
|
67
|
-
details["company"] = company
|
68
|
-
details["children"] = children if has_kids?
|
69
|
-
hash[name] = details
|
70
|
-
count += 1
|
54
|
+
def questioner(current)
|
55
|
+
puts
|
56
|
+
puts "We have some questions for you about this #{current.class.to_s.downcase}:".colorize(:light_blue)
|
57
|
+
puts "current = "
|
58
|
+
if current.is_a? Hash
|
59
|
+
ap current, index: false
|
60
|
+
elsif current.first.is_a? Array
|
61
|
+
PP.pp current
|
62
|
+
elsif current.first.is_a? Fixnum
|
63
|
+
PP.pp current
|
64
|
+
else
|
65
|
+
ap current, index: false
|
71
66
|
end
|
72
|
-
|
67
|
+
puts
|
73
68
|
end
|
74
69
|
|
75
|
-
def
|
76
|
-
|
70
|
+
def eprinter(error)
|
71
|
+
puts
|
72
|
+
puts "Sorry, that code resulted in an error:".colorize(:light_red)
|
73
|
+
puts "#{error}".colorize(:red)
|
77
74
|
end
|
78
|
-
end
|
79
75
|
|
80
|
-
|
81
|
-
|
82
|
-
|
76
|
+
def itswrong(answer)
|
77
|
+
@num_wrong += 1
|
78
|
+
puts "Sorry, that code is incorrect. ".colorize(:light_red)
|
79
|
+
puts
|
80
|
+
puts "The right answer is . . . ".colorize(:light_red)
|
81
|
+
puts answer.to_s
|
82
|
+
puts "Try again!".colorize(:light_red)
|
83
83
|
end
|
84
84
|
|
85
|
-
def
|
86
|
-
|
85
|
+
def itsright
|
86
|
+
@num_right += 1
|
87
|
+
puts "Correct!".colorize(:green)
|
87
88
|
end
|
88
89
|
|
89
|
-
def
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
@ds
|
90
|
+
def prompter(answer)
|
91
|
+
print "Write ruby code to find the following value".colorize(:light_blue)
|
92
|
+
print " (or enter ".colorize(:light_blue) + 'NEW'.colorize(:green)
|
93
|
+
puts " for a new challenge): ".colorize(:light_blue)
|
94
|
+
puts answer.to_s
|
95
|
+
puts
|
96
|
+
print "[1] rubies(main)> "
|
97
97
|
end
|
98
98
|
|
99
|
-
def
|
100
|
-
|
101
|
-
|
99
|
+
def byebye
|
100
|
+
puts
|
101
|
+
puts "Thanks for using ".colorize(:green) + "rubies!".colorize(:light_red)
|
102
|
+
display_score
|
102
103
|
end
|
103
|
-
end
|
104
104
|
|
105
|
-
|
106
|
-
|
107
|
-
combo = Array.new
|
108
|
-
rand(1..3).times do
|
109
|
-
combo << RandomHash.new.hash_three
|
110
|
-
end
|
111
|
-
[RandomHash.new.generate, RandomArray.new.generate, combo].sample
|
105
|
+
def display_score
|
106
|
+
scoreboard(@num_right, @num_wrong)
|
112
107
|
end
|
113
108
|
|
114
|
-
def
|
115
|
-
|
116
|
-
if ds.is_a? Hash
|
117
|
-
values = ds.values
|
118
|
-
elsif ds.flatten.first.is_a? Fixnum
|
119
|
-
values = ds.flatten
|
120
|
-
else
|
121
|
-
ds.each do |hash|
|
122
|
-
hash.deep_traverse{ |path, value| values << value }
|
123
|
-
end
|
124
|
-
values.each do |value|
|
125
|
-
if value.is_a? Array
|
126
|
-
value.each { |element| values << element }
|
127
|
-
values.delete(value)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
values
|
109
|
+
def clear_screen
|
110
|
+
puts "\e[H\e[2J"
|
132
111
|
end
|
133
112
|
|
134
|
-
def
|
135
|
-
|
136
|
-
|
137
|
-
.
|
138
|
-
|
139
|
-
| |_) | | | | | | |_) | | | | |__ | (----`
|
140
|
-
| / | | | | | _ < | | | __| \\ \\
|
141
|
-
| |\\ \\----.| `--' | | |_) | | | | |____.----) |
|
142
|
-
| _| `._____| \\______/ |______/ |__| |_______|_______/
|
113
|
+
def prompt(data_structure, target)
|
114
|
+
questioner(data_structure)
|
115
|
+
prompter(target)
|
116
|
+
gets.chomp.gsub("\"", "\'")
|
117
|
+
end
|
143
118
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
119
|
+
def check_answer(current, input, target)
|
120
|
+
begin
|
121
|
+
routine = lambda { eval(input) }
|
122
|
+
output = routine.call
|
123
|
+
puts "=> #{output}"
|
124
|
+
puts
|
125
|
+
output == target
|
126
|
+
rescue Exception => e
|
127
|
+
eprinter(e)
|
128
|
+
false
|
129
|
+
end
|
130
|
+
end
|
151
131
|
|
152
|
-
|
153
|
-
|
132
|
+
def generate_data_structure
|
133
|
+
rds = RandomDataStructure.new
|
134
|
+
current = rds.generate
|
135
|
+
target = rds.all_values.sample
|
136
|
+
[current, target]
|
137
|
+
end
|
154
138
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
elsif current.first.is_a? Fixnum
|
171
|
-
PP.pp current
|
139
|
+
def play_round # new, exit or check if right/wrong
|
140
|
+
clear_screen
|
141
|
+
correct = false
|
142
|
+
current, target = generate_data_structure
|
143
|
+
until correct
|
144
|
+
input = prompt(current, target)
|
145
|
+
if input == "NEW" || input == "new"
|
146
|
+
return
|
147
|
+
elsif input == "EXIT" || input == "exit"
|
148
|
+
@playing = false
|
149
|
+
return
|
150
|
+
else
|
151
|
+
if check_answer(current, input, target)
|
152
|
+
itsright
|
153
|
+
correct = true
|
172
154
|
else
|
173
|
-
|
155
|
+
itswrong(target)
|
174
156
|
end
|
175
|
-
puts
|
176
|
-
puts "Write some ruby code to find the following value (or enter NEW for a new challenge): ".colorize(:light_blue)
|
177
|
-
puts answer.to_s
|
178
|
-
puts
|
179
|
-
print "[1] ruby_drills(main)> "
|
180
|
-
input = gets.chomp
|
181
|
-
input.gsub("\"", "\'")
|
182
|
-
# $/ = "RUN"
|
183
|
-
# input = STDIN.gets
|
184
|
-
# input = input.gsub("\n", ";").gsub("RUN", "")
|
185
|
-
if input == "NEW"
|
186
|
-
puts "\e[H\e[2J"
|
187
|
-
current = data_structure
|
188
|
-
break
|
189
|
-
elsif input == "EXIT"
|
190
|
-
puts
|
191
|
-
puts "==============================".colorize(:light_yellow)
|
192
|
-
puts "Number correct this session: ".colorize(:green) + num_correct.to_s
|
193
|
-
puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
|
194
|
-
puts "==============================".colorize(:light_yellow)
|
195
|
-
puts
|
196
|
-
puts
|
197
|
-
puts "Thanks for using rubies!".colorize(:green)
|
198
|
-
exit
|
199
|
-
else
|
200
|
-
begin
|
201
|
-
routine = lambda { eval(input) }
|
202
|
-
output = routine.call
|
203
|
-
rescue NoMethodError => e
|
204
|
-
puts
|
205
|
-
puts "Sorry, that code resulted in an error:".colorize(:light_red)
|
206
|
-
puts "#{e}".colorize(:red)
|
207
|
-
rescue Exception => e
|
208
|
-
puts
|
209
|
-
puts "Sorry, that code resulted in an error:".colorize(:light_red)
|
210
|
-
puts "#{e}".colorize(:red)
|
211
|
-
else
|
212
|
-
if answer != output
|
213
|
-
num_wrong += 1
|
214
|
-
puts "=> " + output.to_s
|
215
|
-
puts
|
216
|
-
puts "Sorry, that code is incorrect. ".colorize(:light_red)
|
217
|
-
puts
|
218
|
-
puts "The right answer is . . . ".colorize(:light_red)
|
219
|
-
puts answer.to_s
|
220
|
-
puts "Try again!".colorize(:light_red)
|
221
|
-
else
|
222
|
-
num_correct += 1
|
223
|
-
puts "=> " + output.to_s
|
224
|
-
puts
|
225
|
-
puts "Correct!".colorize(:green)
|
226
|
-
correct = true
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
230
|
-
puts "==============================".colorize(:light_yellow)
|
231
|
-
puts "Number correct this session: ".colorize(:green) + num_correct.to_s
|
232
|
-
puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
|
233
|
-
puts "==============================".colorize(:light_yellow)
|
234
|
-
puts "Press enter to continue . . . "
|
235
|
-
gets.chomp
|
236
|
-
puts "\e[H\e[2J"
|
237
157
|
end
|
158
|
+
scoreboard(@num_right, @num_wrong)
|
159
|
+
continuer
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def gameover?
|
164
|
+
!@playing
|
165
|
+
end
|
166
|
+
|
167
|
+
def game
|
168
|
+
display_splash
|
169
|
+
until gameover?
|
170
|
+
play_round
|
238
171
|
end
|
172
|
+
byebye
|
239
173
|
end
|
240
174
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubies
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vikram Ramakrishnan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faker
|
@@ -97,6 +97,9 @@ files:
|
|
97
97
|
- README.md
|
98
98
|
- Rakefile
|
99
99
|
- bin/rubies
|
100
|
+
- lib/random_array.rb
|
101
|
+
- lib/random_data_structure.rb
|
102
|
+
- lib/random_hash.rb
|
100
103
|
- lib/rubies.rb
|
101
104
|
- lib/rubies/version.rb
|
102
105
|
- rubies.gemspec
|