0

Possible Duplicate:
What does ||= mean in Ruby?

I'm new to ruby and I saw this being used in one of the answers here:

RACK_ENV = ENV['ENVIRONMENT'] ||= 'test'

I couldn't find any reference to the ||= operator...

Community
  • 1
  • 1
bluegray
  • 600
  • 8
  • 18
  • Indeed it is - didn't show up in search :/ – bluegray Mar 14 '12 at 09:31
  • I got lucky -- it was near the top of the _Related_ list on the right side; but you might want to consider the [Symbol Hound Search Engine](http://symbolhound.com/?q=||%3D) when you need to search for symbols in the future. I don't know how they do it but it's been supremely useful at things Google absolutely fails at. – sarnold Mar 14 '12 at 09:40
  • Wow, now that IS useful, thanks! – bluegray Mar 14 '12 at 10:12
  • Spread the good word. :) I've spent years cursing at Google's inability to do simple token searches and to finally have a solution that's magically right most of the time is pretty awesome. :) – sarnold Mar 14 '12 at 10:13
  • Duplicate: [What does `||=` mean in Ruby?](http://StackOverflow.Com/q/995593/), [What does `||=` mean in Ruby?](http://StackOverflow.Com/q/3800957/), [what is `||=` in ruby?](http://StackOverflow.Com/q/3945711/), [Double Pipe Symbols in Ruby Variable Assignment?](http://StackOverflow.Com/q/4500375/), [What does the “`||=`” operand stand for in ruby](http://StackOverflow.Com/q/5124930/), [what does a `||=` mean in Ruby language?](http://StackOverflow.Com/q/5230162/), [Is the ruby operator `||=` intelligent?](http://StackOverflow.Com/q/2989862/), … – Jörg W Mittag Mar 14 '12 at 17:38
  • … [What does `||=` mean?](http://StackOverflow.Com/q/7556902/), [What does “`||=`” do in Ruby 1.9.2?](http://StackOverflow.Com/q/7714803/) ['`||=`' operator in Ruby](http://StackOverflow.Com/q/8506257/), and probably many others as well. See also [The definitive list of `||=` (OR Equal) threads and pages](http://Ruby-Forum.Com/topic/151660/). – Jörg W Mittag Mar 14 '12 at 17:39

2 Answers2

1

here is the article which explains it really good.

« Ruby, concurrency,... | Main | How does one update... » The curious case of the Ruby T-Square operator. By prashant on Dec 14, 2008 The "||=" operator is interesting, both in what it does as much as in how it's widely used in Ruby land. The operator does not do what you would usually expect. i.e.,

a ||= expr

is not the same as

a = a || expr

The evaluation happens to be

a or a = expr

and the difference is important in at least one use case [0]

As a little DTrace script will verify, this operator is not implemented is a method(or anywhere in Ruby land) and is intrinsic to the VM. The reason is performance, and the fact that the entire expression does not have to be evaluated to yield a result when you're 'OR'ing:

"Ruby's Boolean operators are built into the language and are not based on methods: classes, for example, cannot define their own && method. Ruby defines special true and false values but does not have a Boolean type. method. The reason for this is that Boolean operators can be applied to any value and must behave consistently for any kind of operand."

. . .

"Another reason that Ruby's Boolean operators are a core part of the language rather than redefinable methods is that the binary operators are "short-circuiting." If the value of the operation is completely determined by the lefthand operand, then the righthand operand is ignored and is never even evaluated."

https://blogs.oracle.com/prashant/entry/the_ruby_t_square_operator

Mayank
  • 8,777
  • 4
  • 35
  • 60
-2

what does || do? If you have a and b then a || b is true if and only if either a or b is true. It is the same with ||= this operator combines two operations '=' and '||'. So a ||= b is equivelent to c || c = b

EDIT: so in your context ENV['ENVIRONMENT'] ||= 'test' means that if ENV['ENVIRONMENT'] is not nil and not false it will preserve its value, otherwise it will become 'test' and after that the new value of ENV['ENVIRONMENT'] is assigned to RACK_ENV

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • In fact it's `c || c = b`. If c is truthy, no assignment is done at all. This is an important difference when it's actually used the way it is used most of the time, i.e. as a optional initializer. – Holger Just Mar 14 '12 at 09:38
  • 1
    -1. This is wrong, as has been explained to death a gazillion times here on StackOverflow and on the Ruby mailing lists. – Jörg W Mittag Mar 14 '12 at 17:42