Since no answer yet gives you instructions on how to update a string in an array to a new value, here are a few options:
array.select{ |s| s.include? "good" }.each{ |s| s.replace( "bad" ) }
array.grep.replace( "bad" ).each{ |s| s.replace( "bad" ) }
array.find{ |s| s =~ /good/ }.replace( "bad" )
However, all of the above will change each instance of the string. For instance:
jon = Person.new "Johnny B. Goode"
array = [ jon.name, "cat" ]
array.find{ |s| s =~ /good/i }.replace( "bad" )
p array #=> "bad"
p jon #=> #<Person name="bad"> Uh oh...
- , , :
array[ array.index{ |s| s =~ /good/ } ] = "bad"
array[i] = "bad" while i = array.index{ |s| s =~ /good/i }
indices = array.map.with_index{ |s,i| s =~ /good/ ? i : nil }.compact
indices.each{ |i| array[i] = "bad" }
, , :
new_array = array.map do |s|
if s =~ /good/i
"bad"
else
s
end
end
new_array = array.map{ |s| s =~ /good/i ? "bad" : s }
new_array = array.map!{ |s| s =~ /good/i ? "bad" : s }