I am having trouble running an rspec file, provided as part of an exercise, and I am not sure what is going on.
Here is my code in silly_blocks.rb:
def reverser(num = 1)
result = []
if yield == Integer
yield + num
else
yield.split.each{|word| result << word.reverse}
result.join(' ')
end
end
Here is the rspec file:
require "05_silly_blocks"
describe "some silly block functions" do
describe "reverser" do
it "reverses the string returned by the default block" do
result = reverser do
"hello"
end
result.should == "olleh"
end
it "reverses each word in the string returned by the default block" do
result = reverser do
"hello dolly"
end
result.should == "olleh yllod"
end
end
describe "adder" do
it "adds one to the value returned by the default block" do
adder do
5
end.should == 6
end
it "adds 3 to the value returned by the default block" do
adder(3) do
5
end.should == 8
end
end
describe "repeater" do
it "executes the default block" do
block_was_executed = false
repeater do
block_was_executed = true
end
block_was_executed.should == true
end
it "executes the default block 3 times" do
n = 0
repeater(3) do
n += 1
end
n.should == 3
end
it "executes the default block 10 times" do
n = 0
repeater(10) do
n += 1
end
n.should == 10
end
end
end
I get this error when it hits the third test 'adder':
Failures:
1) some silly block functions adder adds one to the value returned by the default block
Failure/Error: adder do
NoMethodError:
undefined method `adder' for #<RSpec::ExampleGroups::SomeSillyBlockFunctions::Adder:0x007f334345b460>
# ./p.rb:30:in `block (3 levels) in <top (required)>'
It seems that adder was defined in the exact same way as previous methods in the rspec, so I am not sure what is going on. I have check various other posts about this but haven't found anything to help me, or at least that I understand enough to help me.
05_silly_blocks.rb, too?