Hello,
i think i once saw an assignment operator |= used in a ruby program.
I can't find anything in the pickaxe-book and on google.
What does it do?
Thanks, Stefan
Easily,
a |= b
is the same as
a = a | b
and | is bit-or.
It works just like you would expect
a = 3 # 0...011 in binary
a |= 4 # 0..0100 in binary
p a # prints 7 which 0..000111 in binary
···
On 7/29/05, Stefan Achatz <erazor@mitochondrien.de> wrote:
i think i once saw an assignment operator |= used in a ruby program.
I can't find anything in the pickaxe-book and on google.
What does it do?
It does the same as foo = foo | bar, where | is the bitwise operator.
It's more common to see foo ||= bar, which is foo = foo || bar.
-austin
···
On 7/29/05, Stefan Achatz <erazor@mitochondrien.de> wrote:
Hello,
i think i once saw an assignment operator |= used in a ruby program.
I can't find anything in the pickaxe-book and on google.
What does it do?
Thanks, Stefan
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
"Stefan Achatz" <erazor@mitochondrien.de> schrieb im Newsbeitrag news:42ea718c_1@news.arcor-ip.de...
Hello,
i think i once saw an assignment operator |= used in a ruby program.
I can't find anything in the pickaxe-book and on google.
What does it do?
Thanks, Stefan
'a |= b' is the same as 'a = a | b', where '|' is the or-method. There is one remarkable difference to '||'
(and '||='), because '||' may be lazy if the left operand evaluates to 'true', while using the '|'-method the
right operand is an argument for the method, so it will always be evaluated.
Best regards, Wolfgang
···
--
Wolfgang Nádasi-Donner
wonado@donnerweb.de
Well, it looks there are more to the meaning of the operator | as ri shows
$ ri "|"
More than one method matched your request. You can refine
your search by asking for information on one of:
Array#|, Bignum#|, FalseClass#|, Fixnum#|, NilClass#|, Set#|,
TrueClass#|
Each of these classes has | implementations, which should be related
one way or the other to the simple meaning of bit-or.
Regards,
Adriano.
Adriano Ferreira wrote:
···
On 7/29/05, Stefan Achatz <erazor@mitochondrien.de> wrote:
> i think i once saw an assignment operator |= used in a ruby program.
> I can't find anything in the pickaxe-book and on google. What does
> it do?
Easily,
a |= b
is the same as
a = a | b
and | is bit-or.
Well, not necessarily. For an array, the | operator is used to create
the (unique) union of the array and a second argument array,
nikolai
--
Nikolai Weibull: now available free of charge at http://bitwi.se/\!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}