0

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.

2
  • Can you show us the content of 05_silly_blocks.rb, too? Commented Apr 9, 2015 at 20:18
  • I just updated it to include my code :) Commented Apr 9, 2015 at 20:22

1 Answer 1

1

The function under test (adder) isn't defined yet, just as the spec failure tells. Defining it is probably part of your exercise. To define it, add

def adder
end

in 05_silly_blocks.rb, either before or after your current code.

(More is needed to get the third example to pass, but as you already got the first two examples passing, you'll probably know what to do from there.)

Sign up to request clarification or add additional context in comments.

3 Comments

Wow. It's so obvious now that you pointed it out, that it was looking for the new method to be defined in silly_blocks.rb, not the spec file. Sad is that I have done this before and understood it simply by reading the spec file, lol. I must need more breaks, as my brain is deep-fried at this point! Thanks @das-g
You're welcome, @HolyMoly. :-) It doesn't specifically look in silly_blocks.rb, though. You could indeed just as well define the function in your spec file to get the spec to pass, but I don't think that's the idea of your exercise.
Good to know! ...So in addition to having my frustration relieved because I missed something obvious, I learned something new!! That makes posting such a stupid question extra-worthwhile :) lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.