Given a class like:
class Thing
CONSTANT = 10
# other code
end
And another like:
class Other
def initialize
@thing = Thing.new
end
def method
puts CONSTANT
end
end
Is it possible to extend Forwardable
to have Other
's CONSTANT
delegate to Thing
's constant?
I have tried extendingSingleForwardable
(see docs here ) like so:
class Other
extend SingleForwardable
def initialize
@thing = Thing.new
end
def method
puts CONSTANT
end
def_single_delegator :@thing, :CONSTANT
end
and:
def_single_delegator :@thing, :constant
but neither worked, am I getting the syntax wrong? If not, is there another way?
This one is answered by the documentation (bold emphasis mine):
def_single_delegator(accessor, method, new_name=method)
Defines a method method which delegates to accessor (i.e. it calls the method of the same name in accessor). If new_name is provided, it is used as the name for the delegate method.
So, no, you can only delegate message sends, not constant lookup.