Dmitriy Non
2018-08-19 19:41:40 UTC
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
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
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>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>