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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:04:35+00:00 2026-05-31T17:04:35+00:00

So I create a class in ruby: class User def initialize end end Now

  • 0

So I create a class in ruby:

class User
  def initialize
  end
end

Now say I want to create a attribute that is a hash with a getter/setter, I’m confused as to the options I have doing this.

If I do this:

class User
  attr_accessor :some_hash
end

But I don’t want this hash to be nil ever, always an empty hash.

I’m confused when I should do:

def some_hash
  self.some_hash ||= {}
end

and do:

def some_hash
  @some_hash ||= {}
end

What is the difference?

If I don’t use the attr_accessor, I have to create both the reader and writer (or getter/setter):

def some_hash=()
end

def some_hash
end

I’m hoping someone can clear up the options I would have for creating a some_hash attribute which is a hash, and that never returns a nil if it is empty.

i.e. using attr_accessor, manually creating the methods, and finally when to use @some_hash and self.some_hash

  • 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-31T17:04:37+00:00Added an answer on May 31, 2026 at 5:04 pm

    attr_accessor :some_hash defines reader and writer methods for the given attribute. It is equivalent to this:

    class User
      def some_hash
        @some_hash
      end
    
      def some_hash=(a_hash)
        @some_hash = a_hash
      end
    end
    

    @some_hash refers to an instance variable of the object, while some_hash and some_hash= are methods. The former returns the value of the variable, the latter sets it.

    The idiom self.some_hash ||= {} is equivalent to self.some_hash || self.some_hash = {}.

    Boolean operators in Ruby short circuit, which means the second expression (self.some_hash = {}) will not be executed at all if the first expression (self.some_hash) returns a true value.

    The method:

    def some_hash
      self.some_hash ||= {}
    end
    

    Is actually recursive, since it expands to some_hash || self.some_hash = {}. It will keep calling itself until you get a stack overflow. Use the second form:

    def some_hash
      @some_hash ||= {}
    end
    

    Since it sets the instance variable directly, you will not have problems with recursion, nor will you have to call a writer method. some_hash can never return nil, because if @some_hash is nil, it will be assigned an empty hash before the method returns.

    By the way, this is also called lazy initialization, because the variable is initialized when it is first accessed, not when the User instance is created.

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

Sidebar

Related Questions

I want to create a class that will mainly house a Vector . The
I'm trying to create a User model for my Ruby on Rails app that
I'm on Rails 2.3.5. In a typical user controller create action class UsersController def
I am trying to create a Ruby class where the initialize method takes a
I am testing this controller: class SessionsController < ApplicationController def new end def create
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
I need to create class Dog and PurebredDog extending Dog. Problem is that Dog
My create user method in the users_controller.rb looks like: def process_login is_login_valid(params[:user][:user_name], params[:user][:password]) if
I create class libraries, some which are used by others around the world, and
How do I create class (i.e. static ) variables or methods in Python?

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.