misc #8447

BasicObject id behaviour

Added by Kyrylo Silin 11 months ago. Updated 11 months ago.

[ruby-core:55155]
Status:Closed
Priority:Low
Assignee:Nobuyoshi Nakada
Category:misc
Target version:-

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.tos(16) }"
puts "Eigenclass' id: #{ eig.
id.tos(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

History

#1 Updated by Nobuyoshi Nakada 11 months ago

  • Status changed from Open to Closed

It's just an implementation detail, 0x9773a68 == 0x4bb9d34.

#2 Updated by Nobuyoshi Nakada 11 months ago

nobu (Nobu4bb9d34yoshi Nakada) wrote:

It's just an implementation detail, 0x9773a68 == 0x4bb9d34.

Sorry, missent.
"0x9773a68 == 0x4bb9d34<<1", of course.

Also available in: Atom PDF