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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:34:07+00:00 2026-06-18T01:34:07+00:00

Where is instance variable initialized as nil first time? Can I redefine it as

  • 0

Where is instance variable initialized as nil first time?
Can I redefine it as other value by default for all instances?

For example:

class Class
  #some code here or maybe in an Object class
end

class Foo1
  attr_accessor :bar
end

class Foo2
  attr_accessor :bar
end

p Foo1.new.bar # result is not nil
p Foo2.new.bar # result is not nil

This can be done by modifying the reader:

class Class
  def attr_accessor(attr_name)
    ...
    define_method "#{attr_name}" do
      if instance_variable_get "@#{attr_name}_history"
        instance_variable_get "@#{attr_name}_history"
      else
        "Not nil"
      end
    end
    ...
  end
end

But this doesn’t help in understanding the core of Ruby.
Many thanks!

  • 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-18T01:34:08+00:00Added an answer on June 18, 2026 at 1:34 am

    Define a new method in class Class. Get instance variables through :instance_variables and set them to anything you like by using :instance_variable_set(:@var,default_value)

    class Class
    
        alias oldNew new
            def new(*args)
            result = oldNew(*args)
            default = 2354 # set default here
            a = result.instance_variables
            a.each do
            |d|
            result.instance_variable_set(d,default)
            end
    
        return result
        end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I access instance variable from singleton method? class Test def initialize(a) @a
Say I have an instance variable MyObject that has been allocated and initialized. Then
I have an instance variable called @filtered_ratings = params[:ratings].keys However, if params[:ratings] is nil
In java can an instance variable and a method have the same name without
Can anyone tell me if the NSString instance variable planetName needs to be allocated
In Ruby, can one object destroy another? For example: class Creature def initialize @energy
cqq.rb:96 **warning: Statement not reached.** /root/newpackage/lib.rb:727 **warning: instance variable @object not initialized** Error: Your
Write the definition of a class Player containing: An instance variable name of type
so i need a NSDocument with a backend instance that all views can access
I have an instance variable NSMutableArray* searchResults. First, I initialize it: self.searchResults = [[NSMutableArray

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.