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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:53:23+00:00 2026-06-06T04:53:23+00:00

Well, like the title shown, what’s the difference between the two class instance variables

  • 0

Well, like the title shown, what’s the difference between the two class instance variables below

class Document
  @default_font = :Arial
  ...
end

And

class Document
  def foo
    @default_font = :Arial
  end
  ...
end

Is there anyone can explain it to me. Thank you very much.

  • 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-06T04:53:27+00:00Added an answer on June 6, 2026 at 4:53 am

    In your first case, the variable is neither a class variable(which should have started with @@, nor an instance variable. It is simply a local variable not available outside the current scope, not even within the instance methods.

    The second is an instance variable.

    class Document
      attr_accessor :var1, :var2
      @var1 = 1
      puts @var1 + 2
    
      def initialize
        @var2 = 4
        puts @var2**2
        #puts @var1 + 6
      end
    
    end
    
    1.9.2p0 :208 > class Document
    1.9.2p0 :209?>     attr_accessor :var1, :var2
    1.9.2p0 :210?>     @var1 = 1
    1.9.2p0 :211?>     puts @var1 + 2
    1.9.2p0 :212?>   
    1.9.2p0 :213 >       def initialize
    1.9.2p0 :214?>         @var2 = 4
    1.9.2p0 :215?>         puts @var2**2
    1.9.2p0 :216?>         #puts @var1 + 6
    1.9.2p0 :217 >         end
    1.9.2p0 :218?>   
    1.9.2p0 :219 >     end
    3
     => nil 
    1.9.2p0 :220 > d = Document.new
    16
     => #<Document:0x1a2f0c @var2=4> 
    

    The @var1 + 6 inside the instance method gives an error.

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

Sidebar

Related Questions

Well the title Question pretty much sums it up, But I'd like to detail
Ok well heres what I would like to do in PHP: http://www.wordle.net/ I know
Well, I'll say it like this. I have a table with 2 columns. The
Well, how to create picture link like this up or down votes (on the
I'd like to get the HTML elements highlighted as well as the MAKO /
Well, this is what I want to do in my app: I would like
Well is there? From everything I've read, it seems like the answer is no,but
Well I want to render a self closing tag say <img> tag like this
There are well-known algorithms for cryptography to compute modular exponentiation (a^b)%c (like Right-to-left binary
I'm using the well known technique of styling a list box to look 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.