rubies 0.1.0 → 0.1.1

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: 50f1b9428e10561c1d4045b4b05b8d563bb587b4
4
- data.tar.gz: b3b4f1554853a562ed1392ba52b389aceed4321b
3
+ metadata.gz: fc7611cc4a2b2011b02cf20cdfb8f9bc44384339
4
+ data.tar.gz: 9e2513c2ab74b6727deabc4d59804af0d41d8209
5
5
  SHA512:
6
- metadata.gz: a34de11e4923f8087bc892962ee38848931100205bfc88899b09dd60ea3f857461f55cf322b9c3e4adc6a930e87133cf5241c3686b390214e390ebeec88d5f59
7
- data.tar.gz: 4d13444688caf6d19a6436026ee31a389f50e9c8c32b62f65717686547be028ae6b197b516d7d93bd70cb7af68b6a1428cebaf677a451c13d48ef37657afda2a
6
+ metadata.gz: 520b6a417ef12a64d3831f236937821c305ebda7c5a850453a10544a184f5b521784ac845163beaad5747f48c1800d7c9454aa25abb23ae38c50428a84494e78
7
+ data.tar.gz: 46a0bf18cb3e6b69346f3f4b82c00b652777fca2eeb54cb680adc2cade7f48d03b4d032d7aea4e02807e1ed0ac670391c1a26f705a9210aa3c2a8775ff0d79a7
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0-p451
data/README.md CHANGED
@@ -8,11 +8,44 @@ Newcomers to programming would benefit from practicing drills regularly until ba
8
8
 
9
9
  ## Usage
10
10
 
11
- run `rubies` from the command line to start the drills.
11
+ ```
12
+ rubies
13
+ ```
12
14
 
13
- type `NEW` to generate a new problem set.
15
+ After typing `rubies` into your terminal, you will see the Splash screen:
14
16
 
15
- type `EXIT` to exit the drills.
17
+ ![alt text](http://i.imgur.com/PGvyVEC.png)
18
+
19
+ After starting, you will see randomly generated data structures for you to work through:
20
+
21
+ ![alt text](http://i.imgur.com/zCHl7Sq.png)
22
+
23
+ Here, you are being asked to find `865` in the array called `current`, which is located in the 3rd position (or index of `2`). The answer would be the following:
24
+
25
+ ```
26
+ [1] rubies(main)> current[2]
27
+ => 865
28
+ ```
29
+
30
+ ![alt text](http://i.imgur.com/MYH1ynW.png)
31
+
32
+ Here, you are given a hash called `current` and are being asked to find the right command for `engineer collaborative schemas`. The key associated with that value is `Bergnaum-Pouros`, so the answer would be the following:
33
+
34
+ ```
35
+ [1] rubies(main)> current["Bergnaum-Pouros"]
36
+ => enable collaborative schemas
37
+ ```
38
+
39
+ ![alt text](http://i.imgur.com/DIjspdO.png)
40
+
41
+ Here, you are given an array with a hash in it, or a compound data structure, and are being asked to find `(106) 777-4274`. This is how you could go about it:
42
+
43
+ ```
44
+ [1] rubies(main)> current[0]["Mikel Herman"]["phone"]
45
+ => (106) 777-4274
46
+ ```
47
+
48
+ Type `new` to get another data structure or `exit` to quit and see your final score. Try to do 15-20 minutes every day and you will be in great shape!
16
49
 
17
50
  ## Contributing
18
51
 
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.pattern = 'spec/*/*_spec.rb'
6
+ end
2
7
 
data/bin/rubies CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require_relative '../lib/rubies'
4
- @game = Game.new
4
+ @game = Rubies::Game.new
5
5
  @game.game
@@ -0,0 +1,180 @@
1
+ module Rubies
2
+ class Game
3
+ def initialize(opts={})
4
+ @num_right = 0
5
+ @num_wrong = 0
6
+ @playing = true
7
+ @in = opts.fetch(:in, $stdin)
8
+ @out = opts.fetch(:out, $stdout)
9
+ end
10
+
11
+ def puts(message='')
12
+ @out.puts message
13
+ end
14
+
15
+ def gets
16
+ @in.gets
17
+ end
18
+
19
+ def display_splash
20
+ puts "\e[H\e[2J"
21
+ puts "
22
+ .______ __ __ .______ __ _______ _______.
23
+ | _ \\ | | | | | _ \\ | | | ____| / |
24
+ | |_) | | | | | | |_) | | | | |__ | (----`
25
+ | / | | | | | _ < | | | __| \\ \\
26
+ | |\\ \\----.| `--' | | |_) | | | | |____.----) |
27
+ | _| `._____| \\______/ |______/ |__| |_______|_______/
28
+
29
+ ".colorize(:light_magenta)
30
+ puts "
31
+ ================================================================
32
+ LEGEND
33
+ NEW : get a new data structure
34
+ EXIT: exit program
35
+ ================================================================
36
+ ".colorize(:light_magenta)
37
+ puts "Press enter to continue . . . "
38
+
39
+ gets.chomp
40
+ puts "\e[H\e[2J"
41
+ end
42
+
43
+ def continuer
44
+ puts "Press enter to continue . . . "
45
+ gets.chomp
46
+ puts "\e[H\e[2J"
47
+ end
48
+
49
+ def scoreboard(num_right, num_wrong)
50
+ puts
51
+ puts "==============================".colorize(:light_yellow)
52
+ puts "Number correct this session: ".colorize(:green) + num_right.to_s
53
+ puts "Number wrong this session : ".colorize(:light_red) + num_wrong.to_s
54
+ puts "==============================".colorize(:light_yellow)
55
+ end
56
+
57
+ def questioner(current)
58
+ puts
59
+ puts "We have some questions for you about this #{current.class.to_s.downcase}:".colorize(:light_blue)
60
+ puts "current = "
61
+ if current.is_a? Hash
62
+ ap current, index: false
63
+ elsif current.first.is_a? Array
64
+ PP.pp current
65
+ elsif current.first.is_a? Fixnum
66
+ PP.pp current
67
+ else
68
+ ap current, index: false
69
+ end
70
+ puts
71
+ end
72
+
73
+ def eprinter(error)
74
+ puts
75
+ puts "Sorry, that code resulted in an error:".colorize(:light_red)
76
+ puts "#{error}".colorize(:red)
77
+ end
78
+
79
+ def itswrong(answer)
80
+ @num_wrong += 1
81
+ puts "Sorry, that code is incorrect. ".colorize(:light_red)
82
+ puts
83
+ puts "The right answer is . . . ".colorize(:light_red)
84
+ puts answer.to_s
85
+ puts "Try again!".colorize(:light_red)
86
+ end
87
+
88
+ def itsright
89
+ @num_right += 1
90
+ puts "Correct!".colorize(:green)
91
+ end
92
+
93
+ def prompter(answer)
94
+ print "Write ruby code to find the following value".colorize(:light_blue)
95
+ print " (or enter ".colorize(:light_blue) + 'NEW'.colorize(:green)
96
+ puts " for a new challenge): ".colorize(:light_blue)
97
+ puts answer.to_s
98
+ puts
99
+ print "[1] rubies(main)> "
100
+ end
101
+
102
+ def byebye
103
+ puts
104
+ puts "Thanks for using ".colorize(:green) + "rubies!".colorize(:light_red)
105
+ display_score
106
+ end
107
+
108
+ def display_score
109
+ scoreboard(@num_right, @num_wrong)
110
+ end
111
+
112
+ def clear_screen
113
+ puts "\e[H\e[2J"
114
+ end
115
+
116
+ def prompt(data_structure, target)
117
+ questioner(data_structure)
118
+ prompter(target)
119
+ gets.chomp.gsub("\"", "\'")
120
+ end
121
+
122
+ def check_answer(current, input, target)
123
+ begin
124
+ routine = lambda { eval(input) }
125
+ output = routine.call
126
+ puts "=> #{output}"
127
+ puts
128
+ output == target
129
+ rescue Exception => e
130
+ eprinter(e)
131
+ false
132
+ end
133
+ end
134
+
135
+ def generate_data_structure
136
+ rds = RandomDataStructure.new
137
+ current = rds.generate
138
+ target = rds.all_values.sample
139
+ [current, target]
140
+ end
141
+
142
+ def play_round # new, exit or check if right/wrong
143
+ clear_screen
144
+ correct = false
145
+ current, target = generate_data_structure
146
+ until correct
147
+ input = prompt(current, target)
148
+ if input == "NEW" || input == "new"
149
+ return
150
+ elsif input == "EXIT" || input == "exit"
151
+ @playing = false
152
+ return
153
+ else
154
+ if check_answer(current, input, target)
155
+ itsright
156
+ correct = true
157
+ else
158
+ itswrong(target)
159
+ end
160
+ end
161
+ scoreboard(@num_right, @num_wrong)
162
+ continuer
163
+ end
164
+ end
165
+
166
+ def gameover?
167
+ !@playing
168
+ end
169
+
170
+ #should rename to 'run'
171
+ def game
172
+ display_splash
173
+ until gameover?
174
+ play_round
175
+ end
176
+ byebye
177
+ end
178
+ end
179
+
180
+ end
@@ -0,0 +1,25 @@
1
+ module Rubies
2
+ class RandomArray < ::Array
3
+
4
+ def mini_array
5
+ (-1_000..1_000).sort_by { rand }.sample 3
6
+ end
7
+
8
+ def nesting_array
9
+ result = []
10
+ rand(1..3).times do
11
+ result << mini_array
12
+ end
13
+ result.each do |array|
14
+ array << self.mini_array
15
+ end
16
+ result
17
+ end
18
+
19
+ def generate
20
+ depth = rand(0..3)
21
+ nesting_array.flatten(depth)
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'random_array'
2
+ require_relative 'random_hash'
3
+ require 'faker'
4
+
5
+ module Rubies
6
+ class RandomDataStructure
7
+ attr_reader :data_structure
8
+
9
+ def initialize
10
+ @data_structure = Array.new
11
+ @all_values = Array.new
12
+ end
13
+
14
+ def generate
15
+ combo = Array.new
16
+ rand(1..3).times do
17
+ combo << RandomHash.new.hash_three
18
+ end
19
+ random_hash = RandomHash.new.generate
20
+ random_array = RandomArray.new.generate
21
+ @data_structure = [combo, random_hash, random_array].sample
22
+ end
23
+
24
+ def all_values
25
+ values = Array.new
26
+ if @data_structure.is_a? Hash
27
+ values = @data_structure.values
28
+ elsif @data_structure.flatten.first.is_a? Fixnum
29
+ values = @data_structure.flatten
30
+ else
31
+ @data_structure.each do |hash|
32
+ hash.deep_traverse { |_path, value| values << value }
33
+ end
34
+ end
35
+ values.flatten
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,74 @@
1
+ module Rubies
2
+ class MyHash < ::Hash
3
+ def deep_traverse(&block)
4
+ stack = self.map { |k, v| [[k], v] }
5
+ while not stack.empty?
6
+ key, value = stack.pop
7
+ yield(key, value)
8
+ if value.is_a? Hash
9
+ value.each { |k, v| stack.push [key.dup << k, v] }
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ class RandomHash < MyHash
16
+ def initialize
17
+ @ds = MyHash.new
18
+ end
19
+
20
+ def children
21
+ array = Array.new
22
+ rand(1..3).times do
23
+ array << Faker::Name.first_name
24
+ end
25
+ array
26
+ end
27
+
28
+ def has_kids?
29
+ rand(2) == 1
30
+ end
31
+
32
+ def hash_one
33
+ hash = Hash.new
34
+ 10.times do
35
+ name = Faker::Company.name
36
+ bs = Faker::Company.bs
37
+ hash[name] = bs
38
+ end
39
+ hash
40
+ end
41
+
42
+ def hash_two
43
+ hash = MyHash.new
44
+ 10.times do
45
+ email = Faker::Internet.email
46
+ num = rand(1..1000)
47
+ hash[email] = num
48
+ end
49
+ hash
50
+ end
51
+
52
+ def hash_three
53
+ hash = MyHash.new
54
+ length = rand(1..5)
55
+ count = 1
56
+ while count <= length
57
+ details = Hash.new
58
+ name = Faker::Name.name
59
+ phone = Faker::PhoneNumber.cell_phone
60
+ company = Faker::Company.name
61
+ details["phone"] = phone
62
+ details["company"] = company
63
+ details["children"] = children if has_kids?
64
+ hash[name] = details
65
+ count += 1
66
+ end
67
+ hash
68
+ end
69
+
70
+ def generate
71
+ @ds = [hash_one, hash_two, hash_three].sample
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,3 @@
1
1
  module Rubies
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/rubies.rb CHANGED
@@ -2,173 +2,7 @@
2
2
  require 'colorize'
3
3
  require 'awesome_print'
4
4
  require 'pp'
5
- require_relative 'random_array'
6
- require_relative 'random_hash'
7
- require_relative 'random_data_structure'
8
-
9
- class Game
10
- def initialize
11
- @num_right = 0
12
- @num_wrong = 0
13
- @playing = true
14
- end
15
-
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"
38
- end
39
-
40
- def continuer
41
- puts "Press enter to continue . . . "
42
- gets.chomp
43
- puts "\e[H\e[2J"
44
- end
45
-
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)
52
- end
53
-
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
66
- end
67
- puts
68
- end
69
-
70
- def eprinter(error)
71
- puts
72
- puts "Sorry, that code resulted in an error:".colorize(:light_red)
73
- puts "#{error}".colorize(:red)
74
- end
75
-
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
- end
84
-
85
- def itsright
86
- @num_right += 1
87
- puts "Correct!".colorize(:green)
88
- end
89
-
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
- end
98
-
99
- def byebye
100
- puts
101
- puts "Thanks for using ".colorize(:green) + "rubies!".colorize(:light_red)
102
- display_score
103
- end
104
-
105
- def display_score
106
- scoreboard(@num_right, @num_wrong)
107
- end
108
-
109
- def clear_screen
110
- puts "\e[H\e[2J"
111
- end
112
-
113
- def prompt(data_structure, target)
114
- questioner(data_structure)
115
- prompter(target)
116
- gets.chomp.gsub("\"", "\'")
117
- end
118
-
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
131
-
132
- def generate_data_structure
133
- rds = RandomDataStructure.new
134
- current = rds.generate
135
- target = rds.all_values.sample
136
- [current, target]
137
- end
138
-
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
154
- else
155
- itswrong(target)
156
- end
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
171
- end
172
- byebye
173
- end
174
- end
5
+ require 'rubies/random_array'
6
+ require 'rubies/random_hash'
7
+ require 'rubies/random_data_structure'
8
+ require 'rubies/game'
data/rubies.gemspec CHANGED
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.7"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
27
28
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module Rubies
4
+ describe Game do
5
+
6
+ before do
7
+ @input = FakeInput.new([])
8
+ @game = Rubies::Game.new({ in: @input })
9
+ end
10
+
11
+ it "displays splash" do
12
+ @input << "\n" #add enter to the input
13
+ @game.display_splash
14
+ expect(@input.size).to eq(0)
15
+ end
16
+
17
+ it "displays score" do
18
+ #@game.display_score
19
+ end
20
+
21
+ it "has scoreboard" do
22
+ #@game.scoreboard(10, 10)
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ module Rubies
4
+ describe RandomArray do
5
+
6
+ before do
7
+ @array = Rubies::RandomArray.new
8
+ end
9
+
10
+ it "generates" do
11
+ Rubies::RandomArray.new.generate
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubies'
2
+
3
+ #provides the stdin interface to test the Rubies::Game class.
4
+ class FakeInput
5
+
6
+ def initialize(strings)
7
+ @strings = strings
8
+ end
9
+
10
+ def gets
11
+ next_string = @strings.shift
12
+ # Uncomment the following line if you'd like to see the faked $stdin#gets
13
+ # puts "(DEBUG) Faking #gets with: #{next_string}"
14
+ next_string
15
+ end
16
+
17
+ def <<(strings)
18
+ @strings << strings
19
+ end
20
+
21
+ def size
22
+ @strings.length
23
+ end
24
+
25
+ end
metadata CHANGED
@@ -1,85 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubies
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
7
  - Vikram Ramakrishnan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-13 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faker
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: colorize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: awesome_print
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.7'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.7'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '10.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: 'Newcomers to programming would benefit from practicing drills regularly
84
98
  until basic problem solving becomes second nature. This gem is a command line tool
85
99
  that allows users to solve randomly generated small problems. It currently supports
@@ -91,18 +105,23 @@ executables:
91
105
  extensions: []
92
106
  extra_rdoc_files: []
93
107
  files:
94
- - .gitignore
108
+ - ".gitignore"
109
+ - ".travis.yml"
95
110
  - Gemfile
96
111
  - LICENSE.txt
97
112
  - README.md
98
113
  - Rakefile
99
114
  - bin/rubies
100
- - lib/random_array.rb
101
- - lib/random_data_structure.rb
102
- - lib/random_hash.rb
103
115
  - lib/rubies.rb
116
+ - lib/rubies/game.rb
117
+ - lib/rubies/random_array.rb
118
+ - lib/rubies/random_data_structure.rb
119
+ - lib/rubies/random_hash.rb
104
120
  - lib/rubies/version.rb
105
121
  - rubies.gemspec
122
+ - spec/rubies/game_spec.rb
123
+ - spec/rubies/random_array_spec.rb
124
+ - spec/spec_helper.rb
106
125
  homepage: https://github.com/vikram7/rubies
107
126
  licenses:
108
127
  - MIT
@@ -113,18 +132,21 @@ require_paths:
113
132
  - lib
114
133
  required_ruby_version: !ruby/object:Gem::Requirement
115
134
  requirements:
116
- - - '>='
135
+ - - ">="
117
136
  - !ruby/object:Gem::Version
118
137
  version: '0'
119
138
  required_rubygems_version: !ruby/object:Gem::Requirement
120
139
  requirements:
121
- - - '>='
140
+ - - ">="
122
141
  - !ruby/object:Gem::Version
123
142
  version: '0'
124
143
  requirements: []
125
144
  rubyforge_project:
126
- rubygems_version: 2.0.14
145
+ rubygems_version: 2.2.2
127
146
  signing_key:
128
147
  specification_version: 4
129
148
  summary: A Ruby gem to practice small ruby coding drills.
130
- test_files: []
149
+ test_files:
150
+ - spec/rubies/game_spec.rb
151
+ - spec/rubies/random_array_spec.rb
152
+ - spec/spec_helper.rb
data/lib/random_array.rb DELETED
@@ -1,24 +0,0 @@
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
@@ -1,42 +0,0 @@
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 DELETED
@@ -1,72 +0,0 @@
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