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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:34:20+00:00 2026-06-13T11:34:20+00:00

So if I understand correctly Object#tap uses yield to produce a temporary object to

  • 0

So if I understand correctly Object#tap uses yield to produce a temporary object to work with during the execution of a process or method. From what I think I know about yield, it does something like, yield takes (thing) and gives (thing).dup to the block attached to the method it’s being used in?
But when I do this:

class Klass
attr_accessor :hash
  def initialize
   @hash={'key' => 'value'}
  end
end

instance=Klass.new
instance.instance_variable_get('@hash')[key] # => 'value', as it should

instance.instance_variable_get('@hash').tap {|pipe| pipe['key']=newvalue}

instance.instance_variable_get('@hash')[key] # => new value... wut?

I was under the impression that yield -> new_obj. I don’t know how correct this is though, I tried to look it up on ruby-doc, but Enumerator::yielder is empty, yield(proc) isn’t there, and the fiber version… I don’t have any fibers, in fact, doesn’t Ruby actually explicitly require include ‘fiber’ to use them?

So what ought have been a read method on the instance variable and a write on the temp is instead a read/write on the instance variable… which is cool, because that’s what I was trying to do and accidentally found when I was looking up a way to deal with hashes as instance variables (for some larger-than-I’m-used-to tables for named arrays of variables), but now I’m slightly confused, and I can’t find a description of the mechanism that’s making this happen.

  • 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-13T11:34:21+00:00Added an answer on June 13, 2026 at 11:34 am

    Object#tap couldn’t be simpler:

    VALUE
    rb_obj_tap(VALUE obj)
    {
        rb_yield(obj);
        return obj;
    }
    

    (from the documentation). It just yields and then returns the receiver. A quick check in IRB shows that yield yields the object itself rather than a new object.

    def foo
      x = {}
      yield x
      x
    end
    
    foo { |y| y['key'] = :new_value }
    # => {"key" => :new_value }
    

    So the behavior of tap is consistent with yield, as we would hope.

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

Sidebar

Related Questions

If I understand correctly, each and every object in JavaScript inherits from the Object
If I understand correctly, in .NET the default implementation of Object.GetHashCode() returns a value
Why does this not work? Do I not understand delegate covariance correctly? public delegate
really can't understand, how to build correctly project that uses boost.python. I've included boost_(python/thread/system)-mt.
If I understand correctly, when a shared_ptr (from boost, tr1, std, whatever) is initialised
If I understand correctly, Infinispan/JBoss Cache uses Java's own serialization mechanism, which can be
I Just want to make sure I understand this correctly... search is an object
If I understand correctly, the __cmp__() function of an object is called in order
Do I understand correctly that CCLabelBMFont only loads the font texture once, no matter
This is just a quick question to understand correctly what happens when you create

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.