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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:09:30+00:00 2026-05-28T07:09:30+00:00

I thought that this question ( How to do attr_accessor_with_default in ruby? ) answered

  • 0

I thought that this question ( How to do attr_accessor_with_default in ruby? ) answered my question, but I’m not using ActiveRecord and after_initialize depends on it.

What’s the Ruby best practice for implementing a default value for an attr_accessor? Is this the closest thing to documentation about it? Should I stop using attr_accessor since it is private?

  • 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-28T07:09:31+00:00Added an answer on May 28, 2026 at 7:09 am
    class Foo
      # class-level instance variable
    
      # setting initial value (optional)
      @class_var = 42  
    
      # making getter/setter methods on the class itself
      class << self
        attr_accessor :class_var
      end
    
      # instance-level variable getter/setter methods
      attr_accessor :inst_var
      def initialize
        # setting initial value (optional)
        @inst_var = 17
      end
    end
    
    p Foo.class_var
    #=> 42
    Foo.class_var = 99
    p Foo.class_var
    #=> 99
    
    f1 = Foo.new
    f2 = Foo.new
    f2.inst_var = 1
    p [f1.inst_var, f2.inst_var]
    #=> [17,1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this question and thought that would easily be solved (not that it
I read some of the discussion in this question and thought to myself that
This is a question that's been nagging me for some time. I always thought
I would have thought that this would be an easy thing to do, but
I thought I heard that py2exe was able to do this, but I never
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
After I thought that I've understood how they work, I tried this: NSString *str1
I always thought that *&p = p = &*p in C. I tried this
The comment to this answer got me wondering. I've always thought that C was
This comment confuses me: kill -l generally lists all signals. I thought that a

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.