We can use rescue in single line to return a value if code of that line raise an error/exception, example shown below-:
Let say we have a hash:
person = {:name => "ABC"}
person[:name].downcase # "abc"
code without rescue:
person[:age].downcase # NoMethodError: undefined method `downcase' for nil:NilClass
code with inline rescue:
person[:age].downcase rescue "Age not present" # "Age not present"
0 Comment(s)