From: Intransition Date: 2010-03-26T22:02:40+09:00 Subject: Re: Creating a gsub! method for Arrays On Mar 26, 5:15 am, Robert Klemme wrote: > 2010/3/26 Derek Cannon : > > > After playing around some more, I've found a solution. > > > For anyone who is interested, the following code works: > > > class Array > >  def gsub!(pattern, replacement) > >    each { |x| > >      x.gsub!(pattern, replacement) > >      } > >  end > > end > > What is the benefit of doing > > arr.gsub! /foo/, 'bar' > > over > > arr.each {|s| s.gsub! /foo/, 'bar'} > > ?  Frankly I'd rather use the latter form.  Because otherwise you'll > have to put a method in Array (or Enumerable for that matter) for > *every* operation you want to apply to elements.  Additionally, there > is another point: since you made Array#gsub! look like String#gsub! > this might confuse readers of the code - especially if someone picks > bad variable names like "a". Agreed. There's also require 'facets/enumerable/every' arr.every.gsub! /foo/, 'bar'