Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/mtodd/extlib into mtodd
Browse files Browse the repository at this point in the history
  • Loading branch information
sam committed Sep 8, 2008
2 parents 0561b52 + f94302d commit 7d48c1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/extlib/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,17 @@ def in?(arrayish,*more)
arrayish = more.unshift(arrayish) unless more.empty?
arrayish.include?(self)
end

# @note
# For instances of objects that don't override the #inspect method, there
# is an encoded hash to uniquely identify the object. This method
# reproduces this to be used in #inspect in order to keep the same look
# as well as provide this information.
# This is inessential, but may be useful.
#
# @return <String>
# Returns the 16-byte encoded hash for the object
def encoded_hash
(self.hash * 2).to_s(16)
end
end
18 changes: 18 additions & 0 deletions spec/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Bar
end
end

class Oi
attr_accessor :foo
end

describe Object do

describe "#full_const_get" do
Expand Down Expand Up @@ -108,4 +112,18 @@ class Bar
5.in?(@set).should be(true)
end
end

describe "#encoded_hash" do
it 'returns the encoded hash like the value in the default Object#inspect' do
o = Oi.new
original_inspect = o.inspect
original_inspect.should =~ %r{#<Oi:0x([a-z0-9]{6,})>}

o.foo = 1
o.inspect.should =~ %r{#<Oi:0x([a-z0-9]{6,}) @foo=1>}

class << o; def inspect; "#<Oi:0x#{self.encoded_hash}>"; end; end
o.inspect.should == original_inspect
end
end
end

0 comments on commit 7d48c1e

Please sign in to comment.