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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:11:00+00:00 2026-06-10T04:11:00+00:00

Can someone explain the difference between initializing self and having @variables when defining classes?

  • 0

Can someone explain the difference between initializing “self” and having @variables when defining classes?

Here’s an example

class Child < Parent
  def initialize(self, stuff):
    self.stuff = stuff
    super()
  end
end

So in this case, wouldn’t I be able to replace self.stuff with @stuff? What’s the difference? Also, the super() just means whatever is in the Parent initialize method the Child should just inherit it right?

  • 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-10T04:11:01+00:00Added an answer on June 10, 2026 at 4:11 am

    In general, no, self.stuff = stuff and @stuff = stuff are different. The former makes a method call to stuff= on the object, whereas the latter directly sets an instance variable. The former invokes a method which may be public (unless specifically declared private in the class), whereas the latter is always setting a private instance variable.

    Usually, they look the same because it is common to define attr_accessor :stuff on classes. attr_accessor is roughly equivalent to the following:

    def stuff
      @stuff
    end
    
    def stuff=(s)
      @stuff = s
    end
    

    So in that case, they are functionally identical. However, it is possible to define the public interface to allow for different results and side-effects, which would make those two “assignments” clearly different:

    def stuff
      @stuff_called += 1    # Keeps track of how often this is called, a side effect
      return @stuff
    end
    
    def stuff=(s)
      if s.nil?             # Validation, or other side effect. This is not triggered when setting the instance variable directly
        raise "Argument should not be nil"
      end
      @stuff = s
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain the difference between using a $ when defining variables in jQuery
Can someone please explain with example that I can understand about the difference between
Can someone explain me the difference between Class.forName() and Thread.currentThread().getContextClassLoader().loadClass(). I have next code
Can someone explain to me what the difference between include_once 'classb.php' class A {
Can someone explain the difference between a class and a module. When do you
can someone explain me the difference between those 2 classes? Why to use satic
Can someone explain the difference between these two, the first one is taken from
Can someone explain the difference between apache mod_wsgi in daemon mode and django fastcgi
Can someone explain the difference between static NSString* CellIdentifier = @Cell; and NSString *CellIdentifier
Can someone please explain to me difference between extends and super in java generics

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.