Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Method Inheritance / Overriding in Ruby

Posted on 2014-09-24
5
Medium Priority
?
192 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Integration Management Part 2
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

972 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