Set A Default Value For A Ruby Variable

Published on Christian Mayer's Weblog

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;
}

Recent Posts

About the Author

Christian is a professional software developer living in Vienna, Austria. He loves coffee and is strongly addicted to music. In his spare time he writes open source software. He is known for developing automatic data processing systems on Debian Linux server.

Categories: Programming
Tags: Ruby, Default

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