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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:22:31+00:00 2026-06-14T19:22:31+00:00

Going through the rspec docs i found these lines of code in yielding section

  • 0

Going through the rspec docs i found these lines of code in yielding section of this page http://rubydoc.info/gems/rspec-expectations/frames

Can anyone explain what each line of code in the yielding section does step-by-step

  • 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-14T19:22:32+00:00Added an answer on June 14, 2026 at 7:22 pm

    What does expect { |b| 5.tap(&b) }.to yield_control do?

    It is expecting that the code given in the block invokes yield statement.

    The code given in the block (viz. { |b| 5.tap(&b) }) calls the ruby 1.9 tap method which has a yield statement within it’s implementation.

    So the statement is effectively asserting that ruby 1.9’s tap method has a yield statement. 🙂

    To better understand this statement, try the following code example:

    order.rb file

    class Order
    end
    

    order_spec.rb file

    require 'rspec-expectations'
    require './order'
    
    describe Order do
      it "should yield regardless of yielded args" do
        expect { |b| 5.tap(&b);puts "The value of b is #{b.inspect}" }.to yield_control
      end
    end 
    

    Output from executing the spec:

    $ rspec yield_spec.rb
    
    Order
    The value of b is #<RSpec::Matchers::BuiltIn::YieldProbe:0x000001008745f0 @used=true, @num_yields=1, @yielded_args=[[5]]>
      should yield regardless of yielded args
    
    Finished in 0.01028 seconds
    1 example, 0 failures
    

    To understand the other lines in the yielding section, similarly include the expect statements in the spec file as follows:

     it "should yield with no args" do
        expect { |b| Order.yield_if_true(true, &b) }.to yield_with_no_args
      end 
          it "should yield with 5 args" do
        expect { |b| 5.tap(&b) }.to yield_with_args(5)
      end 
    
      it "should yield with integer args" do
        expect { |b| 5.tap(&b) }.to yield_with_args(Fixnum)
      end 
    
      it "should yield with string args" do        
        expect { |b| "a string".tap(&b) }.to yield_with_args(/str/)
      end 
    
      it "should yield 1,2,3 successively" do
        expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
      end 
    
      it "should yield ([:a,1], [:b,2]) successively" do
        expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2]) 
      end 
    

    Note: The yield_if_true method used in the second line seems to be removed from where it was originally defined; I got it go work by adding it into the Order class as follows:

    class Order
      def self.yield_if_true(flag)
        yield if flag
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore
im going through the setup on http://www.railstutorial.org/chapters/static-pages#fig:autotest_green and im stuck on this particular error:
Going through my code, I was hoping to remove instances where I do this:
While going through Qt code I had this basic question on the pimpl implementation.
So I'm using rspec to test my code as I'm going through the Rails
Going through Apache Cordova's source code, I ran into two lines of code that
Going through the phantomJS code i ran into this in the webpage.cpp file (line
After going through the build process (found at: http://source.android.com/source/building.html ) for the master branch
I'm going through a tutorial that has suggested using rspec , but I have
Going through Javascript documentation, I found the following two functions on a Javascript object

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.