Q: How to call parent method explistly?

M

makoto kuwata

Is it possible to call parent method specifying method name explicitly
like super.foobar() or parent::foobar()?

class Parent
def hello(name)
puts "Hello #{name}!"
end
end

class Child
def hello(name)
# ...
_hello(name)
# ...
end
private
def _hello(name)
# ...
super(name) # want to call Parent#hello(), not _hello()
# ...
end
end
 
J

John Mair

It's ugly but this works (i assume Child subclasses Parent?)

class Child < Parent
def _hello(name)
Parent.instance_method:)hello).bind(self).call(name)
end
end


makoto kuwata wrote in post #983231:
 
G

Gary Wright

Is it possible to call parent method specifying method name explicitly
like super.foobar() or parent::foobar()?

There are a couple of alternatives.

1) Create an alias of the parent's method and call it instead:

class Child < Parent
alias parent_hello hello
def _hello(name)
parent_hello(name)
end
end

2) use a method object to reference the parent's method:

class Child < Parent
def parent_hello(*args)
Parent.instance_method:)hello).bind(self).call(*args)
end

def _hello(name)
parent_hello(name)
end
end


Gary Wright
 
R

Robert Klemme

There are a couple of alternatives.

1) Create an alias of the parent's method and call it instead:

class Child < Parent
=A0alias parent_hello hello
=A0def _hello(name)
=A0 =A0parent_hello(name)
=A0end
end

2) use a method object to reference the parent's method:

class Child < Parent
=A0def parent_hello(*args)
=A0 =A0Parent.instance_method:)hello).bind(self).call(*args)
=A0end

=A0def _hello(name)
=A0 =A0parent_hello(name)
=A0end
end

Folks, why are you making this so complicated? Nobody mentions the
standard way of doing this by using "super":

class Parent
def hello_same_args(a)
puts a
end

def hello_different_args
puts "We don't need args"
end
end

class Child < Parent
def hello_same_args(a)
super
puts "now in child"
end

def hello_different_args(x)
super()
puts "Child got #{x}"
end
end

irb(main):022:0> Child.new.hello_same_args "ARG"
ARG
now in child
=3D> nil
irb(main):023:0> Child.new.hello_different_args "ARG"
We don't need args
Child got ARG
=3D> nil

Only if you want to call a _different_ method in the super class you
need to use one of the other approaches. But if you think about it,
usually this is not needed.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
G

Gary Wright

Folks, why are you making this so complicated? Nobody mentions the
standard way of doing this by using "super": [...]
Only if you want to call a _different_ method in the super class you
need to use one of the other approaches. But if you think about it,
usually this is not needed.

The original poster specifically asked about calling a different
method in the parent that was also redefined in the child,
which is why I didn't show the use of super. From the original
message:
super(name) # want to call Parent#hello(), not _hello()


Gary Wright
 
R

Robert Klemme

Folks, why are you making this so complicated? Nobody mentions the
standard way of doing this by using "super": [...]
Only if you want to call a _different_ method in the super class you
need to use one of the other approaches. But if you think about it,
usually this is not needed.

The original poster specifically asked about calling a different
method in the parent that was also redefined in the child,
which is why I didn't show the use of super. From the original
message:
super(name) # want to call Parent#hello(), not _hello()

Ah, that was the piece I missed. Sorry for the noise.

I'd still question the wisdom of such a design though.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top