ruby_drills 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +6 -0
  6. data/CREDITS.txt +0 -0
  7. data/Gemfile +2 -0
  8. data/LICENSE.txt +202 -0
  9. data/NOTICE.txt +11 -0
  10. data/README.md +118 -0
  11. data/Rakefile +6 -0
  12. data/bin/ruby_drills +13 -0
  13. data/lib/ruby_drills/array/all_drill.rb +28 -0
  14. data/lib/ruby_drills/array/all_names_drill.rb +30 -0
  15. data/lib/ruby_drills/array/array_drills.rb +18 -0
  16. data/lib/ruby_drills/array/chunk_drill.rb +34 -0
  17. data/lib/ruby_drills/array/drop_while_drill.rb +27 -0
  18. data/lib/ruby_drills/array/partition_drill.rb +27 -0
  19. data/lib/ruby_drills/array/reduce_drill.rb +24 -0
  20. data/lib/ruby_drills/array/take_while_drill.rb +26 -0
  21. data/lib/ruby_drills/array/zip_drill.rb +28 -0
  22. data/lib/ruby_drills/chomper.rb +23 -0
  23. data/lib/ruby_drills/cli.rb +76 -0
  24. data/lib/ruby_drills/commands.rb +106 -0
  25. data/lib/ruby_drills/config.rb +19 -0
  26. data/lib/ruby_drills/data/gambler.ascii +65 -0
  27. data/lib/ruby_drills/drill.rb +66 -0
  28. data/lib/ruby_drills/drills.rb +52 -0
  29. data/lib/ruby_drills/hash/hash_drills.rb +17 -0
  30. data/lib/ruby_drills/sessions/collector_client.rb +39 -0
  31. data/lib/ruby_drills/sessions/local.rb +45 -0
  32. data/lib/ruby_drills/sessions/timestamp.rb +9 -0
  33. data/lib/ruby_drills/string/string_drills.rb +16 -0
  34. data/lib/ruby_drills/version.rb +3 -0
  35. data/lib/ruby_drills/welcome/welcome_drills.rb +13 -0
  36. data/lib/starter.rb +68 -0
  37. data/ruby_drills.gemspec +29 -0
  38. data/spec/commands_spec.rb +24 -0
  39. data/spec/ruby_drills_spec.rb +7 -0
  40. data/spec/sessions/timestamp_spec.rb +10 -0
  41. data/spec/spec_helper.rb +2 -0
  42. metadata +194 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --drb
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ ruby_drills
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p429
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ### 0.1.1 (2014/1/?)
2
+
3
+
4
+ ### 0.1.0 (2014/1/13)
5
+
6
+ Ship It!
data/CREDITS.txt ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/NOTICE.txt ADDED
@@ -0,0 +1,11 @@
1
+ =============================================================================
2
+ == NOTICE file for use with the Apache License, Version 2.0, ==
3
+ == in this case for the Ruby Drills distribution. ==
4
+ =============================================================================
5
+
6
+ Ruby Drills
7
+
8
+ This product includes software developed by
9
+ Bobby Norton (bobby@testedminds.com)
10
+
11
+ For additional credits see the Acknowledgments in the project README.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # Ruby Drills
2
+
3
+ Drills are used in sports to master specific movements. In basketball, players drill on free throws, three-pointers, and dribbling. In programming, drills provide a way to engage in deliberate practice
4
+ to develop skills in specific language features, API's, or idioms.
5
+
6
+ Scrimmages are used in sports to mimic the conditions and complexity of a game in order to integrate skills built in drills and develop teamwork. Drills complement more complex scrimmage practices such as Ruby Koans, Ruby Quiz, or work on an open-source project. Were we to use a Japanese expression, drills could also be thought of as 'Kihon', the simpler precursor to 'Kata'. Drills lack the complexity of a full project. They can be performed alone or with friends.
7
+
8
+ Challenges in Ruby Drills focus on a specific method. Answers typically consist of a single line. Your objective is to complete the drill with ease and joy, without consulting any external documentation. At first, you will most likely find this kind of practice neither easy nor joyful. Code on. Practice for a few days until your knowledge of these methods becomes automatic. Doing so will make your day to day programming more enjoyable by freeing up your mind to focus on higher-level, higher-value complexities than the syntax of the language.
9
+
10
+ ## Getting Started
11
+
12
+ $ gem install ruby_drills
13
+ $ ruby_drills
14
+
15
+ Drills run in the command line. You answer questions in drills using a REPL. This practice has the benefit of preparing you for developing at the REPL, a useful technique for trying out langauge features or algorithms and a great complement to TDD.
16
+
17
+ Start by choosing an available drill from the menu. The Enumerable drill currently has the most
18
+ content, so start there!
19
+
20
+ ## Example
21
+
22
+ A drill session for the `Enumerable#zip` method might consist of the following:
23
+
24
+ We have two arrays:
25
+ a: [4, 5, 6]
26
+ b: [7, 8, 9]
27
+
28
+ Use a method from the Enumerable mixin that will take one element from
29
+ a new array and merge the corresponding elements from a and b to yield
30
+ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]:
31
+
32
+ >> c = [1,2,3]
33
+ => [1, 2, 3]
34
+
35
+ not yet...
36
+
37
+ >> c.zip(a, b)
38
+ => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
39
+
40
+ !!! WIN !!!
41
+
42
+ How does your answer compare to the reference solution?
43
+ [1,2,3].zip(@a, @b)
44
+
45
+ Press any key to continue:
46
+
47
+ ## Community
48
+
49
+ Questions about Ruby Drills? Ideas about how to practice? Head over to the [Google Group](https://groups.google.com/forum/#!forum/ruby-drills).
50
+
51
+ ## Analytics
52
+
53
+ As you interact with Ruby Drills, your answers are being saved in a remote Drill Collector API hosted by Tested Minds. No personally identifiable information is included in this data: At this time, we are interested in overall practice patterns and statistics, not individual user profiles. Activity data looks like:
54
+
55
+ {
56
+ "timestamp" : "May 5, 2013 12:03:51 AM CST",
57
+ "result" : "pass",
58
+ "input" : "@numbers.partition{|x | x.even? }",
59
+ "sessionId" : "2217-47e8-b80a-68b81c980c32",
60
+ "drill" : "PartitionDrill",
61
+ "type" : "attempt",
62
+ "reference" : "@numbers.partition {|x| x.even? }"
63
+ }
64
+
65
+ We're interested in questions like:
66
+
67
+ * Which drills are practiced most frequently?
68
+ * How long do people typically practice in a given session?
69
+ * What are the patterns behind an effective drill vs. one that is too easy or too hard?
70
+
71
+ This data will help us evolve the Ruby Drills experience. This could also form the basis of a more personalized experience, allowing you to review your progress and compare your practice sessions to others. More on this to come as we analyze the data collected and share the results with the community.
72
+
73
+ Ultimately, we'd like to find out if drill and practice in Ruby and other programming languages helps us solve more complex programming challenges. There is currently very little data available to researchers to help answer this question scientifically. Hopefully, the data collected in Ruby Drills can provide a piece of this puzzle.
74
+
75
+
76
+ ## Under the Hood: The Drill API
77
+
78
+ Each drill consists of five methods:
79
+
80
+ `setup`: initialize instance variables used in the drill.
81
+
82
+ `show`: puts a string that describes the drill.
83
+
84
+ `hints`: return an array of hints that will be randomly shown.
85
+
86
+ `reference`: provide a reference solution as valid ruby code in string form. This will be eval'ed to determine if the user's input should be validated.
87
+
88
+ `valid?(input)`: a validation function that returns true or false. Used to see if a user's input that matches the reference solution meets other constraints.
89
+
90
+ ## Contributing
91
+
92
+ Please check the [Issues List](http://github.com/bobbyno/ruby_drills/issues) on Github to find something to do.
93
+
94
+ If you're new to Github:
95
+
96
+ 1. Fork the repository
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Create passing specs or cukes (once we figure out the testing strategy)
99
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
100
+ 5. Push to the branch (`git push origin my-new-feature`)
101
+ 6. Create new Pull Request
102
+
103
+ ## Contributors
104
+
105
+ * [Bobby Norton](https://twitter.com/bobbynorton)
106
+ * [David Chelimsky](https://twitter.com/dchelimsky)
107
+
108
+ ## Acknowledgments
109
+
110
+ * Sarah Aslanifar at [Tested Minds](http://literate.ly) for early review and feedback.
111
+ * The inaugural Chicago [Dev Bootcamp](http://devbootcamp.com) class of June 2013 for inspiring the project in the first place.
112
+ * [Erik Allar](https://twitter.com/allareri) for endless encouragement and enthusiasm.
113
+ * [Bill Van Der Laan](https://twitter.com/Vanderln) and Rod Levy: The first UX testers
114
+ * [Tiffany Mikell](https://twitter.com/mikellsolution) and [Torey Hickman](https://twitter.com/toreyhickman) for feedback on how to incorporate Ruby Drills into the Dev Bootcamp curriculum.
115
+
116
+ ## License
117
+
118
+ Ruby Drills is licensed under the Apache License, version 2.0. See the enclosed LICENSE.txt and NOTICE.txt for more information.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/ruby_drills ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $0 = 'starter'
4
+
5
+ begin
6
+ require 'starter'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'starter'
10
+ end
11
+
12
+ # Process command line options and run
13
+ RubyDrills::CLI.parse_options
@@ -0,0 +1,28 @@
1
+ class AllDrill < Drill
2
+
3
+ def setup
4
+ @values = [ nil, true, 99 ]
5
+ @hints = ["The method you'll use takes an optional block. Optional.",
6
+ "http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-all-3F"]
7
+ end
8
+
9
+ def show
10
+ puts %{
11
+ @values = #{@values.inspect}
12
+
13
+ Does this array contain values that are false or nil?
14
+
15
+ Use the method that will return true only if none of the collection
16
+ members are false or nil. No block is necessary to answer this:
17
+ }
18
+ end
19
+
20
+ def reference
21
+ "@values.all?"
22
+ end
23
+
24
+ def valid?(input)
25
+ input.include?("all?")
26
+ end
27
+
28
+ end
@@ -0,0 +1,30 @@
1
+ class AllNamesDrill < Drill
2
+
3
+ def setup
4
+ @names = %w[ Al Slartibartfast Aaron Yvonne Bobby ]
5
+ @hints = ["Your answer will look something like @names.xxxxx {|y| y.zzz > 4 }",
6
+ "http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-all-3F"]
7
+ end
8
+
9
+ def show
10
+ puts %{
11
+ @names = #{@names.inspect}
12
+
13
+ Is each name longer than four characters?
14
+
15
+ Use the method that passes each element of the collection
16
+ to the given block and returns true if the block never
17
+ returns false or nil.
18
+
19
+ }
20
+ end
21
+
22
+ def reference
23
+ "@names.all? {|n| n.size > 4}"
24
+ end
25
+
26
+ def valid?(input)
27
+ input.include?("all?") && (input.include?('size > 4') || input.include?('length > 4'))
28
+ end
29
+
30
+ end
@@ -0,0 +1,18 @@
1
+ class ArrayDrills < Drills
2
+
3
+ def banner
4
+ %{
5
+ Ruby Drills: Arrays and Enumerable
6
+
7
+ Arrays are ordered, integer-indexed collections of any object.
8
+
9
+ Arrays are Enumerable. The Enumerable mixin provides collection
10
+ classes with several traversal and searching methods, and with the ability to sort.
11
+ In this drill, you'll practice the methods in the Enumerable mixin that relate to
12
+ Arrays.
13
+
14
+ ------------------------------------------------------------------
15
+ }
16
+ end
17
+
18
+ end
@@ -0,0 +1,34 @@
1
+ class ChunkDrill < Drill
2
+
3
+ def setup
4
+ @names = %w[ Al Slartibartfast Aaron Yvonne Bobby ]
5
+ @hints = ["Remember, this method is especially useful for sorted series of elements.",
6
+ ".to_a can be useful to see the output of methods that produce Enumerators",
7
+ "http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-chunk"]
8
+ end
9
+
10
+ def show
11
+ puts %{
12
+ @names = #{@names.inspect}
13
+
14
+ Count the words in @names by the first letter of each name
15
+ to produce an array containing:
16
+ #{expected}
17
+
18
+ First sort this array, then use the method that splits self into
19
+ multiple Enumerators using the rule given in the block. It
20
+ then passes the result of the rule and an Enumerator
21
+ of the successive elements to another Enumerable method.
22
+
23
+ }
24
+ end
25
+
26
+ def reference
27
+ "@names.sort.chunk {|n| n[0]}.map {|char, lines| [char, lines.size]}"
28
+ end
29
+
30
+ def valid?(input)
31
+ ["chunk", "sort"].all? {|fn| input.include?(fn)}
32
+ end
33
+
34
+ end
@@ -0,0 +1,27 @@
1
+ class DropWhileDrill < Drill
2
+
3
+ def setup
4
+ @b = [1, 2, 3, 4, 5, 0]
5
+ @hints = ["http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-drop_while"]
6
+ end
7
+
8
+ def show
9
+ puts %{
10
+ @b = #{@b.inspect}
11
+
12
+ Use the Enumerable method that removes elements up to, but not including,
13
+ the first element for which the block returns nil or false and returns an array
14
+ containing the remaining elements, resulting in:
15
+ #{expected}
16
+ }
17
+ end
18
+
19
+ def reference
20
+ "@b.drop_while {|x| x < 4}"
21
+ end
22
+
23
+ def valid?(input)
24
+ input.include?('drop_while')
25
+ end
26
+
27
+ end
@@ -0,0 +1,27 @@
1
+ class PartitionDrill < Drill
2
+ def setup
3
+ @numbers = (0...10).to_a
4
+ @hints = ["What's another word for 'split' or 'divide'?", "http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-partition"]
5
+ end
6
+
7
+ def show
8
+ puts %{
9
+ @numbers = #{@numbers.inspect}
10
+
11
+ Split this array into even and odd numbers to obtain
12
+ #{expected}.
13
+
14
+ Use the method that returns two arrays, the first containing
15
+ the elements of enum for which the block evaluates to true,
16
+ the second containing the rest.
17
+ }
18
+ end
19
+
20
+ def reference
21
+ "@numbers.partition {|x| x.even? }"
22
+ end
23
+
24
+ def valid?(input)
25
+ input.include?('partition')
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ class ReduceDrill < Drill
2
+
3
+ def setup
4
+ @values = [ 1, 2, 3 ]
5
+ @hints = ["Use a method from the Enumerable module", "How could you use reduce to solve this problem?"]
6
+ end
7
+
8
+ def show
9
+ puts %{
10
+ @values = #{@values.inspect}
11
+
12
+ Calculate the mean of this array.
13
+ }
14
+ end
15
+
16
+ def reference
17
+ "@values.reduce(:+)/@values.size"
18
+ end
19
+
20
+ def valid?(input)
21
+ input.include?("reduce")
22
+ end
23
+
24
+ end
@@ -0,0 +1,26 @@
1
+ class TakeWhileDrill < Drill
2
+
3
+ def setup
4
+ @a = [1, 2, 3, 4, 5, 0]
5
+ @hints = ["http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-take_while"]
6
+ end
7
+
8
+ def show
9
+ puts %{
10
+ @a = #{@a.inspect}
11
+
12
+ Use the Enumerable method that passes elements to a block
13
+ until the block returns nil or false, then stops iterating and
14
+ returns an array of all prior elements to return #{expected.inspect}:
15
+ }
16
+ end
17
+
18
+ def reference
19
+ "@a.take_while {|x| x < 4}"
20
+ end
21
+
22
+ def valid?(input)
23
+ input.include?('take_while')
24
+ end
25
+
26
+ end
@@ -0,0 +1,28 @@
1
+ class ZipDrill < Drill
2
+
3
+ def setup
4
+ @a = [ 4, 5, 6 ]
5
+ @b = [ 7, 8, 9 ]
6
+ @hints = ["http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-zip"]
7
+ end
8
+
9
+ def show
10
+ puts %{We have two arrays:
11
+ @a: #{@a.inspect}
12
+ @b: #{@b.inspect}
13
+
14
+ Use the Enumerable method that will take one element from a new
15
+ array and merge the corresponding elements from a and b to return
16
+ #{expected.inspect}:
17
+ }
18
+ end
19
+
20
+ def reference
21
+ "[1,2,3].zip(@a, @b)"
22
+ end
23
+
24
+ def valid?(input)
25
+ input.include?('zip')
26
+ end
27
+
28
+ end
@@ -0,0 +1,23 @@
1
+ require 'io/wait'
2
+
3
+ class Chomper
4
+ # Allows 'Press any key to continue' to work without requiring return.
5
+ def self.get_char
6
+ begin
7
+ # save previous tty state
8
+ state = `stty -g`
9
+ # -icanon - disable canonical input (ERASE and KILL processing)
10
+ # sig - checking characters against control characters INTR, QUIT, and SUSP
11
+ `stty raw -echo -icanon isig`
12
+
13
+ begin
14
+ # do / while since using the arrow keys results in multi-byte input
15
+ # that gets left in the stdin buffer.
16
+ STDIN.getc.chr
17
+ end while $stdin.ready?
18
+ ensure
19
+ # restore the default tty state
20
+ `stty #{state}`
21
+ end
22
+ end
23
+ end