[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie question

Frantisek Psotka

3/10/2008 10:39:00 AM

is in ruby operator for:

a = b if b

a (operator) b

?
--
Posted via http://www.ruby-....

14 Answers

Farrel Lifson

3/10/2008 10:50:00 AM

0

On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:
> is in ruby operator for:
>
> a = b if b
>
> a (operator) b

a ||= b

Farrel

Sebastian Hungerecker

3/10/2008 11:02:00 AM

0

Farrel Lifson wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:

> > a = b if b
>
> a ||= b

Nope.
a ||= b is equivalent to a = a || b is equivalent to
a = if a then a else b end is NOT equivalent to a = b if b


HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Thomas Wieczorek

3/10/2008 11:03:00 AM

0

On Mon, Mar 10, 2008 at 11:50 AM, Farrel Lifson <farrel.lifson@gmail.com> wrote:
>
> a ||= b
>

That's not right. It will give you a NameError, that b is undefined.
a ||= b
is equivalent to
a = a || b
which is the same as
a = b if not a

>
> a = b if b
>

I know of no operator for it yet.

Jano Svitok

3/10/2008 11:05:00 AM

0

On Mon, Mar 10, 2008 at 11:50 AM, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:
> > is in ruby operator for:
> >
> > a = b if b
> >
> > a (operator) b
>
> a ||= b
>
> Farrel

a ||= b is

a = a || b
that is
a = b if !a
that is
a = b unless a

It is equal to "a = b if b" if both a and b are of boolean type
(doesn't work for e.g. numbers).

Alex Young

3/10/2008 11:08:00 AM

0

On Mon, 2008-03-10 at 19:50 +0900, Farrel Lifson wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.psotka@matfyz.cz> wrote:
> > is in ruby operator for:
> >
> > a = b if b
> >
> > a (operator) b
>
> a ||= b

Not quite:

irb(main):007:0> a = 1
=> 1
irb(main):008:0> b = 2
=> 2
irb(main):009:0> a ||= b
=> 1
irb(main):010:0>
irb(main):011:0* a
=> 1
irb(main):012:0> a = b if b
=> 2
irb(main):013:0> a
=> 2

a ||= b is equivalent to a = b if !a, which isn't quite the same thing
as a = b if b.

--
Alex


Lars Christensen

3/10/2008 11:08:00 AM

0

On Mar 10, 11:50 am, Farrel Lifson <farrel.lif...@gmail.com> wrote:
> On 10/03/2008, Frantisek Psotka <frantisek.pso...@matfyz.cz> wrote:
>
> > is in ruby operator for:
>
> > a = b if b
>
> > a (operator) b
>
> a ||= b

"a ||= b" means "a = b unless a".

I don't think there is an operator for "a = b if b".

Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or
a)" would also work.

Frantisek Psotka

3/10/2008 2:12:00 PM

0

yes, i can write

a = b || a

but isn't it more complex. imagine that b is nil. then a = a will be
evaluated?

maybe time for feature request? new operator a =|| (a = b if b)

(variable = params[:nice_symbol] if params[:nice_symbol] doesnt look
very nice)


Lars wrote:
> On Mar 10, 11:50 am, Farrel Lifson <farrel.lif...@gmail.com> wrote:
>> On 10/03/2008, Frantisek Psotka <frantisek.pso...@matfyz.cz> wrote:
>>
>> > is in ruby operator for:
>>
>> > a = b if b
>>
>> > a (operator) b
>>
>> a ||= b
>
> "a ||= b" means "a = b unless a".
>
> I don't think there is an operator for "a = b if b".
>
> Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or
> a)" would also work.

--
Posted via http://www.ruby-....

Radoslaw Bulat

3/10/2008 3:05:00 PM

0

T24gTW9uLCBNYXIgMTAsIDIwMDggYXQgMzoxMSBQTSwgRnJhbnRpc2VrIFBzb3RrYQo8ZnJhbnRp
c2VrLnBzb3RrYUBtYXRmeXouY3o+IHdyb3RlOgo+IHllcywgaSBjYW4gd3JpdGUKPgo+ICBhID0g
YiB8fCBhCj4KPiAgYnV0IGlzbid0IGl0IG1vcmUgY29tcGxleC4gaW1hZ2luZSB0aGF0IGIgaXMg
bmlsLiB0aGVuIGEgPSBhIHdpbGwgYmUKPiAgZXZhbHVhdGVkPwo+Cj4gIG1heWJlIHRpbWUgZm9y
IGZlYXR1cmUgcmVxdWVzdD8gbmV3IG9wZXJhdG9yIGEgPXx8IChhID0gYiBpZiBiKQo+Cj4gICh2
YXJpYWJsZSA9IHBhcmFtc1s6bmljZV9zeW1ib2xdIGlmIHBhcmFtc1s6bmljZV9zeW1ib2xdIGRv
ZXNudCBsb29rCj4gIHZlcnkgbmljZSkKCkFuZCBhZnRlciB4IHllYXJzIGxhdGVyIHdlIHdvdWxk
IGhhdmUgYW5vdGhlciBQZXJsLiBJIGRvbid0IGxpa2UgdG8KaGF2ZSBnYXppbGxpb25zIG9wZXJh
dG9ycyBmb3IgYWxsIG9jY2FzaW9ucy4KYSA9IGIgaWYgYiBpcyB0aGF0IGhhcmQgdG8gd3JpdGUg
YW5kIHJlYWQ/IEkgZG9uJ3QgdGhpbmsgc28gOikuCkRpcmVjdGlvbiBmb3IgUnVieSBmdXR1cmUg
c2hvdWxkIGJlIHRvIGNsZWFuIHRob3NlIHVnbHkgcGVybGlzbSBmcm9tClJ1YnkgKGZvciBleGFt
cGxlIG1hZ2ljIHZhcmlhYmxlICRfKSwgcmF0aGVyIHRoYXQgdG8gYWRkIG5ldyBzdHVmZgpsaWtl
IHRoYXQuCgotLSAKUmFkb3OzYXcgQnWzYXQKCmh0dHA6Ly9yYWRhcmVrLmpvZ2dlci5wbCAtIG3z
aiBibG9nCg==

Christopher Dicely

3/11/2008 3:30:00 AM

0

On Mon, Mar 10, 2008 at 7:11 AM, Frantisek Psotka
<frantisek.psotka@matfyz.cz> wrote:
> yes, i can write
>
> a = b || a
>
> but isn't it more complex. imagine that b is nil. then a = a will be
> evaluated?

Then why not just "a = b if b"?
>
> maybe time for feature request? new operator a =|| (a = b if b)
>
> (variable = params[:nice_symbol] if params[:nice_symbol] doesnt look
> very nice)

Doesn't bother me, its a lot more clear than variable =|| params[:nice_symbol].
More operators that look similar and have similar but subtly different meanings
is less immediate visual clarity.

William James

3/11/2008 4:42:00 AM

0

On Mar 10, 9:04 am, Radoslaw Bulat <radek.bu...@gmail.com> wrote:

> Direction for Ruby future should be to clean those ugly perlism from
> Ruby (for example magic variable $_)

$_ is no more magic than ARGV.

Paul Graham's new language Arc uses _ for a similar purpose.