Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created April 21, 2009 15:56
Show Gist options
  • Save dchelimsky/99209 to your computer and use it in GitHub Desktop.
Save dchelimsky/99209 to your computer and use it in GitHub Desktop.
class Adder
def initialize(*args)
@args = args
@block = lambda {|*args|
args.inject(0) {|a, sum| sum += a}
}
end
def add
instance_exec(*@args, &@block)
end
end
# Failure for the 3rd spec in ie_spec.rb and test in ie_test.rb is the same:
wrong # of arguments(4 for 3)
require 'adder'
describe "instance_exec" do
it "works with two args" do
adder = Adder.new(1,2)
adder.add.should == 3
end
it "works with three args" do
adder = Adder.new(1,2,3)
adder.add.should == 6
end
it "fails with four args" do
adder = Adder.new(1,2,3,4)
adder.add.should == 10
end
end
require 'adder'
require 'test/unit'
class InstanceExecTest < Test::Unit::TestCase
def test_with_two_args
adder = Adder.new(1,2)
assert_equal 3, adder.add
end
def test_with_three_args
adder = Adder.new(1,2,3)
assert_equal 6, adder.add
end
def test_with_four_args
adder = Adder.new(1,2,3,4)
assert_equal 10, adder.add
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment