Solved

Method Inheritance / Overriding in Ruby

Posted on 2014-09-24
5
169 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
  • 3
  • 2
5 Comments
 
LVL 62

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 62

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 62

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now