Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoking define_singleton on an object is adding a subclass to ObjectSpace #4370

Closed
dmarcotte opened this issue Dec 8, 2016 · 3 comments
Closed

Comments

@dmarcotte
Copy link
Contributor

Environment

$ ruby -v
jruby 9.1.6.0 (2.3.1) 2016-11-09 0150a76 Java HotSpot(TM) 64-Bit Server VM 25.91-b14 on 1.8.0_91-b14 +jit [darwin-x86_64]

$ uname -a
Darwin zero 16.1.0 Darwin Kernel Version 16.1.0: Thu Oct 13 21:26:57 PDT 2016; root:xnu-3789.21.3~60/RELEASE_X86_64 x86_64

Expected Behavior

Invoking define_singleton on an object should not add a subclass to ObjectSpace.

Here's a script to demonstrate:

class MyClass
end

my_class_instance = MyClass.new

my_class_children = ObjectSpace.each_object(::Class).select do |klass|
  klass <= MyClass
end

my_class_children.each do |obj|
  puts "Child of MyClass before `define_singleton_method`: #{obj.name}"
end

my_class_instance.define_singleton_method(:my_singleton) {}

my_class_children = ObjectSpace.each_object(::Class).select do |klass|
  klass <= MyClass
end

my_class_children.each do |obj|
  puts "Child of MyClass after `define_singleton_method`: #{obj.name}"
end

MRI and JRuby 1.7.25 output the following (expected):

Child of MyClass before `define_singleton_method`: MyClass
Child of MyClass after `define_singleton_method`: MyClass

Actual Behavior

JRuby 9.1.6.0 outputs the following, demonstrating the presence of that undesirable class:

Child of MyClass before `define_singleton_method`: MyClass
Child of MyClass after `define_singleton_method`:
Child of MyClass after `define_singleton_method`: MyClass
@headius
Copy link
Member

headius commented Dec 8, 2016

I thought they changed each_object(Class) to also emit singleton classes in a recent Ruby. Will confirm.

@headius
Copy link
Member

headius commented Dec 8, 2016

I think you may have been testing an older MRI.

$ ruby23 -v blah2.rb
ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-darwin14]
Child of MyClass before `define_singleton_method`: MyClass
Child of MyClass after `define_singleton_method`: 
Child of MyClass after `define_singleton_method`: MyClass

@headius headius closed this as completed Dec 8, 2016
@dmarcotte
Copy link
Contributor Author

Ah, thanks @headius! My testing was indeed against 2.2.2. Apologies for the noise.

Is lack of a name the "right" way to detect these? Or is there another property that should be consulted?

@headius headius added this to the Invalid or Duplicate milestone Dec 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants