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

  • SEARCH
  • Home
  • 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 3949576
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:30:35+00:00 2026-05-20T01:30:35+00:00

I am working on a small rails app and have a problem with ruby’s

  • 0

I am working on a small rails app and have a problem with ruby’s OOP model. I have the following simplified class structure.

class Foo
  protected
    @bar = []
    def self.add_bar(val)
      @bar += val
    end
    def self.get_bar
      @bar
    end
end

class Baz < Foo
  add_bar ["a", "b", "c"]
end

My problem is now, that when I call add_bar in the class definition of Baz, @bar is apparently not initialized and I get an error that the + Operator is not available for nil. Calling add_bar on Foo directly does not yield this problem. Why is that and how can I initialize @bar correctly?

To make clear what I want, I will point out the behavior I would expect from these classes.

Foo.add_bar ["a", "b"]
Baz.add_bar ["1", "2"]
Foo.get_bar # => ["a", "b"]
Baz.get_bar # => ["a", "b", "1", "2"]

How could I achieve this?

  • 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-20T01:30:36+00:00Added an answer on May 20, 2026 at 1:30 am

    Short answer: instance variables don’t get inherited by subclasses

    Longer answer: the problem is that you wrote @bar = [] in the body of the class (outside any method). When you set an instance variable, it is stored on whatever is currently self. When you’re in a class body, self is the class object Foo. So, in your example, @foo gets defined on the class object Foo.

    Later, when you try to look up an instance variable, Ruby looks in whatever is currently self. When you call add_bar from Baz, self is Baz. Also self is STILL Baz in the body of add_bar (even though that method is in Foo). So, Ruby looks for @bar in Baz and can’t find it (because you defined it in Foo).

    Here’s an example that might make this clearer

    class Foo
      @bar = "I'm defined on the class object Foo. self is #{self}"
    
     def self.get_bar
        puts "In the class method. self is #{self}"    
        @bar
      end
    
      def get_bar
        puts "In the instance method. self is #{self} (can't see @bar!)"
        @bar
      end
    end
    
    >> Foo.get_bar
    In the class method. self is Foo
    => "I'm defined on the class object Foo. self is Foo"
    
    >> Foo.new.get_bar
    In the instance method. self is #<Foo:0x1056eaea0> (can't see @bar!)
    => nil
    

    This is admittedly a bit confusing, and a common stumbling point for people new to Ruby, so don’t feel bad. This concept finally clicked for me when I read the ‘Metaprogramming’ chapter in Programming Ruby (aka “The Pickaxe”).

    How I’d solve your problem: Look at Rails’ class_attribute method. It allows for the sort of thing you’re trying to do (defining an attribute on a parent class that can get inherited (and overidden) in its subclasses).

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

Sidebar

Related Questions

I recently started working on a small personal project in Ruby on Rails, and
Working live URL showing problem: http://69.24.73.172/demos/newDemo/test.html The HTML : <div class="small-vote"> <a href="#" class="s
I'm working on a small app where I can generate a list of barcodes.
I'm working in a small company and weeks away from deploying a web-app that
I am creating a small rails app for personal use and would like to
we are currently working on a rails project that uses i18n and we have
I am working on a small script using the USB bindings for ruby. Since
I have been working on this, very, small photo cms lately. And now i
I am developing a small rails app to serve fonts to other sites say
I'm working on a small Rails application in which I'd like users to be

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.