Closed kennyfrc closed 9 months ago
I came here with exactly the same request! This is a blocker for a large piece of work to upgrade to Ruby 3.0, and I would love to have this merged.
I'm just having a look at what the code is meant to be doing. It's a simpler version of https://github.com/rails/rails/blob/94ca3e0a571dba0fe41ca18d61634c5f3aa11209/activesupport/lib/active_support/core_ext/class/attribute.rb
I've forked my own copy of sucker_punch, and unfortunately, there is no test that covers a positive use case for if singleton_class?
, so I can't see how/if it is used in real life. My gut feel is that it's not used, give that the class_attribute
method is only expected to be called in a class definition, but I can't say that with any authority!
Without having a test to show any failures, I would recommend renaming or inlining it, rather than making it a class method. This is because singleton_class?
is a core ruby method (it probably wasn't when sucker_punch was first written - it isn't there in Ruby 2.4) and we really don't want to be overwriting core ruby methods.
Btw, I'm not even sure if the singleton_class? method does what it is expected to. When I run the demo code from the core ruby singleton_class?
example with the definition of singleton_class? from sucker_punch in Ruby 2.4 this is what I get:
class Class
def singleton_class?
ancestors.first != self
end
end
class C
end
C.singleton_class? #=> false
C.singleton_class.singleton_class? #=> should be true according to the demo, but is false
I ran into this a while back with #231
Couple more alternatives to fixing it:
Hey @brandonhilkert! 👋🏾
Would you consider merging this or #253 as projects looking to upgrade to Ruby 3.x would greatly benefit from this.
Any updates on merging this?
Closed in favor of https://github.com/brandonhilkert/sucker_punch/pull/253
3.2.0
released w/ fix
Hi there, thanks for this gem! As I was testing this, I bumped into a bug when it's used with zeitwerk.
Problem
When zeitwerk loads the Busy Class, it invokes
singleton_class?
method (see here. Because of the singleton_class monkey patch incore_ext.rb
, it raises the below error.Error Trace
Steps to reproduce with Zeitwerk
You can use the demo application located here and follow the steps. Running
bundle exec rackup
should throw the above error.Proposed Fix
To avoid this from clashing with the instance method
singleton_class?
I propose to to move this to a class method.Tests
All tests pass