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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:44:41+00:00 2026-06-04T12:44:41+00:00

This is a description of how to create a helper method in Rspec taken

  • 0

This is a description of how to create a helper method in Rspec taken from the Rspec book (page 149). This example assumes that there is a method called ‘set_status’ which is triggered when the ‘Thing’ object is created.

Both sets of code create a new ‘Thing’ object, set the status, then do ‘fancy_stuff’. The first set of code is perfect clear to me. One of the ‘it’ statements it triggered, which then calls the ‘create_thing’ method with options. A new ‘Thing’ object is created and the ‘set_status’ method is called with the ‘options’ attribute as the parameter.

The second set of code is similar. One of the ‘it’ statements is triggered, which then calls the ‘given_thing_with’ method while passing ‘:status’ hash assignment as a parameter. Within the ‘given_thing_with’ method the ‘yield’ is triggered taking the ‘Thing.new’ as a parameter. This is where I am having trouble. When I try to run this code I get an error of “block given to yield”. I understand that whatever attributes that are passed by yield will be returned to the ‘thing’ in pipe brace from the ‘it’ statement that called the ‘given_thing_with’ method. I can get the new

What I don’t understand is why the code block is not called in the ‘given_thing_with’ method after the ‘yield’ command. In other words, I can’t code in that block to run.

Thanks in advance for your help.

The remainder of this question is quoted directly from the Rspec book:

describe Thing do
  def create_thing(options)
    thing = Thing.new
    thing.set_status(options[:status])
    thing
  end

  it "should do something when ok" do
    thing = create_thing(:status => 'ok')
    thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
    ...
  end

  it "should do something else when not so good" do
    thing = create_thing(:status => 'not so good')
    thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
    ...
  end
end

One idiom you can apply to clean this up even more is to yield self from initializers in your objects. Assuming that Thing’s initialize() method does this and set_status() does as well, you can write the previous like this:

describe Thing do
  def given_thing_with(options)
    yield Thing.new do |thing| 
      thing.set_status(options[:status])
    end
  end

  it "should do something when ok" do
    given_thing_with(:status => 'ok') do |thing|
      thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
      ... 
    end
  end

  it "should do something else when not so good" do
    given_thing_with(:status => 'not so good') do |thing|
      thing.do_fancy_stuff(1, true, :move => 'left', :obstacles => nil)
      ... 
    end
  end
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-04T12:44:42+00:00Added an answer on June 4, 2026 at 12:44 pm

    There are 2 issues with the code from book.

    1. Setting up the initializer to yield itself

    When the Thing object is created, it needs an initializer and need yield itself.

    class Thing
      def initialize
        yield self
      end
    end
    

    However, this alone will still causes an error, at least on my system, which is Ruby 1.9.3. Specifically, the error is ‘block given to yield (SyntaxError)’. This doesn’t make much sense, since that is what we want it to do. Regarless, that is the error I get.

    2. Fixing the ‘block given to yield’ error

    This is not as obvious and has something to do with either Ruby or the ‘yield’ statement, but creating a block using ‘do…end’ as was written in the book and is shown below causes the error.

    yield Thing.new do |thing| 
      thing.set_status(options[:status])
    end
    

    Fixing this error is simlpy a matter of creating the block using braces, ‘{…}’, as is shown below.

    yield Thing.new { |thing| 
      thing.set_status(options[:status])
    }
    

    This is not good form for multiline Ruby code, but it works.

    Extra. How the series of yields works to set the parameters of the ‘Thing’ object

    The problem is already fixed, but this explains how it works.

    • the “caller block” calls ‘given_thing_with’ method with a parameter
    • that method yields back to the “caller block” a new “Thing” and a block (I’ll call it the “yield block”)
    • to execute the “yield block”, the Thing class needs the initialization and ‘yield self’, otherwise the ‘set_status’ method will never be run because the block will be ignored
    • the new “Thing” is already in the “caller block” and has it’s status set and now the relevant method is executed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this table i want to create job_id dynamic Jobs Title text Job Description text
I have a lot of C# VS2008 code that looks like this: new SqlParameter(@Description,
FYI, There is some overlap in the initial description of this question with a
Hi I have created a setDecorator() which is something like this: $timeSu->setDecorators(array('ViewHelper', 'Description', 'Errors',
I have something like this: [Description(Sets the color.), Category(Values), DefaultValue(Color.White), Browsable(true)] public Color MyColor
I've just read this description of the Android Support Package / Compatibility Library... http://developer.android.com/sdk/compatibility-library.html
How to describe this in description logic? every human is either male or female
I have a query which produces a list of orders like this: Invoice Description
here is ma rss code, this display description in Arabic but in title it
quick question... How can I best create internal links? This is the markup I

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.