Method Inheritance / Overriding in Ruby

Hi

I am curious to know how to do something like this in ruby:

class One
def do_something
v1=5
v2=6
puts(v1,v2)
end
end

Class Two < One
def do_something
v2=7
end
end

printit=Two.new.do_something

where the result is that, instead of 5 and 6 being output, 5 and 7 are output. In this example,  nothing happens.
MaglinFurnitureAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi MaglinFurniture;

Try it like this.

class One
  # Creates a get and set accessor methods
  # to access @v1 and @v2 from outside the class
  attr_accessor :v1, :v2
	
  # Initializes the object @v1 and @v2 with the values
  # passed in on instantiation of the object or if nothing is
  # passed in sets @v1 and @v2 to zero
  def initialize( ciV1 = 0, ciV2 = 0 ) 
    @v1 = ciV1
    @v2 = ciV2
  end
	 
  def do_something
    puts(@v1,@v2)
  end
	
end

class Two < One

  def initialize( ciV1, ciV2 )
    # Passes the values to the parent class to 
    # initialize @v1 and @v2
    super( ciV1, ciV2 )
  end
	
  # When calling this method a value for @v2 
  # needs to be passed in.
  def do_something( newV2 )
    @v2 = newV2
    puts( @v1, @v2 )
  end
end

# Creates an instance of the class Two
two = Two.new(5, 6)
# Calls Class Two do_something with a value of 7
printit = two.do_something(7)

Open in new window

0
MaglinFurnitureAuthor Commented:
@Fernando Soto

Hi,

Thanks for your reply!

First, I tried your code and it works fine

Second, i adapted your code to my specific situation and got no results. In pondering why this might be I notice that you have a call to puts() in the second definition of do_something() you have in Two. I'm thinking that the call to puts() should not be necessary if inheritance is working. If I remove the call in your code, I also get no results when running it. do_something() in my specific application has code which makes use of the values being set/passed. The code is defined in the parent object method, and  should be accessible from the child through inheritance. Therefore, there should be no need to repicate it in the second definition of do_something().

With respect to your code, therefore, why is it necessary to replecate the call to puts()?

Thanks!
0
Fernando SotoRetiredCommented:
Hi MaglinFurniture;

To your statement, "Second, i adapted your code to my specific situation and got no results.", it would be difficult to say without seeing the actual code.

To your statement, "In pondering why this might be I notice that you have a call to puts() in the second definition of do_something() you have in Two. I'm thinking that the call to puts() should not be necessary if inheritance is working. ", OK, I was trying to answer your question where you placed do_something in class Two and changed a @v2 and wanted to see the results as 5 and 7 being printed out. If you do not place a do_something in class Two then it will be calling the do_something of class One which if called in that way will not have change v2 to be 7 and it would not have printed out 5 and 7 as you stated you were expecting to see but would have printed out 5 and 6.

To your statement, "If I remove the call in your code, I also get no results when running it.", I am assuming you mean the call to puts. That would be correct. This is due to the fact that you have overrode the do_something of class One in class Two and you are NOT putting out a message. In a setup like that it is NOT calling the do_something of class One because it already found one do_something in the class Two. You are overriding the do_something method.

To your statement, "do_something() in my specific application has code which makes use of the values being set/passed. The code is defined in the parent object method, and  should be accessible from the child through inheritance. Therefore, there should be no need to repicate it in the second definition of do_something().",
You are correct in stating that the variables should be accessible from the child class and it is if you take a look at @v2 in the do_something of class Two @v2 is defined in the superclass.

Modify the already posted code with these changes.

class Two < One

  def initialize( ciV1, ciV2 )
    # Passes the values to the parent class to 
    # initialize @v1 and @v2
    super( ciV1, ciV2 )
  end
	
end

# Creates an instance of the class Two
two = Two.new(5, 6)
# Modify the value of v2 through the class Two
two.v2 = 7
# Calls do_something of class One through class Tqo
printit = two.do_something

Open in new window


To your question, "With respect to your code, therefore, why is it necessary to replecate the call to puts()?", because the do_something of class One is not being called and therefore if you want to display something you need to use the puts or you could call the do_something of class One by using the super keyword in the do_something of class Two, the super keyword will execute the code in a method in a superclass having the same name..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MaglinFurnitureAuthor Commented:
Hi Fernando,

Ok, thanks very much.

The code I'm using is for a Plugin for Sketchup. I'm learning Ruby to, possibly, write, or modify Plugins and Extensions for use with Sketchup. The "do_something" method just draws things in Sketchup based on parameters: a set of steps, a fence, or a wall. It is just an exercise in learning about Ruby Plugins for Sketchup.
0
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Ruby

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.