大文字と小文字の配列があります。私は新しい配列の大文字と次の小文字を連結しようとしています。例えば、私は次の配列配列内の項目をマージするRuby
first_array = ["A","b","C","d","e"]
を持っていると私は、このようなコードの最初の配列を反復処理しようとしています次の配列
["Ab","Cde"] #new array
を取得したい:
new_array = []
first_array.each_with_index do |a,index|
if (a!~/^[a-z].*$/)
new_array = new_array.push "#{a}"
else
new_array[-1] = first_array[index-1] + "#{a}" #the idea is to concatenate the small letter with the previous capital letter and replace the last item in the new array
end
しかし、動作しません。私はこの問題に効率的に取り組んでいるとは確信していません。それが解決できないのです。誰かがいくつかのオプションを提案できますか?
「動作しません」と定義してください。 –
@Sergio - 正しい結果が得られません。上のコードで私は["Ab"、 "de"]を得ます – Bastien
質問にその情報を入れてください。 –