Created
April 21, 2009 15:56
-
-
Save dchelimsky/99209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Failure for the 3rd spec in ie_spec.rb and test in ie_test.rb is the same: | |
wrong # of arguments(4 for 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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