Explain the difference between map and each. When would you use map!?

Last updated 27 June 2026

each iterates purely for side effects and returns the original collection, while map transforms each element and returns a new array of the results. Use map! when you want that transformation applied in place, mutating the original array.

  • each is for side effects: Run code per element (print, accumulate); the block's return value is ignored and each returns the receiver unchanged.

  • map is for transformation: Collects each block return value into a new array of the same length; the original is untouched.

  • map! mutates in place: Replaces each element with the block's result on the original array; use only when mutation is intended and safe.

  • Choosing: Need a new transformed collection: map. Just doing work: each. Want to overwrite the source: map!.

[lang:ruby] [1, 2, 3].each { |n| puts n } # => [1, 2, 3] (returns original) [1, 2, 3].map { |n| n * 2 } # => [2, 4, 6] (new array) a = [1, 2, 3] a.map! { |n| n * 2 } # a is now [2, 4, 6]

  • https://www.turing.com/interview-questions/ruby
  • https://www.youtube.com/watch?v=lkunzd5uDrw
  • https://blog.jacobvosmaer.nl/0004-ruby-scheduler-interaction/
  • https://wohotech.in/coding/top-java-interview-questions
  • https://jetthoughts.com/blog/ruby-3-4-yjit-performance-guide/
  • https://speed.ruby-lang.org/
  • https://flexiple.com/python/interview-questions
  • 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
All Ruby interview questions