【发布时间】:2013-01-01 15:32:16
【问题描述】:
举个例子:
def method_of_doom
my_string = "I sense impending doom."
my_string.ah_ha_i_called_a_nonexistent_method
rescue NoMethodError => e:
puts "PROBLEM: " + e.to_s
rescue Exception:
puts "Uhh...there's a problem with that there method."
end
在上面写着的那一行:
rescue NoMethodError => e:
“=>”在做什么?
它与这种用法有何不同:
module FighterValues
BAMBOO_HEAD = { 'life' => 120, 'hit' => 9 }
DEATH = { 'life' => 90, 'hit' => 13 }
KOALA = { 'life' => 100, 'hit' => 10 }
CHUCK_NORRIS = { 'life' => 60000, 'hit' => 99999999 }
def chuck_fact
puts "Chuck Norris' tears can cure cancer..."
puts "Too bad he never cries."
end
end
module ConstantValues
DEATH = -5 # Pandas can live PAST DEATH.
EASY_HANDICAP = 10
MEDIUM_HANDICAP = 25
HARD_HANDICAP = 50
end
puts FighterValues::DEATH
→ {'life'=>90,'hit'=>13}
puts ConstantValues::DEATH
→ -5
【问题讨论】:
-
请养成缩进习惯。
-
你不应该这样做
rescue Exception-Exception可能包含非常严重的错误。 (不是批评你,只是教程的作者)。
标签: ruby syntax rescue hashrocket