Question: I'm looking over someone's codes and wondering about the difference between: 

```
def blah
  @hello ||= [1,2,3].collect{|x| x+1}
end

```

and

```
def blah 
  @hello = [1,2,3].collect{|x| x+1}
end

```

I understand that `||=` means "or equal", but why do we need it? An example would be great.

Also, for the `collect` method, let's say I have an array:

```
a = [1,2,4,5]

```

and I wanted to find the array that contains integers that are greater than `2`, how can I use `collect` with that?

```
a.collect{|x| x>2} # => [false, false, true, true]

```

I want `[4,5]`.

Answer: 
`||=` does nothing if the variable has been assigned a value (other than `false` or `nil`):

```
irb(main):001:0> blah ||= 'foo'
=> "foo"
irb(main):002:0> blah ||= 'bar'
=> "foo"
irb(main):003:0> puts blah
foo
=> nil
irb(main):004:0> blah = nil
=> nil
irb(main):005:0> blah ||= 'bar'
=> "bar"
irb(main):006:0> puts blah
bar
=> nil

```

One possible use is to assign variables in a fault-tolerant way. Compare:

```
@data = get_data_from_some_source
@data ||= get_data_from_fallback_source
@data ||= get_data_from_last_resort_source

```

with:

```
@data = get_data_from_some_souce
if @data == nil
  @data = get_data_from_fallback_source
end
if @data == nil
  @data = get_data_from_last_resort_source
end

```

or even (saints preserve us):

```
begin
  @data = get_data_from_some_source
rescue GettingDataDidntWorkException
  begin
    @data = get_data_from_fallback_source
  rescue GettingDataDidntWorkException
    @data = get_data_from_last_resort_source
  end
end

```

Haskell programmers will recognize this as similar in use to the `Maybe` monad.
> 
> a = [1,2,4,5] and I wanted to find the array that contains integers that are greater than 2,
> 
> 
> 

Here we go using [`Array#select`](http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-select):

```
 a.select{|e| e > 2 } # => [4,5]

```

Explanation of the part `@hello ||= [1,2,3].collect{|x| x+1}` can be found [What does ||= (or equals) mean in Ruby?](https://stackoverflow.com/questions/995593/what-does-or-equals-mean-in-ruby)