What does this mean? ||=

I'm looking at this method and don't understand what ||= means:

def current_account
    @account ||= Account.find(session[:account_id])
end

I have the ruby for rails book and can't find anything on it.

···

--
Posted via http://www.ruby-forum.com/.

It does the following in this order:

if @account HAS A VALUE (is not nil) then DO NOTHING
  otherwise, compute Account.find(...) and set its return value into
@account.

It is useful whenever you want to avoid computing something if it already
holds a value.

Pedro.

···

On 1/12/07, Justin Ko <jko170@yahoo.com> wrote:

I'm looking at this method and don't understand what ||= means:

def current_account
    @account ||= Account.find(session[:account_id])
end

I have the ruby for rails book and can't find anything on it.

--
Posted via http://www.ruby-forum.com/\.

--
Pedro Fortuny Ayuso
C/Capuchinos 14, 1. 47006 Valladolid. SPAIN

var ||= value is

var = var || value

./alex

···

--
.w( the_mindstorm )p.

On 1/12/07, Justin Ko <jko170@yahoo.com> wrote:

I'm looking at this method and don't understand what ||= means:

def current_account
    @account ||= Account.find(session[:account_id])
end

I have the ruby for rails book and can't find anything on it.

--
Posted via http://www.ruby-forum.com/\.

If @account is nil it'll look in the database to find an account using
the account id from the session and return it; otherwise it'll just
return the current value of @account.

···

--
Posted via http://www.ruby-forum.com/.

Justin Ko wrote:

I'm looking at this method and don't understand what ||= means:

Much like 'x += 1' is a shortcut for 'x = x + 1', 'x ||= 1' is a
shortcut for 'x = x || 1'.

The significance of this is that the || method doesn't simply return a
boolean value; it checks the first argument, returns it if it evaluates
to 'true' (i.e. is something other than false or nil), and otherwise
returns the second argument. So it's commonly used as a shortcut for an
if/then statement, checking if the first argument is nil (or false). So
the following are all equivalent:

if @account
  @account
else
  Account.find(session[:account_id])
end

is the same as:

@account = @account || Account.find(session[:account_id])

is the same as:

@account ||= Account.find(session[:account_id])

Hope this helps!

···

--
Posted via http://www.ruby-forum.com/\.

Hi Justin,

I'm looking at this method and don't understand what ||= means:

def current_account
    @account ||= Account.find(session[:account_id])
end

A scheme I practice a lot:

  def meth parm = nil
    parm ||= "default"
    ...
  end

  ...

  a = some_calculation
  obj.meth a # a may be nil and still hits the default value

Bertram

···

Am Samstag, 13. Jan 2007, 04:52:29 +0900 schrieb Justin Ko:

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Just to be explicit, it's both 'nil' or 'false' that will be reset to
the new value:

irb(main):001:0> f = false
=> false
irb(main):003:0> f ||= 1
=> 1
irb(main):004:0> f
=> 1 # was set
irb(main):002:0> n = nil
=> nil
irb(main):005:0> n ||= 1
=> 1
irb(main):006:0> n
=> 1 # was set
irb(main):007:0> t = true
=> true
irb(main):008:0> t ||= 1
=> true
irb(main):009:0> t
=> true # wasn't set

HTH,
Keith

···

On 1/12/07, Pedro Fortuny Ayuso <pfortuny@gmail.com> wrote:

It does the following in this order:

if @account HAS A VALUE (is not nil) then DO NOTHING
  otherwise, compute Account.find(...) and set its return value into
@account.

HI --

Justin Ko wrote:

I'm looking at this method and don't understand what ||= means:

Much like 'x += 1' is a shortcut for 'x = x + 1', 'x ||= 1' is a
shortcut for 'x = x || 1'.

The significance of this is that the || method doesn't simply return a
boolean value; it checks the first argument, returns it if it evaluates
to 'true' (i.e. is something other than false or nil), and otherwise
returns the second argument. So it's commonly used as a shortcut for an
if/then statement, checking if the first argument is nil (or false). So
the following are all equivalent:

if @account
@account
else
Account.find(session[:account_id])
end

is the same as:

@account = @account || Account.find(session[:account_id])

is the same as:

@account ||= Account.find(session[:account_id])

Your first example doesn't set @account; it just tests it. You'd need
to do the assignment explicitly:

   @account = if @account
                @account
              else
                Account.find(session[:account_id]
              end

David

···

On Sat, 13 Jan 2007, Chris Gernon wrote:

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Great explanation! Thanks.

···

--
Posted via http://www.ruby-forum.com/.

Wow thanks, I thought I understood it before, but that example really spells
it out explicitly.

···

On 1/12/07, Keith Fahlgren <keith@audiobeta.com> wrote:

Just to be explicit, it's both 'nil' or 'false' that will be reset to
the new value:

irb(main):001:0> f = false
=> false
irb(main):003:0> f ||= 1
=> 1
irb(main):004:0> f
=> 1 # was set
irb(main):002:0> n = nil
=> nil
irb(main):005:0> n ||= 1
=> 1
irb(main):006:0> n
=> 1 # was set
irb(main):007:0> t = true
=> true
irb(main):008:0> t ||= 1
=> true
irb(main):009:0> t
=> true # wasn't set

HTH,
Keith