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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:11:20+00:00 2026-06-17T06:11:20+00:00

Here are the Ruby classes I have: class MyBase class << self def static_method1

  • 0

Here are the Ruby classes I have:

class MyBase
  class << self
     def static_method1
        @@method1_var ||= "I'm a  base static method1"
     end

     def static_method1=(value)
        @@method1_var = value
     end

     def static_method2
        @@method2_var ||= "I'm a base static method2"
     end

     def static_method2=(value)
        @@method2_var = value
     end
  end

  def method3
    MyBase::static_method1
  end
end

class MyChild1 < MyBase
end

class MyChild2 < MyBase
  class << self
     def static_method1
        @@method1_var ||= "I'm a child static method1"
     end
  end
end

c1 = MyChild1.new
puts c1.method3 #"I'm a  base static method1" - correct


c2 = MyChild2.new
puts c2.method3  # "I'm a  base static method1" - incorrect. I want to get "I'm a child static method1"

I’m aware of attr_accessor and modules, but I can’t use use them here because I want them to give default values in MyBase class. I want to override MyBase.static_method1 in MyChild2.

  • 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-17T06:11:21+00:00Added an answer on June 17, 2026 at 6:11 am

    The problem is that method3 is always explicitly calling the method on the base class. Change it to this:

    def method3
      self.class.static_method1
    end
    

    After that, consider not using @@.

    @@ in ruby is extremely counterintuitive and rarely means what you think it means.

    The problem with @@ is that it is shared across the all of the inherited classes and the base class. See this blog post for an explanation.

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

Sidebar

Related Questions

I have these model classes: class Song < ActiveRecord::Base has_many :urls, as: :media has_one
Here's a classic fizzbuzz in Ruby: class PrimeChecker def print_em 1.upto 100 do |fizzbuzz|
I have been looking at ways to program a UI in ruby here, on
Complete new person to Ruby and Rails here... Have tried some tutorials in the
I have a Ruby application, and here is my setup: I have this HAML
So, lets say I have two nearly identical classes in C# and Ruby: C#
In Ruby, I have a DAO class, which is extended by a class that
In Ruby 1.9.3 I need to create a few class instances which each have
I've been learning Ruby for a class and have been writing a sample game.
I have got two classes class Claim include Mongoid::Document embeds_many :claim_fields belongs_to :user ...

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.