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 8860255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:13:50+00:00 2026-06-14T15:13:50+00:00

How to implement inheritance in ruby for the following? class Land attr_accessor :name, :area

  • 0

How to implement inheritance in ruby for the following?

class Land
  attr_accessor :name, :area
  def initialize(name, area)
    @name = name
    @area = area
  end
end

class Forest < Land
  attr_accessor :rain_level
  attr_reader :name

  def name=(_name)
    begin
      raise "could not set name"
    rescue  Exception => e
            puts e.message  
        end
  end

  def initialize(land, rain_level)
    @name = land.name
    @rain_level = rain_level
  end
end

l = Land.new("land", 2300)
f = Forest.new(l, 400)
puts f.name # => "land"    

suppose when i change name for land l, then it should change for sub class also

l.name ="new land"
puts f.name # => "land"

what expected is puts f.name # => “new land”

  • 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-06-14T15:13:51+00:00Added an answer on June 14, 2026 at 3:13 pm

    This is kind of an interesting thing you want to build.

    Summarizing you want to have two objects that share a value but only one is allowed to edit the value, the other one is only allowed to read it.

    I think the easiest way to implement this is in your case to implement a new getter in Forest which returns land.name. By writing l.name = 'meow' will f.name return moew too because it holds a reference to l.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How the compilers implement the virtual inheritance? In the following code: class A {
I'm trying to implement class inheritance hieararchy with NHibernate and SQL server. I made
I am trying to implement Inheritance in VBA in the following way - I
In the following code i am trying to implement inheritance and polymorphism properties,My question
So far I have used the following statements in SQLAlchemy to implement table inheritance
I'm trying to implement abstract inheritance in Django with the following code, but it
I'm trying to implement a TPC inheritance model in EF 4.3 CodeFirst for an
I'm experimenting with TextArea and inheritance to implement some additional functionality on the protected
Are Composition and Inheritance the same? If I want to implement the composition pattern,
I am trying to clarify the concept of runtime dynamic binding and class inheritance

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.