Misc #8447
closedBasicObject id behaviour
Description
=begin
This is not really an issue. This is rather a question. It's about ids of BasicObject instances. When I invoke an undefined method on a BasicObject instance, I get an odd NoMethodError. The message of this error informs me that an undefined method is undefined on some other BasicObject (it has a different id). The code demonstrates the problem better.
b = BasicObject.new
eig = class << b; self; end
puts "A BasicObject's id: #{ b.id.to_s(16) }"
puts "Eigenclass' id: #{ eig.id.to_s(16) }"
puts "Eigenclass' inspect: #{ eig.inspect }"
begin
b.piyo
rescue => e
puts e
end
Output.
A BasicObject's id: 4e1d918
Eigenclass' id: 4e1d90e
Eigenclass' inspect: #<Class:#BasicObject:0x9c3b230>
undefined method `piyo' for #BasicObject:0x9c3b230
This is how it looks in a REPL. The ids are different.
irb(main):001:0> b = BasicObject.new
(Object doesn't support #inspect)
=>
irb(main):002:0> b.id.to_s(16)
=> "4bb9d34"
irb(main):003:0> b.piyo
NoMethodError: undefined method piyo' for #<BasicObject:0x9773a68> from (irb):3 from /opt/rubies/ruby-1.9.3-p392/bin/irb:12:in
'
irb(main):004:0>
I tested this against Ruby 1.9.3, Ruby 2.0.0. I guess this is by design, because I got the same result on JRuby. So I'm just curious, what happens here?
=end
Updated by nobu (Nobuyoshi Nakada) about 10 years ago
- Status changed from Open to Closed
It's just an implementation detail, 0x9773a68 == 0x4bb9d34.
Updated by nobu (Nobuyoshi Nakada) about 10 years ago
nobu (Nobu4bb9d34yoshi Nakada) wrote:
It's just an implementation detail, 0x9773a68 == 0x4bb9d34.
Sorry, missent.
"0x9773a68 == 0x4bb9d34<<1", of course.