Empty Variables

  • published on September 23rd, 2008

    One of the more confusing differences between Ruby and PHP is the different way in which the languages treat empty variables when evaluating statements.

    The chart below shows the most common scenarios you may encounter. We have several similar charts in the Rails for PHP Developers book with other handy information.

    PHP if ($x) empty($x)  
    $x = ""; FALSE TRUE  
    $x = null; FALSE TRUE  
    $x = array(); FALSE TRUE  
    $x = false; FALSE TRUE  
    $x = 0; FALSE TRUE  
    $x = "0"; FALSE TRUE  
     
    Ruby if (x) x.empty? x.blank?**
    x = "" TRUE TRUE TRUE
    x = nil FALSE u/m * TRUE
    x = [] TRUE TRUE TRUE
    x = {} TRUE TRUE TRUE
    x = false FALSE u/m * TRUE
    x = 0 TRUE u/m * FALSE
    x = "0" TRUE FALSE FALSE
    * NoMethodError: undefined method ** Only Available in Rails

    While PHP evaluates all empty variable as false, Ruby only evaluates nil and false to be false. Some Ruby objects have an empty? method, but this is not always as useful as you might expect. The method is only available on some objects, and calling nil.empty? or false.empty? will result in NoMethodError.

    The Rails framework adds a consistent method named blank? that exists on all objects as a single way to check if a variable contains a blank object. Rails has also very recently added a corresponding present? method, which is equivalent of !blank?

    Handling Zero

    Handling zero values often trips up beginning Ruby programmers. This cause of this confusion is best demonstrated by the following two code snippets.

    PHP

    $num = 0;
    if ($num) {
      // This not evaluated.
    }

    Ruby

    num = 0
    if num
      # This is evaluated!
    end

    Refer to the chart above as you read the snippets. While this may seem counter intuitive coming from PHP, for the most part it’s not a problem.

    In Ruby code, we typically know when we’re working with numbers, or we can cast to them. We can then simply do something like these examples:

    Ruby

    num = User.count
     
    if num > 0
      puts "there are some users"
    end
     
    if num.zero?
      puts "no users here"
    end

    Similarly, it is important to note that zero (0 and "0") evaluates to true using 0.blank?.

21 comments

  • comment by lowell 24 Sep 08

    “Similarly, it is important to note that zero (0 and “0″) evaluates to true using 0.blank?.”

    of course.. in ruby, everything but nil and false itself is true.

  • comment by Custom PHP 12 Nov 08

    Might mess someone up if they are going back and forth or working on PHP apps while working on Ruby apps.

  • pingback by d x m i o . c o m » Links for 09/24/2008 25 Jan 09

    [...] PHP and Rails: variable differences [...]

  • comment by Toronto PHP 11 May 09

    I agree, this leaves too much room for error, not a good idea.

  • comment by feng 16 Jun 09

    thanks~

  • comment by Tyler 3 Sep 09

    Like many, I started in php and have been switched over to rails for a few months now. So I have a continuing problem along these lines. Say I have a person object and person “belongs_to :network”. Then if i have a person that for whatever reason doesn’t have a network, and I say “person.network.name”, I get an error. Is there a way to just make it return nothing like php instead of raising an error?

  • comment by Php Developer 2 Oct 09

    yes when i start programming with ruby and when switch to PHP i faced same issue.

  • comment by Chuck Vose 28 Oct 09

    @Tyler for those who read this later (I’m assuming you already found your solution).

    It is a common idiom in ruby to say something like:
    person.network.name if person.network
    or
    person.network.name unless person.network.nil?

    Since a relationship will never return false you really only have to worry about nullity. This is why both above lines work just fine.

    The reason Rails and Ruby don’t return nothing in this context is because nothing is very different than nil. Someone once told me that nothing means you have a phone but no phone-number, null is when you don’t have a phone and your house isn’t even capable of having a phone even if you did have one.

    Besides, returning nothing would break if person.network.name :)

  • comment by sompylasar 17 Jan 10

    Am I missing something or the table above states the opposite to “Similarly, it is important to note that zero (0 and “0″) evaluates to true using 0.blank?.”

    It says FALSE, FALSE on rows with 0 and “0″ and column “x.blank?**”.

  • comment by hoopla 18 Mar 10

    i suffered the same issue when I began.

  • comment by James 27 May 10

    I am having this problem right now – thanks for clarifying!

  • comment by Nirmik 4 Sep 10

    It is really great

  • comment by Woodworking Machinery 6 Oct 10

    Nice rails

    Thanks it is useful

  • comment by PHP Development India 11 Oct 10

    Most of developers face the same problem when they switch the progamming from PHP to Rails.Thanks for sharing the difference between PHP and Rails empty variables.

  • comment by hotels in great barrington 1 Dec 10

    nice information about ruby on rails and really helpful one for php developers.

  • comment by web development 5 Dec 10

    I am looking for such type of informative news and i get through this blog so i am very much thankful to you for sharing such a great information.

  • comment by Opensource Deveploment 9 Jun 11

    You explained the concept of zero in a neat way. Thanks for such a great information. I was wondering for some information about ruby.Your post helped me well.

  • comment by Dividend Tax 30 Sep 11

    Thanks for sharing such a useful information sharing.

  • trackback by Tel zoznam 6 Dec 11

    Tel zoznam…

    [...]Rails for PHP Developers – Empty Variables[...]…

  • comment by jesica 18 Jan 12

    Thanks for sharing such a nice info.

  • comment by Nelson Kelem 23 Jan 12

    0 and “0″ are true always. Nothing is true as well as null. Everything in Ruby is an object. It either contains something or ‘nothing’. You just check for what is in it.
    So 0.blank? will evaluate to TRUE if its indeed blank or to FALSE if it contains something.

    Remember you are just checking for whether or not it has content. I think it uses “`0.length >= 0“`

Post a comment


We welcome your participation but please note we reserve the right to remove any comments that we think are not relevant or do not contribute to the discussion.