Inheritance in ruby

Hi, all.

I’m creating an automation framework, and met this inheritance
private/protected problem.
Say I’m having SshCon and LinuxSshCon
class SshCon
def connect
cmd()
end
def cmd
end
def search
cmd("")
end
end

class LinuxSshCon < SshCon
def cmd
format()
super()
end
end

lsc = LinuxSshCon.new
lsc.connect # here it would use the cmd in LinuxSshCon, and cause me
some trouble
lsc.cmd

My problem is this:
When I overwrite a method in sub class, is there a way I can keep the
original class calling its own method?
I tried
def connect
self.cmd
end
or use private/protected to claim cmd, none of them works

thanks

JarodZZ

On Wed, Oct 19, 2011 at 3:16 PM, salamond [email protected] wrote:

My problem is this:
When I overwrite a method in sub class, is there a way I can keep the
original class calling its own method?

There is a way (line 15) but it’s horrible:

irb(main):001:0> class Base
irb(main):002:1> def a;“base”;end
irb(main):003:1> def b;a;end
irb(main):004:1> end
=> nil
irb(main):005:0> class Derived < Base
irb(main):006:1> def a;“derived”;end
irb(main):007:1> end
=> nil
irb(main):008:0> Base.new.a
=> “base”
irb(main):009:0> Derived.new.a
=> “derived”
irb(main):010:0> class Base
irb(main):011:1> end
=> nil
irb(main):012:0> Derived.new.b
=> “derived”
irb(main):013:0> Base.new.b
=> “base”
irb(main):014:0> class Base
irb(main):015:1> def b;Base.instance_method(:a).bind(self).call;end
irb(main):016:1> end
=> nil
irb(main):017:0> Base.new.b
=> “base”
irb(main):018:0> Derived.new.b
=> “base”

In short: you don’t want to do this. Rather lay out your methods in a
way that it’s not necessary. Btw, it’s similar in other languages.

Kind regards

robert

On Oct 19, 2011, at 06:16 , salamond wrote:

When I overwrite a method in sub class, is there a way I can keep the
original class calling its own method?

This is a bad design smell. Try to design your code so it isn’t
necessary.

On 10/19/2011 09:16 AM, salamond wrote:

end
end

lsc = LinuxSshCon.new
lsc.connect # here it would use the cmd in LinuxSshCon, and cause me
some trouble
lsc.cmd

I agree with another poster on this thread: the above has serious code
stink. Why does LinuxSshCon override cmd() if that is a problem for the
parent class? It seems that what cmd() is doing in your LinuxSshCon
class needs to be split up into one or more additional methods.

My problem is this:
When I overwrite a method in sub class, is there a way I can keep the
original class calling its own method?
I tried
def connect
self.cmd
end
or use private/protected to claim cmd, none of them works

Go back and revisit your design. If the method ON the base class should
only invoke the base class method then your child shouldn’t be
overriding it.

thanks, Robert, Ryan and Darry, I agree it’s a design problem.

I know if I choose to include an object SshCon in LinuxSshCon, not
inheritance.
Then I won’t have any of these problems.

I’ll look into some OO books, to get a better design next time. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs