sollte die Indizes des Wortes 'hello'
zum indices
Array hinzufügen, aber es ist das Hinzufügen sie nicht auf das Array:Wie findet man alle Indizes eines bestimmten Elements in einem Array? Dieser Code
words = %w(hello how are you then okay then hello how)
def global(arg1, arg2)
indices = []
arg1.each do |x, y|
indices << y if arg2 == x
end
indices
end
global(words,'hello')
#=> [nil, nil]
Was mit meinem Code falsch?
'wenn arg1 == x' - Ein Array wird nie einem seiner Elemente entsprechen, daher ist diese Bedingung niemals wahr. Deshalb bekommst du keine Indizes. Meintest du 'if arg2 == x'? –
Wenn Sie bessere, aussagekräftigere Namen für Ihre Argumente hätten, wäre dieser Fehler nicht passiert. –
Auch "each_index" anstelle von "jeder". –