Possible Duplicate:
A concise explanation of nil v. empty v. blank in Ruby on Rails
Can anyone tell me the difference between nil?, blank? and empty? in Ruby?
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.
In Ruby,
nilin an object (a single instance of the classNilClass). This means that methods can be called on it.nil?is a standard method in Ruby that can be called on all objects and returnstruefor thenilobject andfalsefor anything else.empty?is a standard Ruby method on some objects like Arrays, Hashes and Strings. Its exact behaviour will depend on the specific object, but typically it returnstrueif the object contains no elements.blank?is not a standard Ruby method but is added to all objects by Rails and returnstruefornil,false, empty, or a whitespace string.Because
empty?is not defined for all objects you would get aNoMethodErrorif you calledempty?onnilso to avoid having to write things likeif x.nil? || x.empty?Rails adds theblank?method.After answering, I found an earlier question, “How to understand nil vs. empty vs. blank in Rails (and Ruby)“, so you should check the answers to that too.