Ruby: How to write a bang method, like map?
问题 I'd like to write some new Array methods that alter the calling object, like so: a = [1,2,3,4] a.map!{|e| e+1} a = [2,3,4,5] ...but I'm blanking on how to do this. I think I need a new brain. So, I'd like something like this: class Array def stuff! # change the calling object in some way end end map! is just an example, I'd like to write a completely fresh one without using any pre-existing ! methods. Thanks! 回答1: EDIT - Updated answer to reflect the changes to your question. class Array def