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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:14:28+00:00 2026-05-20T15:14:28+00:00

I am using Ruby on Rails 3 and I would like to know in

  • 0

I am using Ruby on Rails 3 and I would like to know in what circumstances should I use instance variables instead of other variable types and if there are security reason for those.

Example:

# Using an instance variable
@accounts = Account.find(...)

# Using a "local"\"normal" variable
account = Account.find(...)
  • 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-20T15:14:29+00:00Added an answer on May 20, 2026 at 3:14 pm

    In general an instance variable is local and persisted inside an instance of an object, whereas a local variable is only local and persisted inside a function/object/block scope. For instance:

    class User
      def name
        @name
      end
    
      def name= name
        @name = name
      end
    end
    
    def greet user
      name = user.name || 'John'
      p "Hi, my name is #{name}"
    end
    
    user = User.new
    greet user
    => 'Hi, my name is John'
    name
    => NameError: undefined local variable or method 'name' for main:Object
    user.name = "Mike"
    greet user
    => 'Hi, my name is Mike'
    @name
    => nil
    

    In the greet function name is a local variable that is only defined within that function. The name variable is set on the first line on the function, name = user.name || 'John', but its value is not persisted outside of the function. When you try calling name you get a NameError because name has only been defined as a local variable within the greet function.

    @name is local to the user instance of the User class. When you try calling it outside of that context you get nil. This is one difference between local and instance variables, instance variables return nil if they have not been defined, whereas local non-instance variables raise an Error.

    Notice that both variable types are local to a specific context though. @name is defined within the user instance, so when you call user.name you are calling the name function in the user instance, in which @name is defined. name is only defined in the greet function, so when you call p "Hi, my name is #{name}" you are able to get a value for name because you are within the scope in which it is defined.

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

Sidebar

Related Questions

I am using Ruby on Rails 3 and I would like to know some
I am using Ruby on Rails 3 and I would like to know what
I'm using Ruby on Rails 2.3.8 and I would like to know how to
I am building web applications using Ruby on Rails and I would like to
I am using Ruby on Rails 3 and I would like, in some way,
I'm using Ruby on Rails 2.3.8 and I would like to get all the
I am running Ruby on Rails 3 and I would like to know if
I am using Ruby on Rails. I want to create a filter field on
I am using ruby on rails with a MySQL backend. I have a table
I'm trying to write an app using Ruby on Rails and I'm trying to

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.