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.
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.
Ruby N. wrote:
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
2010/1/18 Ammar A. [email protected]:
end
else
@speaks_english = false
end
end
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
Robert K. wrote:
if @name.size > 6
if @name > 6
@speaks_english = @name.size > 6
Kind regardsrobert
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
2010/1/18 Ammar A. [email protected]:
@speaks_english ||= @name.size > 6
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?
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?)
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
2010/1/18 Ruby N. [email protected]:
On Mon, Jan 18, 2010 at 5:27 PM, Robert K.
[email protected] wrote:unless @speaks_english
@speaks_english = @name.size > 6
endThanks. 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 “=” 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
On Mon, Jan 18, 2010 at 5:27 PM, Robert K.
[email protected] wrote:
unless @speaks_english
 @speaks_english = @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 “=” in ruby.
On Mon, Jan 18, 2010 at 3:44 AM, Ruby N. [email protected]
wrote:
For this statement:
@speaks_english ||= @name.size > 6
does it mean something like below?
if @name.size > 6
@speaks_english ||= @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 &&= 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
A while back there was quite a thread on the Ruby-lang mailing list
about the real semantics of the expression.
a[x] ||= some_value
In many books and articles on Ruby the form:
a = b
Where is an operator like + is described as syntactic sugar for:
a = a b
While this is true in most cases, it isn’t true if the operator is ||.
…
Matz explains that the real expansion of x ||= y is:
x || (x = y)
…
Since || is a ‘short-circuit’ 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 ||= as an assignment operator
makes perfect sense to me. *** The ||= assignment operator
reserves the short-circuit nature of ||. ***
On Mon, Jan 18, 2010 at 11:54 AM, Colin B.
[email protected] wrote:
As an addition to the other replies to your question, there is a useful
post on this by Rick DeNatale, see below.
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.
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: Rick DeNatale - Developer - IBM | LinkedIn
在 2010-01-19二的 01:54 +0900,Colin B.写é“:
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.
For those people who were coming from Perl, ||= is a pretty clear
operator,
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs