Ruby 这个return关键字是否会退出if/else块的方法?
我有一个类似于此的方法,我不确定使用此Ruby 这个return关键字是否会退出if/else块的方法?,ruby,Ruby,我有一个类似于此的方法,我不确定使用此return方法是否会退出该方法或if/else块。有人能给我解释一下在这种情况下会发生什么吗。如果我达到返回的条件,它会退出并点击我想要避免的方法吗 def buy_another_pet(owner) if owner.has_pets? if owner.pets.is_healthy? if owner.pets.count == 3 # Don't buy a pet else
return
方法是否会退出该方法或if/else
块。有人能给我解释一下在这种情况下会发生什么吗。如果我达到返回的条件,它会退出并点击我想要避免的方法吗
def buy_another_pet(owner)
if owner.has_pets?
if owner.pets.is_healthy?
if owner.pets.count == 3
# Don't buy a pet
else
# buy another pet
return
end
end
# =====Will my return keyword avoid the below method=====?
method_I_want_to_avoid
end
end
提前谢谢你的帮助
如果我达到返回的条件,它会退出并点击吗
我想要避免的方法
def buy_another_pet(owner)
if owner.has_pets?
if owner.pets.is_healthy?
if owner.pets.count == 3
# Don't buy a pet
else
# buy another pet
return
end
end
# =====Will my return keyword avoid the below method=====?
method_I_want_to_avoid
end
end
否。return
退出该方法,而不是if
/else
块
如果我达到返回的条件,它会退出并点击吗
我想要避免的方法
def buy_another_pet(owner)
if owner.has_pets?
if owner.pets.is_healthy?
if owner.pets.count == 3
# Don't buy a pet
else
# buy another pet
return
end
end
# =====Will my return keyword avoid the below method=====?
method_I_want_to_avoid
end
end
否。
return
退出该方法,而不是if
/else
块。它将退出购买另一宠物方法。如果您想跳过If/else,您应该使用break insteadit,它将退出“购买另一个宠物”方法。如果你想跳过这个If/else,你应该使用break