newbie question

S

Sebastian Hungerecker

Farrel said:

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
 
J

Jano Svitok

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).
 
A

Alex Young


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

Lars


"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.
 
F

Frantisek Psotka

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)
 
R

Rados³aw Bu³at

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

Christopher Dicely

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

William James

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

Benjamin Oakes

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)



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

I guess I don't understand what the point of having a = b if b is.
The only thing I can see that's different than a ||= b is that false
will become nil. Here's what I tried:

def demo(some_value)
out = some_value if some_value
puts("out: #{out.inspect}")
out = nil || some_value
puts("out: #{out.inspect}")
out = some_value
puts("out: #{out.inspect}")
a_different_variable ||= some_value
puts("a_different_variable: #{a_different_variable.inspect}")
puts('------------')
end

demo(nil)
demo(88)
demo(false)

Outputs:

out: nil
out: nil
out: nil
a_different_variable: nil
------------
out: 88
out: 88
out: 88
a_different_variable: 88
------------
out: nil
out: false
out: false
a_different_variable: false
------------

So... Why doesn't ||= work for how you're using it? (Why do you want
the "b if b" behavior?)

-- Ben (aka divokz)
 
T

Todd Benson

I guess I don't understand what the point of having a = b if b is.
The only thing I can see that's different than a ||= b is that false
will become nil. Here's what I tried:

def demo(some_value)
out = some_value if some_value
puts("out: #{out.inspect}")
out = nil || some_value
puts("out: #{out.inspect}")
out = some_value
puts("out: #{out.inspect}")
a_different_variable ||= some_value
puts("a_different_variable: #{a_different_variable.inspect}")
puts('------------')
end

demo(nil)
demo(88)
demo(false)

Outputs:

out: nil
out: nil
out: nil
a_different_variable: nil
------------
out: 88
out: 88
out: 88
a_different_variable: 88
------------
out: nil
out: false
out: false
a_different_variable: false
------------

So... Why doesn't ||= work for how you're using it? (Why do you want
the "b if b" behavior?)

-- Ben (aka divokz)

Because your first test will be different if "out" already exists and
is not nil. The OP wants "out" to change only if "some_variable" is
not nil.

Todd
 
R

Rados³aw Bu³at

MjAwOC8zLzExIFdpbGxpYW0gSmFtZXMgPHdfYV94X21hbkB5YWhvby5jb20+Ogo+IE9uIE1hciAx
MCwgOTowNCBhbSwgUmFkb3OzYXcgQnWzYXQgPHJhZGVrLmJ1Li4uQGdtYWlsLmNvbT4gd3JvdGU6
Cj4KPiAgPiBEaXJlY3Rpb24gZm9yIFJ1YnkgZnV0dXJlIHNob3VsZCBiZSB0byBjbGVhbiB0aG9z
ZSB1Z2x5IHBlcmxpc20gZnJvbQo+ICA+IFJ1YnkgKGZvciBleGFtcGxlIG1hZ2ljIHZhcmlhYmxl
ICRfKQo+Cj4gICRfIGlzIG5vIG1vcmUgbWFnaWMgdGhhbiBBUkdWLgoKSSBkb24ndCBnZXQgaXQu
IEFSR1YgaXMgZm9yIHByb2dyYW0gcGFyYW1ldGVycy4gQWxsIGxhbmd1YWdlcyBoYXZlIGl0CmJ1
dCBvZiBjb3Vyc2UgbWF5YmUgaW4gZGlmZmVyZW50IHdheSAobGlrZSBqYXZhIFN0cmluZ1tdIGFy
Z3MgaW4gbWFpbgptZXRob2QpLiBXaGVyZSBpcyBtYWdpYyBoZXJlPwoKCgotLSAKUmFkb3OzYXcg
QnWzYXQKCmh0dHA6Ly9yYWRhcmVrLmpvZ2dlci5wbCAtIG3zaiBibG9nCg==
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top