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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:44:59+00:00 2026-06-12T00:44:59+00:00

Here is a bit of code from M Hartl’s Ruby on Rails Tutorial. Can

  • 0

Here is a bit of code from M Hartl’s Ruby on Rails Tutorial. Can anyone explain why an instance variable (@user) is necessary and why not use a local variable. Also, since instance variables are supposed to be the variables in the instance of a class, which class is @user instantiated from?

require 'spec_helper'

describe User do

  before { @user = User.new(name: "Example User", email: "user@example.com") }

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
end
  • 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-12T00:45:00+00:00Added an answer on June 12, 2026 at 12:45 am

    Use of a local variable in that instance would mean that its scope would be restricted to the before and hence result in an error. The @user is of type User but is an instance variable of the describe block. Rspec has some magic that at run-time makes a class out of each describe block. Each example (it block) ends up being a subclass of said class. Class inheritance lets the examples see @user.

    Edited 2017-05-14

    Linked blog post is no longer available. Updating with Wayback Machine link + inlining relevant section here.

    Note that this is considered an anti-pattern as detailed in this blog post. Use let instead.

    let has the following advantages:

    • It is memoized when used multiple times in one example, but not
      across examples.
    • It is lazy-loaded, so you wont waste time initializing the variable for examples that don’t reference it.
    • Will raise an exception if you have a typo in your variable name.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is some code from a C++ procedure. The original can be found here;
Here's a bit of code which prints out the squares of the numbers from
Here is a piece of code from the jQuery source (bit.ly/jqsource): // The DOM
It seems like I had to inline quite a bit of code here. I'm
Here's the relevant bit of the source code: class Dice { String name ;
I have some code here that uses bitsets to store many 1 bit values
i just want to ask about my html code here, i have a bit
I have a little bit of Javascript that almost works correctly. Here's the code:
I am playing around with WPF a bit. Here is the Code that does
Bit confused here, I have an on-demand instance but do I get charged even

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.