Solved

Method Inheritance / Overriding in Ruby

Posted on 2014-09-24
5
182 Views
Last Modified: 2014-09-25
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.
0
Comment
Question by:MaglinFurniture
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40343101
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
 

Author Comment

by:MaglinFurniture
ID: 40343879
@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
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40344188
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
 

Author Comment

by:MaglinFurniture
ID: 40344482
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40344510
Not a problem, glad to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question