Skip to content

Commit

Permalink
Method#== and specs from Scott Taylor, slightly tweaked. Closes rubin…
Browse files Browse the repository at this point in the history
  • Loading branch information
Eero Saynatkari committed Jan 8, 2008
1 parent 16ae5f8 commit 6df303e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kernel/core/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Method
attr_reader :module
attr_reader :receiver
attr_reader :method

def initialize(recv, mod, cm)
@receiver = recv
Expand Down Expand Up @@ -39,6 +41,16 @@ def to_proc
env = Method::AsBlockEnvironment.new self
Proc.from_environment(env)
end

# Method objects are equal if they have the
# same body and are bound to the same object.
def ==(other)
if other.kind_of? Method
return true if other.receiver.equal?(@receiver) && other.method == @method
end

false
end
end

class Method::AsBlockEnvironment < BlockEnvironment
Expand Down
20 changes: 20 additions & 0 deletions spec/ruby/1.8/core/method/equal_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
describe "Method#==" do
before(:each) do
@m = MethodSpecs::Methods.new
@m2 = MethodSpecs::Methods.new
@a = MethodSpecs::A.new
end

it "returns true if methods are the same" do
Expand All @@ -20,4 +22,22 @@

(m1 == m2).should == true
end

it "returns false on a method which is neither aliases nor the same method" do
m1 = @m.method(:foo)
m2 = @m.method(:zero)

(m1 == m2).should == false
end

it "returns false for a method which is not bound to the same object" do
m1 = @m.method(:foo)
m2 = @m2.method(:foo)

a = @a.method(:baz)

(m1 == m2).should == false
(m1 == a).should == false
(m2 == a).should == false
end
end

0 comments on commit 6df303e

Please sign in to comment.