Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using rescue in single line

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 367
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: