Im sure I am missing something here but none the less.
foo['bar'] = nil
if(foo['bar'] == nil)
puts "foo bar is nil :("
However, nothing happens? Any thoughts?
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.
You need an
endstatement to close yourifi.e.Note that there is a
nil?method for checking if something isniland the brackets around your condition aren’t necessary so it would be more idiomatic Ruby to write:As Arkku commented, a single line
ifcan be written concisely as:Which is better depends on the context (e.g. do you want to emphasise the condition or emphasise what happens if the condition is true) and a certain amount of personal preference. But as an example you might put a guard condition at the start of a method e.g.