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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:07:50+00:00 2026-06-13T05:07:50+00:00

I Just started learning ruby and I don’t see the difference between an @instace_variable

  • 0

I Just started learning ruby and I don’t see the difference between an @instace_variable and an attribute declared using attr_accessor.

What is the difference between the following two classes:

class MyClass  
  @variable1 
end

and

class MyClass
  attr_accessor :variable1
end

I searched lot of tutorials online and everybody uses different notation, Does it have to do anything with the ruby version? I also searched few old threads in StackOverflow

What is attr_accessor in Ruby?
What's the Difference Between These Two Ruby Class Initialization Definitions?

But still I am not able to figure out what is the best way to use.

  • 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-13T05:07:51+00:00Added an answer on June 13, 2026 at 5:07 am

    An instance variable is not visible outside the object it is in; but when you create an attr_accessor, it creates an instance variable and also makes it visible (and editable) outside the object.

    Example with instance variable (not attr_accessor)

    class MyClass
      def initialize
        @greeting = "hello"
      end
    end
    
    m = MyClass.new
    m.greeting #results in the following error:
      #NoMethodError: undefined method `greeting' for #<MyClass:0x007f9e5109c058 @greeting="hello">
    

    Example using attr_accessor:

    class MyClass
      attr_accessor :greeting
    
      def initialize
        @greeting = "hello"
      end
    end
    
    m2 = MyClass.new
    m2.greeting = "bonjour" # <-- set the @greeting variable from outside the object
    m2.greeting #=> "bonjour"   <-- didn't blow up as attr_accessor makes the variable accessible from outside the object
    

    Hope that makes it clear.

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

Sidebar

Related Questions

I just started learning ruby on rails. I'm using 'Agile Web Development with Rails,
I've just recently started learning/using Ruby at work. I've got a super-simple test script
I've just started learning ruby, so this question is simple. I created @subject in
I just started learning C but I don't understand this part of the code.
I have just started learning ruby on rails. I have a doubt wrt routing.
I have just started learning Ruby on Rails. I happened to look for prevention
I have started learning Ruby and just tried out my first hello world program
I've just started learning Ruby on Rails and it's a newbie question. I did
I've only just started learning ruby on rails and I would like to create
Just started learning WP7 platform. Don't even know how to describe occurred error. It

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.