Como ninguna respuesta hasta el momento se instruye sobre cómo actualizar la cadena de la matriz con el nuevo valor, aquí hay algunas opciones:
# Find every string matching a criteria and change them
array.select{ |s| s.include? "good" }.each{ |s| s.replace("bad") }
# Find every string matching a pattern and change them
array.grep.replace("bad").each{ |s| s.replace("bad") }
# Find the first string matching a criteria and change it
array.find{ |s| s =~ /good/ }.replace("bad")
Sin embargo, todo lo anterior va a cambiar todas las instancias de una cadena. Por ejemplo:
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...
Si no quieren este lado efecto, si desea reemplazar la cadena de la matriz con una cadena dediferente, pero no cambio la propia cadena, entonces necesita encontrar el índice del elemento y actualización que:
# Find the _first_ index matching some criteria and change it
array[ array.index{ |s| s =~ /good/ } ] = "bad"
# Update _every_ item in the array matching some criteria
array[i] = "bad" while i = array.index{ |s| s =~ /good/i }
# Another way to do the above
# Possibly more efficient for very large arrays any many items
indices = array.map.with_index{ |s,i| s =~ /good/ ? i : nil }.compact
indices.each{ |i| array[i] = "bad" }
Por último, el más simple y sobre todo más rápido, sin curioseaba con índices:
# Create a new array with the new values
new_array = array.map do |s|
if s =~ /good/i # matches "Good" or "good" in the string
"bad"
else
s
end
end
# Same thing, but shorter syntax
new_array = array.map{ |s| s =~ /good/i ? "bad" : s }
# Or, change the array in place to the new values
new_array = array.map!{ |s| s =~ /good/i ? "bad" : s }
Esto no resuelve el problema de establecer el elemento de matriz en otro valor. – cvshepherd
@maprihoda Tenga en cuenta que aunque 'grep' _is_ convenient, nada le impide usar una expresión regular con' select' o 'find' (como en mi respuesta). De hecho, puede hacer ** mucho más ** con los métodos de bloque, p. 'select {| s | s = ~/good/i && s.length> 3} '. – Phrogz
@Phrogz mucho más en términos de expresión regular; sin embargo, tienes razón, puedes hacer mucho con select, el segundo ejemplo de seleccionar en tu comentario lo ilustra muy bien – maprihoda