could explain syntax "||="?

The "|| nil" is redundant in this case.

Basically, ||= is shorthand for

@country = unless @country

aka, set the variable if the variable is currently 'nil'


= is pretty confusing to many people

x ||= y
expands to
x or x = y

It is fairly simple unless x in a hash or array, then it gets hairy.

Here is David Black's discussion on the topic

For a really drawn out discussion and lots of arguing go here:


Here's a more verbose translation:

if x.nil? or x == false then
  x = y

(Both of those alternatives return the value in x--the first one after
the assignment from y.)

Usually what's meant is "give me the value in x, if there is one, but if
x is nil, then first assign whatever's in y to x, and *then* give me the
value in x". So this method in one of my models:

  def self.get_list
    @@all_organizations ||= self.find(:all, :select => 'id, name')

Will only hit the database once. After the first time,
@@all_organizations won't be nil and ruby will just return the list
previously fetched.