Each method

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 )

Amir E. 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!” ]

Maurizio De Santis wrote:

Amir E. 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)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs