Update Var if Var==nil

Would

kindergarden ||= 0

do what you’re looking for?

···

-----Original Message-----
From: Dominik Werder [mailto:dwerder@gmx.net]
Sent: 16 June 2003 12:42
To: ruby-talk@ruby-lang.org
Subject: update Var if Var==nil

Hi there,

I’m doing often things like:

kindergarden=0 if kindergarden<0

Is there any super cool syntax magic to do something like:

kindergarden=0 if its<0 # ← no need to repeat the var name?

It would be cool especially with long var names, or on arrays
with long
indices!

bye!
Dominik


This communication (including any attachments) contains confidential information. If you are not the intended recipient and you have received this communication in error, you should destroy it without copying, disclosing or otherwise using its contents. Please notify the sender immediately of the error.

Internet communications are not necessarily secure and may be intercepted or changed after they are sent. Abbey National Treasury Services plc does not accept liability for any loss you may suffer as a result of interception or any liability for such changes. If you wish to confirm the origin or content of this communication, please contact the sender by using an alternative means of communication.

This communication does not create or modify any contract and, unless otherwise stated, is not intended to be contractually binding.

Abbey National Treasury Services plc. Registered Office: Abbey National House, 2 Triton Square, Regents Place, London NW1 3AN. Registered in England under Company Registration Number: 2338548. Regulated by the Financial Services Authority (FSA).


kindergarden ||= 0

do what you’re looking for?

Yes, it does :slight_smile:
But could you tell me where I can read more about this operator?
google doesn’t like the search term “||=” :frowning:

thanks!
Dominik

Would

kindergarden ||= 0

do what you’re looking for?

if you’re using the subject as his question, yes, but if you’re using
his example (below), no. (i.e. consider the case if kindergarden is
-1.)

···

— “Woodhouse, Mike (ANTS)” mike.woodhouse@ants.co.uk wrote:

kindergarden=0 if kindergarden<0


Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!

“Woodhouse, Mike (ANTS)” mike.woodhouse@ants.co.uk schrieb im
Newsbeitrag
news:DB3429229181E341996812BE7A3BECAA053D17B6@lonre001.ants.anplc.co.uk

Would

kindergarden ||= 0

This only assigns if kindergarden is nil or false. The OP wanted <0 as
condition.

I think, with <0 it can’t be done without mentioning the variable twice,
since assignment always needs an lvalue on the left and the condition
needs the variable, too.

Regards

robert
···

-----Original Message-----
From: Dominik Werder [mailto:dwerder@gmx.net]
Sent: 16 June 2003 12:42
To: ruby-talk@ruby-lang.org
Subject: update Var if Var==nil

Hi there,

I’m doing often things like:

kindergarden=0 if kindergarden<0

Is there any super cool syntax magic to do something like:

kindergarden=0 if its<0 # ← no need to repeat the var name?

It would be cool especially with long var names, or on arrays
with long
indices!

bye!
Dominik


This communication (including any attachments) contains confidential
information. If you are not the intended recipient and you have received
this communication in error, you should destroy it without copying,
disclosing or otherwise using its contents. Please notify the sender
immediately of the error.

Internet communications are not necessarily secure and may be
intercepted or changed after they are sent. Abbey National Treasury
Services plc does not accept liability for any loss you may suffer as a
result of interception or any liability for such changes. If you wish to
confirm the origin or content of this communication, please contact the
sender by using an alternative means of communication.

This communication does not create or modify any contract and, unless
otherwise stated, is not intended to be contractually binding.

Abbey National Treasury Services plc. Registered Office: Abbey National
House, 2 Triton Square, Regents Place, London NW1 3AN. Registered in
England under Company Registration Number: 2338548. Regulated by the
Financial Services Authority (FSA).


It does
a = 0 unless a
instead of
a = 0 if a < 0

···

On Mon, Jun 16, 2003 at 09:19:22PM +0900, Dominik Werder wrote:

kindergarden ||= 0

do what you’re looking for?

Yes, it does :slight_smile:

      =====

But could you tell me where I can read more about this operator?
google doesn’t like the search term “||=” :frowning:

It works like other operators: +=, -=, *=…
and means

whatever = whatever || rhs

will only evaluate the rhs if the lhs is false or nil.

Another implication is that when you define for instance Object#+ you
get += for free. And that

string += “bla”

creates a new string corresponding to the concatenation of string and
“bla”.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

  • DDD no longer requires the librx library. Consequently, librx
    errors can no more cause DDD to crash.
    – DDD

Would

kindergarden ||= 0

do what you’re looking for?

if you’re using the subject as his question, yes, but if you’re using
his example (below), no. (i.e. consider the case if kindergarden is
-1.)

You’re right.

I tried the following:

irb(main):012:0> kindergarden<0 ||=0
SyntaxError: compile error
(irb):12: syntax error
kindergarden<0 ||=0
^
from (irb):12

[snip]
But could you tell me where I can read more about this operator?

google doesn’t like the search term “||=” :frowning:
[snip]

The explanation could be added to

which is a good reference to lots of symbols.

···

On Monday, June 16, 2003, at 08:19 AM, Dominik Werder wrote:

if you’re using the subject as his question, yes, but if you’re
using
his example (below), no. (i.e. consider the case if kindergarden
is -1.)

You’re right.

I tried the following:

irb(main):012:0> kindergarden<0 ||=0
SyntaxError: compile error
(irb):12: syntax error
kindergarden<0 ||=0
^
from (irb):12

Well, yeah; that was my point. =) His subject asked (or at least
implies) about setting a var if it’s not set, for which “||=” is
perfect. But his example asks about setting something to 0 if it’s
not 0 already (< 0, i.e. negative), which is somewhat different.

The code you used in irb doesn’t quite make sense. (To me, anyway,
and evidently to ruby as well.) “kindergarden<0” is a boolean
expression, not a (var) name.

···

Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!