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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:52:31+00:00 2026-05-26T02:52:31+00:00

When I set or get instance variables using some name, for example @foo ,

  • 0

When I set or get instance variables using some name, for example @foo, I can do something like:

instance_variable_set(“@foo”, some_value)
…
instance_variable_get(“@foo”)

But often, I use a variable for the method name, which does not include the @ prefix, so that I end up doing:

method = :foo
…
instance_variable_set(“@#{method}”, some_value)
…
instance_variable_get(“@#{method}”)

But since all instance variables are prefixed with @, I think it redundant to have to type "@#{method}" instead of simply typing method. Why are the methods instance_variable_set and instance_variable_get not designed to accept string/symbol without @ as its first argument like this:

method = :foo
…
instance_variable_set(method, some_value)
…
instance_variable_get(method)

where the variable to be actually set will be @foo rather than foo?

Is there any advantage with the way it is?

  • 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-26T02:52:32+00:00Added an answer on May 26, 2026 at 2:52 am

    The reason is, quite simply, that the instance variable is named @foo, not foo. The @ is part of the variable name, just as the $ is part of the global variable name $foo.

    The reason that @ is not necessary when calling attr_accessor and friends is because they define attribute methods, so it makes sense to provide the method names, not the variable names.

    Of course there is no technical reason instance_variable_set cannot prepend the @ itself. However, the method accepts a symbol that corresponds to the variable name. A symbol by definition represents the identifier with the given name. So the only symbol that corresponds to the instance variable @foo is :@foo. That is why you have to include the @, because we know that :foo does not correspond to any instance variable identifier at all. (And if you supply a string, it will be converted to a symbol internally first.)

    Update: In the C Ruby implementation (MRI), there is actually no mention of @ anywhere in the code that handles instance variables. Only the parser knows instance variables start with a @. So it seems that separating code parsing from implementation is another possible reason.

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

Sidebar

Related Questions

I am a little confused about how I set and get the instance variables
I need to set/get the cookies stored at first.example while browsing second.example , I
I need to wrap a List<T> with some class that allows calls to set/get
Using get/set seems to be a common practice in Java (for various reasons), but
How can I set a variable with the current location? For instance, if I
How do you set/get the values of attributes of t given by x ?
Is it possible to ignore set and get when I'm assigning to or retrieving
With the new approach of having the get/set within the attribut of the class
I've been reading through the details of the System libraries set and get methods
I'm using Session variables inside a web service to maintain state between successive method

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.