Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6543929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:24:19+00:00 2026-05-25T11:24:19+00:00

So I have many child classes that will inherit from a parent class. I’ve

  • 0

So I have many child classes that will inherit from a parent class. I’ve been playing around with instance variables @ and class variables @@ and I have yet to be able to achieve with them what I want. What I want actually works with the code below but it doesn’t seem DRY at all. Any suggestions on how I can refactor this?

class Planet
    def has_color?(color)
        self.color == color
    end

    def has_position?(position)
        self.position == position
    end
end

class Mars < Planet
    def color
        "red"
    end 

    def position
        4
    end
end

class Earth < Planet
    def color
        "blue"
    end

    def position
        3
    end
end

What I hope to achieve

>> Mars.has_color?("red")
true

>> Earth.position
3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:24:19+00:00Added an answer on May 25, 2026 at 11:24 am

    is this useful for you ? I have tried , it works in Ruby1.9.2

        class Planet
            def self.has_color?(color)
                @color == color
            end
    
            def self.has_position?(position)
                @position == position
            end
    
            def self.position
                @position
            end
    
            def self.color
                @color
            end
        end
    
        class Mars < Planet
                @color="red"
                @position=4
        end
    
        class Earth < Planet
                @color="blue"
                @position=3
        end
    
        puts Mars.has_color?("red")
        puts Mars.has_position?("3")
        puts Earth.position
        puts Earth.color
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one abstract class and many child classes. In child classes are from
If I have a class with many child classes and I use ObjectSerializer.SerializeObject will
I have a base class DockedToolWindow : Form, and many classes that derive from
I have a parent project that is branched to many dependent child projects. I
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have an abstract immutable base class that defines enforces child classes to be
I have two classes: Parent and Child , mapped one-to-many with relationship owned by
I have a parent class called Exam and that has many instances of a
I have a bi-directional one-to-many relationship defined between Parent and Child classes. I'm looking
Lets say I have two tables - child and parent with many-to-one relation. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.