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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:19:53+00:00 2026-06-18T00:19:53+00:00

When accessing an instance variable via accessor method attribute , what is the difference

  • 0

When accessing an instance variable via accessor method attribute, what is the difference between the expressions self.attribute and attribute? Say, we define an accessor:

def post
  @post
end

We can call

self.post

or simply

post

What is special about adding self?

  • 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-18T00:19:54+00:00Added an answer on June 18, 2026 at 12:19 am

    It makes a difference when there might be a local var that shadows method call. Using self allows us to specify that we want the method, not the local var. See an example:

    class Foo
      def post
        @post
      end
    
      def post= (content)
        @post = content
      end 
    
      def test
        #difference 1 
        p post # >> nil
    
        @post = 10
        p post # >> 10
    
        post = 42
        p post # >> 42
        p self.post # >> 10
    
        #difference 2
        # assign to @post, note that you can put space between "self.post" and "="
        self.post = 12 
    
        #otherwise it means assigning to a local variable called post.
        post = 12
      end
    end
    
    Foo.new.test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am some what confused as to the difference between accessing an instance variable
When accessing the winapi method CloseHandle() via .net P/Invoke, should the argument be IntPtr
I am accessing object of one class in the another class. But instance variable
I am trying to have a private instance variable and use a getter method
I know it's possible to define public instance variable with @public keyword. However, Objective-C
Just curious what the best practice is for accessing an instance variable from within
I am wondering at the difference between declaring a variable as volatile and always
I wonder if there is any difference in performance when accessing a class variable
Is there no magic python way of accessing the instance of the class that
I am accessing an XML API (flight search comparison) via a PHP Proxy like

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.