I watched this video. Why is a = a evaluated to nil if a is not defined?
a = a # => nil
b = c = q = c # => nil
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Ruby interpreter initializes a local variable with
nilwhen it sees an assignment to it. It initializes the local variable before it executes the assignment expression or even when the assignment is not reachable (as in the example below). This means your code initializesawithniland then the expressiona = nilwill evaluate to the right hand value.The first assignment expression is not executed, but
ais initialized withnil.You can find this behaviour documented in the Ruby assignment documentation.