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/.
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/.
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/\.
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 } → arrayInvokes 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/\.