What do the tap and then (yield_self) methods do and when would you use them?

Last updated 27 June 2026

  • tap yields the object and returns the same object, ideal for logging or mutating mid-chain.

  • then (alias yield_self) yields the object and returns the block's result, letting you pipe a value through transformations left-to-right.

  • https://ruby-doc.org/3.2/Kernel.html
  • https://blog.tatsuya-develop.com/articles/20250108114152_ruby_tap
  • https://medium.com/coverwallet-engineering/beautify-ruby-code-with-then-f1d8419f4fd1
  • https://docs.ruby-lang.org/en/3.3/Kernel.html
  • https://docs.ruby-lang.org/ja/latest/method/Object/i/then.html
  • https://docs.ruby-lang.org/en/2.6.0/Object.html
  • https://dev.to/anakbns/ruby-methods-tap-and-then-2h7e
  • https://monorails.substack.com/p/ruby-tap-vs-yield_self
All Ruby interview questions