Explain how the yield keyword works.

Last updated 27 June 2026

yield invokes the block that was implicitly passed to the current method, optionally passing it arguments and using its return value. It lets a method hand control back to caller-supplied code.

  • Calls the implicit block: Any block attached to the method call ({ } or do...end) is run where yield appears.

  • Passes and receives values: yield x passes x to the block; the expression evaluates to the block's return value.

  • Guard with block_given?: Calling yield without a block raises LocalJumpError, so check first if the block is optional.

[lang:ruby] def each_double(arr) return unless block_given? arr.each { |x| yield x * 2 } end each_double([1, 2]) { |n| puts n } # 2, 4

  • https://www.it-jobs-nl.com/ruby-developer/
  • https://www.bacancytechnology.com/blog/outsource-ruby-on-rails-development
  • https://blog.nextideatech.com/how-to-hire-the-best-ruby-on-rails-developers-the-complete-guide/
  • https://ubiminds.com/en-us/ruby-on-rails-developers/
  • https://dataford.io/interview-guides/pattern/software-engineer
  • https://medium.com/@hpultimatemedia/advanced-ruby-programming-tutorial-9bebc6335c21
  • https://hellointern.in/blog/ruby-interview-questions-and-answers-for-2-years-experience-28493
  • https://www.reddit.com/r/rails/comments/1eb4wfx/ruby_on_rails_role_interview_what_questions_to/
  • https://news.ycombinator.com/item?id=42495135
  • https://www.adaface.com/blog/ruby-on-rails-interview-questions/
  • https://lemon.io/interview-questions/python/
  • https://www.kore1.com/hire-elixir-developers-2026/
  • https://www.testgorilla.com/blog/elixir-developer-interview-questions/
  • https://pragtob.wordpress.com/
  • https://www.shivabhusal.com/2026/01/19/deduplication-of-string-objects-for-memory-efficiency-and-performance/
All Ruby interview questions