Monthly Archives: November 2013

Upgrade to Ruby 2.0.0-x

To upgrade your ruby version follow those steps.

First run the following commands:

rvm get head

followed by:

rvm requirements

If you don’t get any errors you can run:

rvm install 2.0.0-x

To set as your current version of Ruby run the following command:

rvm use 2.0.0-x

To make it the default Ruby:

rvm default 2.0.0-x

Or you can run single command for both use and set default:

rvm use 2.0.0-x --default

Now ruby 2.0.0-x is the default version. To check ruby version run:

ruby -v

and where it’s located executing the command:

which ruby

That’s it. Happy coding πŸ™‚

Understanding .nil? .empty? .blank? .present? in Ruby on Rails

nil?, empty? and blank? and .present? are confusing for beginner.

Developer needs to be careful in using them and it is better that we understand it well before using it.

Let’s see which method does what.

.nil?

– It is Ruby method
– It can be used on any object and is true if the object is nil.
– “Only the object nil responds true to nil?” – RailsAPI

In Ruby, all classes inherit from the Object class. nil? is a method of Object; therefore, unless explicitly overridden, all classes have access to nil?. To better understand how Ruby expresses the concept of a non existent object, we need to look at its pseudo-variable nil. The same idea in other languages is often mapped to the primitive type, null or NULL. Depending on the language, NULL may point to nothing at all. Ruby differs here, in that, nil references an actual class called NilClass which exposes methods. This is why it’s possible to do seemingly illogical things like:

nil.nil?
=> true

When calling on an object that is non existent, invalid or explicitly set to nil, an instance of the singleton NilClass is returned. A few examples will illustrate how this works.

books = {:physics => "quantam mecanics", :chemistry => "organic chemistry"}

books[:mathmatics]
=> nil
books[:mathmatics].class
=> NilClass
books[:mathmatics].nil?
=> true
books[:physics].nil?
=> false

test_var = nil
test_var.nil?
=> true

[].nil?
=> false

"".nil?
=> false

0.nil?
=> false

false.nil?
=> false

empty?

Unlike nil?, empty? is only available on some Ruby objects. String, Hash and Array are a few of the classes that implement it. When calling empty? the receiving object is being checked to see if it possesses a non-nil value or values. empty? (at least for the mentioned classes) returns either true or false.

– It is Ruby method
– can be used on strings, arrays and hashes and returns true if:
String length == 0
Array length == 0
Hash length == 0
– Running .empty? on something that is nil will throw a NoMethodError

["physics", "chemistry", "mathmatics"].empty?
=> false

[""].empty?
=> false

[].empty?
=> true

{}.empty?
=> true

"".empty?
=> true

0.empty?
=> NoMethodError: undefined method `empty?' for 0:Fixnum

test_var2 = nil
test_var2.empty?
= > NoMethodError: undefined method `empty?' for nil:NilClass

The last example above clearly illustrates why one would get unexpected results when trying to do things like this:

dog = {:name => "Beauregard"}
puts "What kind?" if dog[:breed].empty?
=> NoMethodError: undefined method `empty?' for nil:NilClass

# Use nil? instead.
puts "What kind?" if dog[:breed].nil?
=> What kind?

.blank?

– It is Rails method
– operate on any object as well as work like .empty? on strings, arrays and hashes.
– It also evaluates true on strings which are non-empty but contain only whitespace:

β€œAn object is blank if itβ€˜s false, empty, or a whitespace string.
For example, “”, ” “, nil, [], and {} are blank.”

For instance, I often use blank? when operating on form field values that are considered optional by a model. Going back to the dog hash, we can see how blank? simplifies testing for values.

nil.blank? = true 
[].blank? = true 
{}.blank? = true 
"".blank? = true 
5.blank? == false
dog = {:name => "Beauregard"}
puts "What kind?" if dog[:breed].blank?
=> What kind?

dog = {:name => "Beauregard", :breed => ""}
puts "What kind?" if dog[:breed].blank?
=> What kind?
"  ".blank? == true"  ".empty? == false

present?
Quick tip: !obj.blank? == obj.present?
activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9)

def present? 
 !blank?
end

Happy Coding πŸ™‚

Access Rails Development Server From A Different Computer

Sometime you need to access your rails app from a different computer. Here is a trick.

To access your rails app from different computer, you need to run rails app with IP.
While starting the webrick server specify the IP on which your rails application will run (192.168.1.x in your case) using -b option, it binds Rails to the specified IP.

rails server -b 192.168.1.x -p 3000

That’s it. Happy coding πŸ™‚