what does this mean?

R

Ruby Newbee

For this statement:

@speaks_english ||= @name.size > 6

does it mean something like below?

if @name.size > 6
@speaks_english ||= @name.size
end


Even if I guess that correctly, I'm still surprised why ruby can write
like that way.

Thanks.
 
A

Ammar Ali

Ruby said:
For this statement:

@speaks_english ||= @name.size > 6

does it mean something like below?

if @name.size > 6
@speaks_english ||= @name.size
end


Even if I guess that correctly, I'm still surprised why ruby can write
like that way.

Thanks.
More like:

if @speaks_english.nil? or @speaks_english == false
if @name > 6
@speaks_english = true
else
@speaks_english = false
end
end
 
R

Robert Klemme

2010/1/18 Ammar Ali said:
More like:

if @speaks_english.nil? or @speaks_english =3D=3D false
=A0if @name > 6
=A0 @speaks_english =3D true
=A0else
=A0 @speaks_english =3D false
=A0end
end

I'd shorten that to

unless @speaks_english
@speaks_english =3D @name.size > 6
end

"@name.size > 6" is a boolean expression and it does return "true" or
"false" already.

Note that in this case this would probably a better solution because
it does not reevaluate if @name.size <=3D 6:

@speaks_english =3D @name.size > 6 if @speaks_english.nil?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

Ammar Ali

Robert said:
I'd shorten that to

unless @speaks_english
@speaks_english = @name.size > 6
end

"@name.size > 6" is a boolean expression and it does return "true" or
"false" already.

Note that in this case this would probably a better solution because
it does not reevaluate if @name.size <= 6:

@speaks_english = @name.size > 6 if @speaks_english.nil?

Kind regards

robert

I intentionally expanded || to nil? or false tests, and "unrolled" the
assignment to @name.size > 6 to illustrate the boolean result. I hoped
the painfully procedural rendering would get the point across quickly
and maybe cause some head scratching (where did all that come from?)

I should have shown some of the other elegant ways to achieve this in
ruby, like Robert did. Or at least pointed out that what I posted is not
the way to do things. I guess I assumed that no one would actually type
what I did once they figured out what the one liner does.

Thanks,
ammar
 
R

Robert Klemme

2010/1/18 Ammar Ali said:
I intentionally expanded || to nil? or false tests, and "unrolled" the
assignment to @name.size > 6 to illustrate the boolean result. I hoped th= e
painfully procedural rendering would get the point across quickly and may= be
cause some head scratching (where did all that come from?)

Well, the question is how many levels deep one "unrolls" the code. I
tried to stick with one level but it's as valid of course to go
deeper. I just felt that it might obscure the answer to the original
question a bit.
I guess I assumed that no one would actually type what I did
once they figured out what the one liner does.

Absolutely agree.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Ruby Newbee

unless @speaks_english
=C2=A0@speaks_english =3D @name.size > 6
end

Thanks. This is much clearer than the original one which I do don't like.
But I also at the first time know that ">" has priority to "=3D" in ruby.
 
R

Robert Klemme

2010/1/18 Ruby Newbee said:
Thanks. This is much clearer than the original one which I do don't like.
But I also at the first time know that ">" has priority to "=3D" in ruby.

That's very common among programming languages. Assignment has
generally low precedence in order to allow for arbitrary expressions
on the right hand side without the necessity for brackets.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
C

Colin Bartlett

For this statement:
@speaks_english ||=3D @name.size > 6
does it mean something like below?
if @name.size > 6
=A0 =A0@speaks_english ||=3D @name.size
end

As an addition to the other replies to your question, there is a useful
post on this by Rick DeNatale, see below.
Rick DeNatale also notes that x &&=3D y behaves similarly - it means:
x && (x =3D y)

I've emphasized the last sentence below because although
I know the real meaning of ||=3D the point that it preserves
the short-circuit nature hadn't occurred to me, and I think
it's a useful help to remembering exactly what ||=3D does.

http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux

A while back there was quite a thread on the Ruby-lang mailing list
about the real semantics of the expression.
a[x] ||=3D some_value
In many books and articles on Ruby the form:
a <op>=3D b
Where <op> is an operator like + is described as syntactic sugar for:
a =3D a <op> b
While this is true in most cases, it isn't true if the operator is ||.
...
Matz explains that the real expansion of x ||=3D y is:
x || (x =3D y)
...
Since || is a =91short-circuit=92 boolean operator,
the left hand operand expression is only evaluated
if the right hand operand expression evaluates
to a logically false value, i.e. either nil or false.

The way that Matz included ||=3D as an assignment operator
makes perfect sense to me. *** The ||=3D assignment operator
reserves the short-circuit nature of ||. ***
 
R

Rick DeNatale

As an addition to the other replies to your question, there is a useful
post on this by Rick DeNatale, see below.

http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux

Which hopefully is accessible. I've been having an issue with my ISP.
I had an outage a couple of weeks ago, and just found out yesterday
that port 80 has not apparently been getting through to my server. I
didn't realize it because my router could get to it from inside the
lan.

It appears to be working now (I can reach the server from my iPhone
with wi-fi turned out) but I'm still waiting to hear back from my ISP
about whatever they did to fix it and it might not stay fixed
depending on what they are mucking with.
--
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
 
J

Jeff Peng

在 2010-01-19二的 01:54 +0900,Colin Bartlett写é“:
As an addition to the other replies to your question, there is a useful
post on this by Rick DeNatale, see below.
Rick DeNatale also notes that x &&= y behaves similarly - it means:
x && (x = y)

I've emphasized the last sentence below because although
I know the real meaning of ||= the point that it preserves
the short-circuit nature hadn't occurred to me, and I think
it's a useful help to remembering exactly what ||= does.

http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux

For those people who were coming from Perl, ||= is a pretty clear
operator, :)
 

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,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top