Reputation: 17261
I have a class in Ruby with some static initialization like this:
class Repository
def self.my_static_setup
....
end
my_static_setup
...
end
The code above works fine, and my custom static initializer gets called, the problem is whenever I inherit this class:
class PersonRepository
...
end
The static initialization is not inherited, and therefore not called. What am I doing wrong?
Upvotes: 0
Views: 1193
Reputation: 118271
@megar told correctly, why the issue you are having.
As per OP's comment:
I see it is not inherited, so I am trying to find a workaround to get self.my_static_setup called whenever I define subclasses.
I can then give you the below soltuion to things get work for you. See Class#inherited
for the same, which is saying Callback invoked whenever a subclass of the current class is created.
class Repository
def self.my_static_setup
puts 'Hello!'
end
def self.inherited(subclass)
my_static_setup
end
end
class PersonRepository < Repository
#...
end
# >> Hello!
Upvotes: 4
Reputation: 239311
You're immediately invoking the method with my_static_setup
. That part cannot be inherited, it's just code.
Upvotes: 0