Calling a Protected Superclass Class Method in Ruby - ruby

I want to call a protected superclass class method from an instance method in the base class.
class A
class << self
protected
def foo
puts "In foo"
end
end
end
class B < A
def bar
puts "In bar"
# call A::foo
end
end
What's the best way to do this?

... 2.67 years later ...
A simpler way to solve this is with class_eval
class A
class << self
protected
def foo
puts "In foo"
end
end
end
class B < A
def bar
self.class.class_eval { foo }
end
end
B.new.bar # prints "In foo"

Override the method in B, calling super:
class A
class << self
protected
def foo
puts "In foo"
end
end
end
class B < A
def self.foo
super
end
def bar
puts "In bar"
# call A::foo
self.class.foo
end
end
>> B.foo
=> In foo
>> B.new.bar
=> In bar
=> In foo

So far, the only solution I've found is to define a class method in the subclass that calls the class method in the superclass. Then I can call this method in the subclass' instance method.
class A
class << self
protected
def foo
puts "In foo"
end
end
end
class B < A
def self.call_foo
puts "In call_foo"
A::foo
end
def bar
puts "In bar"
self.class.call_foo
end
end
Is this really necessary?

I'd probably just make A.foo public. Otherwise send will do it, since it bypasses access controls:
A.send(:foo)

Related

In Ruby, what's the best way to execute a block around a child method?

I have a parent class:
class Base
def my_method
block_method do
# EXECUTE WHATEVER'S IN THE CHILD VERSION OF my_method
# HOW TO DO?
end
end
def block_method
original_foo = 'foo'
foo = 'CUSTOM FOO'
yield
foo = original_foo
end
end
and some child classes--currently five and there will be more:
class ChildA < Base
def my_method
puts 'apple'
puts 'aardvark'
end
end
class ChildE < Base
def my_method
puts 'eel'
puts 'elephant'
end
end
I want to change what a variable refers to just for the duration of each Child's #my_method.
My thought is to do this by wrapping the functionality of each Child's #my_method in a block. But I'd rather do that in the parent class than have to wrap each child class's #my_method in the exact same block.
Any insights on how I can do this?
If there's some opposite of super, I guess that would be one way to accomplish what I want to do.
You could give the idea of what you are calling "some opposite of super" using a module and prepend like so:
module MethodThing
# added to remove binding from class since
# #my_method relies on the existence of #foo and #foo=
def self.prepended(base)
base.attr_reader(:foo) unless base.method_defined?(:foo)
base.attr_writer(:foo) unless base.method_defined?(:foo=)
end
def my_method
puts "Before: #{foo}"
original_foo = foo
self.foo= 'CUSTOM FOO'
begin
super
rescue NoMethodError
warn "(skipped) #{self.class}##{__method__} not defined"
end
self.foo = original_foo
puts "After: #{foo}"
end
end
Prepend the module on inheritance
class Base
def self.inherited(child)
child.prepend(MethodThing)
end
attr_accessor :foo
def initialize
#foo = 12
end
end
class ChildA < Base
def my_method
puts 'apple'
puts "During: #{foo}"
puts 'aardvark'
end
end
class ChildE < Base
end
Output:
ChildA.new.my_method
# Before: 12
# apple
# During: CUSTOM FOO
# aardvark
# After: 12
ChildE.new.my_method
# Before: 12
# (skipped) ChildE#my_method not defined
# After: 12
There are other strange ways to accomplish this with inheritance as well such as
class Base
class << self
attr_accessor :delegate_my_method
def method_added(method_name)
if method_name.to_s == "my_method" && self.name != "Base"
warn "#{self.name}#my_method has been overwritten use delegate_my_method instead"
end
end
end
attr_accessor :foo
def my_method
puts "Before: #{foo}"
original_foo = foo
self.foo= 'CUSTOM FOO'
begin
method(self.class.delegate_my_method.to_s).()
rescue NameError, TypeError
warn "(skipped) #{self.class} method delegation not defined"
end
self.foo = original_foo
puts "After: #{foo}"
end
end
class ChildA < Base
self.delegate_my_method = :delegation_method
def delegation_method
puts 'apple'
puts "During: #{foo}"
puts 'aardvark'
end
end
I could probably keep going with stranger and stranger ways to solve this problem but I think these will get you where you need to go.
One option would be to define a private or nodoc method which the parent class can call and is defined in each of the children.
class Parent
def my_method
block_method do
my_method_behavior
end
end
def block_method
original_foo = #foo
#foo = 'CUSTOM FOO'
yield
#foo = original_foo
end
end
class Child1 < Parent
def my_method_behavior
puts #foo
end
end
class Child2 < Parent
def my_method_behavior
puts #foo
end
end

Creating a method that functions as both a class and instance method

Let's say I had a class and I wanted to be able to call the same method on the class itself and an instance of that class:
class Foo
def self.bar
puts 'hey this worked'
end
end
This lets me do the following:
Foo.bar #=> hey this worked
But I also want to be able to do:
Foo.new.bar #=> NoMethodError: undefined method `bar' for #<Foo:0x007fca00945120>
So now I modify my class to have an instance method for bar:
class Foo
def bar
puts 'hey this worked'
end
end
And now I can call bar on both the class and an instance of the class:
Foo.bar #=> hey this worked
Foo.new.bar #=> hey this worked
Now my class Foo is 'wet':
class Foo
def self.bar
puts 'hey this worked'
end
def bar
puts 'hey this worked'
end
end
Is there a way to avoid this redundancy?
Have one method call the other. Otherwise, no, there is no way to avoid this "redundancy", because there is no redundancy. There are two separate methods that happen to have the same name.
class Foo
def self.bar
puts 'hey this worked'
end
def bar
Foo.bar
end
end

Can I overwrite instance method from module?

I know, I can overwrite class method from module this way
class Foo
class << self
def some_static_method
puts 'some_static_method'
end
end
end
module BAR
class << Foo
def some_static_method
puts 'another_static_method'
end
end
end
class Foo
include BAR
end
Foo.some_static_method # => 'another_static_method'
Is it possible for an instance method?
You can do the following:
class Foo
def self.some_static_method; puts "Hello from Foo" end
end
module Bar
def self.included(base)
base.instance_eval do
def some_static_method; puts "Hello from Bar" end
end
end
end
class Foo
include Bar
end
Foo.some_static_method
This should work
UPDATE
To override instance method use:
class Foo
def some_instance_method; puts "Hello from Foo" end
end
module Bar
def self.included(base)
base.class_eval do
def some_instance_method; puts "Hello from Bar" end
end
end
end
class Foo
include Bar
end
Foo.new.some_instance_method
Your question is actually not about method overriding. It is about what class is referred to within a class ... construction in a module body.
When you do
module Bar
class << Foo
p self
end
end
# => #<Class:Foo>
the << Foo points to the singleton class of the Foo in the main environment because class << Foo cannot define the singleton class directly of a class Foo that has not been defined in advance. So it looks up for Foo that is already defined, and such class is found in the main environment.
When you do
module Bar
class Foo
p self
end
end
# => Bar::Foo
a new class Bar::Foo is created; the Foo points to this Bar::Foo that is newly created, and it does not point to the Foo in the main environment. In order to point to it, you have to explicitly specify that with ::.
module Bar
class ::Foo
p self
end
end
# => Foo
If you are using Ruby > 2.0.0 then what you can use is Module#prepend. Instead of include you can prepend an module and that way all of the module's methods are overriding any existing class instance methods with the same name. You can see a quick example here.
Prior to Ruby 2, Rails had introduced a similar hack: #alias_method_chain
Here is a nice comparison of the two approaches.

Calling Super Methods in Ruby

I am trying to define some classes in Ruby that have an inheritance hierarchy, but I want to use one of the methods in the base class in the derived class. The twist is that I don't want to call the exact method I'm in, I want to call a different one. The following doesn't work, but it's what I want to do (basically).
class A
def foo
puts 'A::foo'
end
end
class B < A
def foo
puts 'B::foo'
end
def bar
super.foo
end
end
Probably, this is what you want?
class A
def foo
puts 'A::foo'
end
end
class B < A
alias bar :foo
def foo
puts 'B::foo'
end
end
B.new.foo # => B::foo
B.new.bar # => A::foo
A more general solution.
class A
def foo
puts "A::foo"
end
end
class B < A
def foo
puts "B::foo"
end
def bar
# slightly oddly ancestors includes the class itself
puts self.class.ancestors[1].instance_method(:foo).bind(self).call
end
end
B.new.foo # => B::foo
B.new.bar # => A::foo

Using methods from two different scopes?

How do I use methods from two different namespaces?
class Bar
def self.configure &block
new.instance_eval &block
end
def method2
puts "from Bar"
end
end
class Foo
def method1
puts "from Foo"
end
def start
Bar.configure do
method1
method2
end
end
end
Foo.new.start
In the above example the method1 can't be called because it is not from the Bar scope. How do I make methods from both scopes callable at the same time?
The trick is to forward missing method calls to the instance of Foo:
class Bar
def self.configure(&block)
o = new
context = eval('self', block.binding)
class << o; self; end.send(:define_method, :method_missing) do |meth, *args|
context.send(meth, *args)
end
o.instance_eval &block
end
def method2
puts "from Bar"
end
end
class Foo
def method1
puts "from Foo"
end
def start
Bar.configure do
method1
method2
end
end
end
Foo.new.start #=> "from Foo"
#=> "from Bar"
Try this:
class Bar
def initialize(foo)
puts "init"
#f = foo
end
def self.configure(foo, &block)
new(foo).instance_eval &block
end
def method2
puts "from Bar"
end
end
class Foo
def method1
puts "from Foo"
end
def start
Bar.configure(self) do
#f.method1
method2
end
end
end
This makes #f a class level instance variable of Bar, which is set when you initialize an object of Bar using new(foo) in Bar.configure. The block being passed assumes the existence of #f, which contains a reference to the object of class Foo.
It's a convoluted way of doing this - I can't think of anything better though. It'd be interesting to know the use case.
Maybe, this is the easiest way. It doesn't need to modify Bar.
class Bar
def self.configure &block
new.instance_eval &block
end
def method2
puts "from Bar"
end
end
class Foo
def method1
puts "from Foo"
end
def start
foo = self # add
Bar.configure do
foo.method1 # modify
method2
end
end
end
Foo.new.start
I would simplify your code as follows:
class Bar
def self.configure &block
obj = new
block.call(obj)
obj
end
def method2
puts "from Bar"
end
end
class Foo
def method1
puts "from Foo"
end
def start
Bar.configure do |obj|
method1
obj.method2
end
end
end
Foo.new.start
Block logic is clean and implementation doesn't require context switching. You are using the standard ruby functionality of passing parameters to the block.

Resources