Hulihan Applications
about projects portfolio services contact_us
Default Variable Values In Ruby

    

Setting default values for variables can be very useful in ruby. There are a variety of ways to do it and the way you assign default values all depends on how the value is being stored. Here's some examples:

Default variables in a method or function:

def print_my_name(name = "John Doe")

puts name

end

print_my_name # => "John Doe" 

This is the old fashioned way to set default variables in a method...Sure, it works, but it has one major drawback. The values you pass into this method have to be order, which can be annoying if you want to assign only one custom value in a method, and that arguments is at the end of the list of arguments passed into the method. Instead, I like to pass in a hash of value into a method and then handle default value assignment inside the method, instead of in the the method definition:

def print_my_name(options = {})

options[:name] ||= "John Doe"

puts options[:name]

end

print_my_name :name => "John Doe" #  => "John Doe"

This lets you pass in any value as you please, in any order, since everything is stored in a hash! This also has a slight downside to it: Since we're working with hash members, we have to assign default values to hash items the right way. This can vary depending on the data type of the variable. We can't Let's take a look:

def print_my_information(options = {})

  options[:name] ||= "John Doe" # default value for a string

  options[:age]    ||= 25 # default value for an integer/float

  options[:hates_pickles]    = true if options[:hates_pickles].nil?  # default value for a bool

  return options

end

print_my_information :name => "Dave", :age => "28", :hates_pickles => false # => {:age=>"28", :hates_pickles=>false, :name=>"Dave"}

I like to use ruby's ||=  operator, which checks a variable to see if it's defined, nil, or false. This works great for strings, chars, integers, and floats, but is horrible for booleans values(aka bools) because if you pass in a value for a bool that's false, it will get reassigned if you use the ||= operator, so never use this operator for default bool values. ever. If you make reference to an undefined hash item, ruby will return nil, so we can assign default bool values by checking if the hash key you're looking for is nil:

 

options[:hates_pickles] = true if options[:hates_pickles].nil?

 

This is one good way to handle default values in an method in ruby, and can come in very handy!

 


  0 Comments
Tags:


Installing Custom Gems on a Shared Host

    

Have you ever wanted to install any gem you want on your account with a shared hosting company, like hostmonster, bluehost, dreamhost, railsplayground, etc? Here's how you can do it!

 

Before we begin, you'll need ssh access to your account. Log into to your ssh now!

 

First, we must create a directory you can install local gems to

cd ~
mkdir .gem bin lib src

Next, install your own local executable of rubygems. This step is optional, but I like to have the latest version of gem on my account, so I can update it as needed. At the time of this post, the latest version of rubygems is 1.3.7.

cd ~/src
wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
tar -xzvf rubygems-1.3.*
cd rubygems-1.3.*
ruby setup.rb --prefix=$HOME
cd ~/bin
ln -s gem1.8 gem
cd ~

Next, add this  to the your user's .bash_profile file(~/.bash_profile), so the gem command knows where to install new gems:

export PATH="$HOME/bin:$HOME/.gem/bin:$PATH"
export RUBYLIB="$HOME/lib:$RUBYLIB"
export GEM_HOME="$HOME/.gem"
export GEM_PATH="/usr/lib/ruby/gem/1.8:$GEM_HOME"
alias gem="nice -n19 ~/bin/gem"

After this step is completely, re-login to your account to apply these changes.

Next, verify that the gem command you're using is in the right place:

which gem   # should return /home/USERNAME/bin/gem
gem -v      # should return 1.3.7

 

That's pretty much it! You can now install gems locally to your account using the command gem install [gemname].

Now, if your application needs access to a gem that's installed locally to your account, add this to your application's config/environment.rb or config.ru(if you're using a rackup config file).

That's it!

Special Thanks goes to Katz for her guide on installing gems on dreamhost.

 


  1 Comments


Happy Birthday Opal!

    

Opal's First Birthday!Today is Opal's first birthday! 

We released Opal one year ago today! This open-source Item Management System we develop has come quite a long way since then! In celebration of this wonderful occasion, we're releasing the newest version of Opal: 0.4.2. You can try out a demo or download it for free on the Opal Project Page.


  0 Comments




Hulihan Applications © 2007-2009
No portion of this site may be copied, altered, duplicated or otherwise used without the express written approval of Hulihan Applications.