Hi All,
I have a code:
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()
which produces:
From method2 of ClassA
From method1 of ClassB
From method2 of ClassB
Now, the most important, is that in method2 of ClassA the method1 is called and it is taken from ClassB but I need it from ClassA, i.e. I need the output like this:
From method2 of ClassA
From method1 of ClassA
From method2 of ClassB
How can I implement this?
Thank you!
Regards,
Yury Vishnevskiy
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
···
On 19 Aug 2018, at 22:33, Yury V. Vishnevskiy <YVVishnevsky@yandex.ru> wrote:
Hi All,
I have a code:
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()
which produces:
From method2 of ClassA
From method1 of ClassB
From method2 of ClassB
Now, the most important, is that in method2 of ClassA the method1 is called and it is taken from ClassB but I need it from ClassA, i.e. I need the output like this:
From method2 of ClassA
From method1 of ClassA
From method2 of ClassB
How can I implement this?
Thank you!
Regards,
Yury Vishnevskiy
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
BTW, instead of `ClassA.instance_method(:method1)` you can do: `method(:method1).super_method.call`
···
On 19 Aug 2018, at 22:41, Dmitriy Non <non.dmitriy@gmail.com> wrote:
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
On 19 Aug 2018, at 22:33, Yury V. Vishnevskiy <YVVishnevsky@yandex.ru> wrote:
Hi All,
I have a code:
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()
which produces:
From method2 of ClassA
From method1 of ClassB
From method2 of ClassB
Now, the most important, is that in method2 of ClassA the method1 is called and it is taken from ClassB but I need it from ClassA, i.e. I need the output like this:
From method2 of ClassA
From method1 of ClassA
From method2 of ClassB
How can I implement this?
Thank you!
Regards,
Yury Vishnevskiy
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
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
···
On Sun, Aug 19, 2018 at 9:41 PM Dmitriy Non <non.dmitriy@gmail.com> wrote:
But there obviously something wrong with your design so I suggest you
fix it instead of using something like code above.
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/