From: Brian Candler Date: 2010-08-01T20:21:50+09:00 Subject: Re: each method Maurizio De Santis wrote: > Amir Ebrahimifard wrote: >> Hi >> What can I do to can apply some change to an array for always ? ( I want >> to use iterator and each method and apply some change in an array ) > > if I understood the question: > > array.collect! {|item| block } → array > array.map! {|item| block } → array > > Invokes the block once for each element of self, replacing the element > with the value returned by block. See also Enumerable#collect. > > a = [ "a", "b", "c", "d" ] > a.collect! {|x| x + "!" } > a #=> [ "a!", "b!", "c!", "d!" ] Another option, if you know the contents are all Strings or Arrays, is to use replace: a.each { |x| x.replace(x+"!") } It's different because it modifies the string objects themselves (leaving the Array pointing to the same string objects) -- Posted via http://www.ruby-forum.com/.