Class: ReduceDrill
- Inherits:
-
Drill
show all
- Defined in:
- lib/ruby_drills/array/reduce_drill.rb
Constant Summary
Constants included
from Commands
Commands::GAMBLER
Instance Attribute Summary
Attributes inherited from Drill
#hints, #next, #previous
Instance Method Summary
collapse
Methods inherited from Drill
#done?, #drills, #expected, #initialize, #non!
Methods included from Commands
#back, #clear, #continue, #fail, #fold, #help, #hint, #quit, #review, #skip, #welcome, #win
Constructor Details
This class inherits a constructor from Drill
Instance Method Details
#reference ⇒ Object
23
24
25
|
# File 'lib/ruby_drills/array/reduce_drill.rb', line 23
def reference
"@values.reduce(:+)/@values.size"
end
|
#setup ⇒ Object
3
4
5
6
7
8
9
|
# File 'lib/ruby_drills/array/reduce_drill.rb', line 3
def setup
@values = [3, 5, 19, 25, 23, 40, 55, 36, 32, 52]
@hints = ["How could you reduce this problem to something simpler?",
"TODO: Inject a hint here...",
"This method is called foldLeft in some languages.",
"http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-reduce"]
end
|
#show ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/ruby_drills/array/reduce_drill.rb', line 11
def show
puts %{
@values = #{@values.inspect}
The mean of the numbers in @values is #{expected.inspect}.
Use a method that combines the items in an Enumerable by
iterating over them to calculate this mean.
}
end
|
#valid?(input) ⇒ Boolean
27
28
29
|
# File 'lib/ruby_drills/array/reduce_drill.rb', line 27
def valid?(input)
['reduce', 'inject'].any? {|x| input.include?(x) }
end
|