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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf0be13d0a2e546dcd92980dbc71b6c8956366ba
4
- data.tar.gz: ddc92b6eae61170ed3ed0d1c40c32bab3c273da1
3
+ metadata.gz: 50f1b9428e10561c1d4045b4b05b8d563bb587b4
4
+ data.tar.gz: b3b4f1554853a562ed1392ba52b389aceed4321b
5
5
  SHA512:
6
- metadata.gz: 75ee9f9ac74995be062484ff8b9f0bbf2892293647858bd8c38d48c8515641139683e12f47169d473fb411fd0694c41c3f3b916b629afd08b55a9f33d196ae99
7
- data.tar.gz: 992bb2b2d1029ec93fd94ecee760d1be9a0c60824e2c60f621f10d8aef504bdcf9e96f9cea52a36c26b1ff65ad9d92e99b679ac5d04752cd2ac926f77914b92d
6
+ metadata.gz: a34de11e4923f8087bc892962ee38848931100205bfc88899b09dd60ea3f857461f55cf322b9c3e4adc6a930e87133cf5241c3686b390214e390ebeec88d5f59
7
+ data.tar.gz: 4d13444688caf6d19a6436026ee31a389f50e9c8c32b62f65717686547be028ae6b197b516d7d93bd70cb7af68b6a1428cebaf677a451c13d48ef37657afda2a
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Rubies
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
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 Hash
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
- @ds = Hash.new
11
+ @num_right = 0
12
+ @num_wrong = 0
13
+ @playing = true
23
14
  end
24
15
 
25
- def children
26
- array = Array.new
27
- rand(1..3).times do
28
- array << Faker::Name.first_name
29
- end
30
- array
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 has_kids?
34
- rand(2) == 1
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 hash_one
38
- hash = Hash.new
39
- 10.times do
40
- name = Faker::Company.name
41
- bs = Faker::Company.bs
42
- hash[name] = bs
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 hash_two
48
- hash = Hash.new
49
- 10.times do
50
- email = Faker::Internet.email
51
- num = rand(1..1000)
52
- hash[email] = num
53
- end
54
- hash
55
- end
56
-
57
- def hash_three
58
- hash = Hash.new
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
- hash
67
+ puts
73
68
  end
74
69
 
75
- def generate
76
- @ds = [hash_one, hash_two, hash_three].sample
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
- class RandomArray < Array
81
- def initialize
82
- @ds = Array.new
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 mini_array
86
- (-1_000..1_000).sort_by { rand }.sample 3
85
+ def itsright
86
+ @num_right += 1
87
+ puts "Correct!".colorize(:green)
87
88
  end
88
89
 
89
- def nesting_array
90
- rand(1..3).times do
91
- @ds << mini_array
92
- end
93
- @ds.each do |array|
94
- array << mini_array
95
- end
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 generate
100
- depth = rand(0..3)
101
- nesting_array.flatten(depth)
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
- class Game #gamerun
106
- def data_structure
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 all_values(ds)
115
- values = Array.new
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 game
135
- puts "\e[H\e[2J"
136
- puts "
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
- ".colorize(:light_magenta)
145
- puts "================================================================".colorize(:light_magenta)
146
- puts " LEGEND ".colorize(:light_magenta)
147
- puts " NEW : get a new data structure"
148
- puts " EXIT: exit program"
149
- puts "================================================================".colorize(:light_magenta)
150
- puts "Press enter to continue . . . "
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
- gets.chomp
153
- puts "\e[H\e[2J"
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
- num_correct = 0
156
- num_wrong = 0
157
- current = data_structure
158
- playing = true
159
- while playing == true
160
- correct = false
161
- answer = all_values(current).sample
162
- while correct == false
163
- puts
164
- puts "We have some questions for you about this #{current.class.to_s.downcase}:".colorize(:light_blue)
165
- puts "current = "
166
- if current.is_a? Hash
167
- ap current, index: false
168
- elsif current.first.is_a? Array
169
- PP.pp current
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
- ap current, index: false
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.9
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-09 00:00:00.000000000 Z
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