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]
.