with adder default blocks exercise. How Do I vary output depending if I get a block to my method or not?

Here is the rspec I need to pass:

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

This is my code

def adder(&block)

if block.call == 5
  block.call + 1

else

block.call + 3

end

end

My error output

adder adds one to the value returned by the default block adds 3 to the value returned by the default block (FAILED - 1)

Failures:

1) some silly block functions adder adds 3 to the value returned by the default block Failure/Error: adder(3) do ArgumentError: wrong number of arguments (1 for 0) # ./05_silly_blocks/silly_blocks.rb:5:in adder' # ./05_silly_blocks/silly_blocks_spec.rb:37:inblock (3 levels) in '

Answers


You pass adder an unexpected parameter:

adder(3) do
  5
end

When adder is defined to receive no parameters at all:

def adder(&block)

You should add an optional parameter to adder definition

def adder(num=1, &block)
  block.call + num
end

def adder(num = 1)
  yield + num
end

Need Your Help

Open and close an electrical circuit

c# .net serial-port

I have an application that one of it's features needs to control an electrical circuit of some device (open and close a circuit). Since i have no background in electronics i really don't know where...

cpython vs cython vs numpy array performance

python numpy cython

I am doing some performance test on a variant of the prime numbers generator from http://docs.cython.org/src/tutorial/numpy.html.