-1
私は "Rosetta Code 100 Doors"の問題に取り組んでおり、壁に衝突しました。 私は "100 doors help using Ruby"を見つけましたが、これは助けになっていましたが、私のコードを動作させることができません。ルビーの100ドア
私のtoggle
メソッドは、配列の反復方法では機能しません。
def toggle(state)
if state == 'closed'
state.replace('open')
elsif state == 'open'
state.replace('closed')
end
end
def one_hundred_doors(array)
i = 0
100.times do
i += 1
array.each_with_index.map do |state, index|
if (index + 1) % i == 0
toggle(state)
end
end
end
array.each_with_index { |state, door| puts "Door #{door + 1} is #{state}." }
end
doors = Array.new(100, "closed")
one_hundred_doors(doors)
誰かが間違っていることを説明できますか?
はSOへようこそ。 "私のトグル方法は、私の配列反復メソッド内で動作していないようです。"どういう意味ですか?説明と詳細のヘルプ。 "[ask]"と "[mcve]"をお読みください。 –