Ruby1.9.2とRubyonRails3.2.2を使用しています。私は次の方法を持っています:
# Note: The 'class_name' parameter is a constant; that is, it is a model class name.
def my_method(class_name)
case class_name
when Article then make_a_thing
when Comment then make_another_thing
when ... then ...
else raise("Wrong #{class_name}!")
end
end
上記のステートメントで、などのメソッド呼び出しを実行するとcase
、常にelse
「部分」が実行される理由を理解したいと思います。my_method(Article)
my_method(Comment)
どうすれば問題を解決できますか?誰かがこれを処理する方法についてアドバイスがありますか?