||= with 1.8 and 1.9 ?

B

brabuhr

Hi --



It isn't, though, at least in the ||=3D case (see Joel W.'s post).
Matz's characterization of it to me at RubyConf (or somewhere) was:

=A0x ||=3D y =A0 same as =A0 =A0x || x =3D y

Yeah, that version matches my mental model.

x ||=3D y
x =3D x || y
x || x =3D y

x +=3D y
x =3D x + y
x + x =3D y # :)

o.x ||=3D y
o.x =3D o.x || y
# .x=3D is always called
o.x || o.x =3D y
# .x=3D is not always called

o.x +=3D y
o.x =3D o.x + y

Then maybe ||=3D and +=3D should be split apart in rubyspec:
describe "var +=3D expr" it "is equivalent to 'var =3D var op expr'"
describe "var ||=3D expr" it "is equivalent to 'var || var =3D expr'"
 
R

Rick DeNatale

It isn't, though, at least in the ||=3D case (see Joel W.'s post).
Matz's characterization of it to me at RubyConf (or somewhere) was:

=A0x ||=3D y =A0 same as =A0 =A0x || x =3D y

It's approximately the same as certainly closer than x =3D x || y

There's a difference in what happens if x is not previously defined


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
7

7stud --

I've never understood ||=. As a result, I would never use it in my
code. As far as I'm concerned, I can explicitly write what I want
rather than trust some unpredictable, quirky short hand operator.
 
7

7stud --

Brian said:
The parentheses in (10) are not delimiting arguments of a method call;
they are just bracketing a value expression. Note:

SyntaxError: compile error
(irb):3: syntax error, unexpected ',', expecting ')'
Foo.new.bar=(7,8,9)
^
from (irb):3
from :0
ArgumentError: wrong number of arguments (1 for 3)
from (irb):4:in `bar='
from (irb):4
from :0

Wow. Thanks for the explanation.
 
R

Rick DeNatale

Hi,

In message "Re: ||=3D with 1.8 and 1.9 ?"
=A0 =A0on Thu, 27 Aug 2009 01:20:20 +0900, Rick DeNatale <rick.denatale@g=
mail.com said:
|a =3D b =3D c
|
| is treated the same as
|
|b =3D c; a =3D c
|
|in other words the result of the b=3D is ignored.

No,

=A0a =3D b =3D c

is parsed as

=A0a =3D (b =3D c)

Yes, I was speaking a bit loosely and was trying to say just what you said =
next
so, result of the latter assignment is used for the former assignment.
The point is when b here is an attribute assignment (e.g. foo.b =3D c),
the result value from the setter method (b=3D) is ignored.

And I think that the really important point here is that

a.b ||=3D c

should have have the same effect as

a.b || a.b =3D c

And that it's surprising that in Ruby 1.9 it doesn't

puts RUBY_VERSION

class OrOrEquals
def test
@test
end

def test=3D(test)
@test =3D test
'not test'
end
end

o =3D OrOrEquals.new

direct =3D o.test =3D 'a test'
p direct
o.test =3D nil
with_oror =3D o.test ||=3D 'a test'
p with_oror

with_or_or_assign =3D o.test || o.test =3D "a test"
p with_or_or_assign

$ multiruby or_or_equals.rb
/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:144:
warning: /Users/rick/.gem/ruby/1.8/specifications: Permission denied

VERSION =3D 1.8.6-p368
CMD =3D ~/.multiruby/install/1.8.6-p368/bin/ruby or_or_equals.rb

1.8.6
"a test"
"a test"
"a test"

RESULT =3D 0

VERSION =3D 1.8.7-p160
CMD =3D ~/.multiruby/install/1.8.7-p160/bin/ruby or_or_equals.rb

1.8.7
"a test"
"a test"
"a test"

RESULT =3D 0

VERSION =3D 1.9.1-p0
CMD =3D ~/.multiruby/install/1.9.1-p0/bin/ruby or_or_equals.rb

1.9.1
"a test"
"not test"
"a test"

RESULT =3D 0


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top