Prev: Argument Error
Next: FREE SOFTWARE
From: Amir Ebrahimifard on 31 Jul 2010 18:12 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 ) -- Posted via http://www.ruby-forum.com/.
From: Maurizio De Santis on 31 Jul 2010 21:30 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!" ] -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 1 Aug 2010 07:21 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/.
|
Pages: 1 Prev: Argument Error Next: FREE SOFTWARE |