這是要聽起來不可思議,但我很樂意做這樣的事情:關於哈希的Ruby case語句?
case cool_hash
when cool_hash[:target] == "bullseye" then do_something_awesome
when cool_hash[:target] == "2 pointer" then do_something_less_awesome
when cool_hash[:crazy_option] == true then unleash_the_crazy_stuff
else raise "Hell"
end
理想情況下,我甚至不會需要引用又有因爲它是case語句是什麼。如果我只想使用一個選項,那麼我會「case cool_hash [:that_option]」,但我想使用任意數量的選項。另外,我知道Ruby中的case語句只評估第一個真正的條件塊,有沒有一種方法可以覆蓋這個來評估每個真正的塊,除非有一箇中斷?
非常感謝響應快!不勝感激。這有助於並希望隨着時間的推移,我會在Ruby中思考。 –