Discussion:
Method invocation from parent class
Dmitriy Non
2018-08-19 19:41:40 UTC
Permalink
You can achieve it via

def method2
ClassA.instance_method(:method1).bind(self).call
end

But there obviously something wrong with your design so I suggest you
fix it instead of using something like code above.

If you don't want your method to be overridden then just don't.
Do something like:


class A
def method1
some_private_method
end

def method2
puts 'A#method2'
some_private_method
end

private

def some_private_method
puts 'whatever'
end
end

class B < A
def method1
puts 'B#method1'
end
end

B.new.method2
Hi All,
class ClassA
def method1()
puts("From method1 of ClassA")
end
def method2()
puts("From method2 of ClassA")
method1() // <---- Here is the problem, I would like to call method1 from the same ClassA, although it is applied to instance of ClassB!
end
end
class ClassB < ClassA
def method1()
puts("From method1 of ClassB")
end
def method2()
super()
puts("From method2 of ClassB")
end
end
b = ClassB.new()
b.method2()
From method2 of ClassA
From method1 of ClassB
From method2 of ClassB
From method2 of ClassA
From method1 of ClassA
From method2 of ClassB
How can I implement this?
Thank you!
Regards,
Yury Vishnevskiy
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Dmitriy Non
2018-08-19 19:43:57 UTC
Permalink
BTW, instead of `ClassA.instance_method(:method1)` you can do: `method(:method1).super_method.call`
Post by Dmitriy Non
You can achieve it via
def method2
ClassA.instance_method(:method1).bind(self).call
end
But there obviously something wrong with your design so I suggest you
fix it instead of using something like code above.
If you don't want your method to be overridden then just don't.
class A
def method1
some_private_method
end
def method2
puts 'A#method2'
some_private_method
end
private
def some_private_method
puts 'whatever'
end
end
class B < A
def method1
puts 'B#method1'
end
end
B.new.method2
Hi All,
class ClassA
def method1()
puts("From method1 of ClassA")
end
def method2()
puts("From method2 of ClassA")
method1() // <---- Here is the problem, I would like to call method1 from the same ClassA, although it is applied to instance of ClassB!
end
end
class ClassB < ClassA
def method1()
puts("From method1 of ClassB")
end
def method2()
super()
puts("From method2 of ClassB")
end
end
b = ClassB.new()
b.method2()
From method2 of ClassA
From method1 of ClassB
From method2 of ClassB
From method2 of ClassA
From method1 of ClassA
From method2 of ClassB
How can I implement this?
Thank you!
Regards,
Yury Vishnevskiy
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Yury V. Vishnevskiy
2018-08-19 21:44:37 UTC
Permalink
Thank you, Dmitriy!
Post by Dmitriy Non
BTW, instead of `ClassA.instance_method(:method1)` you can do: `method(:method1).super_method.call`
Post by Dmitriy Non
You can achieve it via
def method2
ClassA.instance_method(:method1).bind(self).call
end
But there obviously something wrong with your design so I suggest you
fix it instead of using something like code above.
If you don't want your method to be overridden then just don't.
class A
def method1
some_private_method
end
def method2
puts 'A#method2'
some_private_method
end
private
def some_private_method
puts 'whatever'
end
end
class B < A
def method1
puts 'B#method1'
end
end
B.new.method2
Hi All,
class ClassA
def method1()
puts("From method1 of ClassA")
end
def method2()
puts("From method2 of ClassA")
method1() // <---- Here is the problem, I would like to call method1 from the same ClassA, although it is applied to instance of ClassB!
end
end
class ClassB < ClassA
def method1()
puts("From method1 of ClassB")
end
def method2()
super()
puts("From method2 of ClassB")
end
end
b = ClassB.new()
b.method2()
From method2 of ClassA
From method1 of ClassB
From method2 of ClassB
From method2 of ClassA
From method1 of ClassA
From method2 of ClassB
How can I implement this?
Thank you!
Regards,
Yury Vishnevskiy
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Robert Klemme
2018-08-20 15:15:25 UTC
Permalink
Post by Dmitriy Non
But there obviously something wrong with your design so I suggest you
fix it instead of using something like code above.
I agree. This is suspicious. For this discussion it would be helpful
if methods are a bit more concrete than "method1" and "method2". We
might be able to come up with much better advice if we knew the real
scenario.

Kind regards

robert
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Loading...