Can you explain why empty? and nil? methods return different results?
ruby-1.9.2-p0 > [].empty?
=> true
ruby-1.9.2-p0 > {}.nil?
=> false
ruby-1.9.2-p0 > {}.empty?
=> true
ruby-1.9.2-p0 > [].empty?
=> true
ruby-1.9.2-p0 > [].nil?
=> false
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.
I’m guessing you are coming from python? Python and other languages can be quite loose about tests for various design reasons.
Ruby takes the strict approach.
nil? is only for testing nil (no value).
empty? is testing for containers (arrays, hashes) that have nothing in them.
Search for ruby documentation on those methods. For example, here is empty for array.
You might also like this tutorial.