How come ().nil? is true in Ruby?
Share
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.
Simple answer:
()is an empty expression that evaluates tonil.More detailed: all expressions have a result in Ruby, returning
nilif there’s nothing better to return.()doesn’t cause any action by itself, so an expression that is merely()has nothing in particular to return. Thus the result of the expression is set tonil, and so().nil?evaluates an empty expression, decides there’s nothing much to return so returnsnil. This is indeed equal tonil, sonil?saystrue.