Christian Mayer's Weblog

Set A Default Value For A Ruby Variable

Sometimes you see something like this in Ruby source code:

var1 ||= 123

Which is a pretty awesome way to express this:

var1 = var1 || 123

What in turn means:

var1 = 123 if !var1

Or the long version:

if !var1
  var1 = 123
end

And in the case you only speak PHP:

<?php
if(!$var1){
    $var1 = 123;
}

Posted on .
Categories: Programming
Tags: Ruby, Default

Imprint | Usage | Categories | RSS Feed
Copyright © 2006 by