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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:29:47+00:00 2026-05-27T22:29:47+00:00

For example, in FactoryGirl, it allows you to do something like factory :user, :class

  • 0

For example, in FactoryGirl, it allows you to do something like

factory :user, :class => User do
  sequence :username do |n|
    "blahblah#{n}"
  end
end

I’ve looked through the source code at https://github.com/thoughtbot/factory_girl but can’t seem to figure out how they are achieving this (it loks like it’s some type of proxy?). That is, how is it we are able to access the sequence method?

  • 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-27T22:29:48+00:00Added an answer on May 27, 2026 at 10:29 pm

    They are using instance_eval.

    This means that inside the block self (and thus the object sequence is called on) isn’t what it would otherwise be (probably the top level object in this case) but the object that instance_eval was called on (in this case an object that implements the constructs of the factory girl syntax.

    It’s often used with DSLs (another example is the routes.rb file in rails >= 3) because without it you’d have to do something like

    factory ... do |f|
      f.sequence ...
    end
    

    which tend to distract from the DSL.

    One tiny example illustrating its effects

    foo = "123456"
    foo.instance_eval do
      length
    end
    #=> 6
    

    With a ‘normal’ block this would have raised an error, because the top level object has no length method, but because instance_eval is used length is called on the string instead.

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

Sidebar

Related Questions

FactoryGirl.define do sequence :email do |n| user#{n}@example.com end factory :user do email {FactoryGirl.generate :email}
FactoryGirl.define do factory :agency do name Example Inc available_items 20 recruiter # recruiter.id end
Here are my factories: FactoryGirl.define do factory :account do company 'Example, Inc.' end factory
When building the following factory: Factory.define :user do |f| f.sequence(:name) { |n| foo#{n} }
ruby-1.9.2-p180 :007 > Factory.define :user do |user| ruby-1.9.2-p180 :008 > user.email user@example.com ruby-1.9.2-p180 :009?>
Example: function example($x = ) { Do something } Isn't $x empty by default
Example I have Person , SpecialPerson , and User . Person and SpecialPerson are
Example: The user login to the webpage => Click on a button This action
I'm trying to use factory_girl to create a user factory (with RSpec) however it
Example: error_reporting(E_ALL | E_STRICT); class Test {} $obj = new Test(); $obj->undeclared = oops;

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.