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 Newbee wrote:
For this statement:
@speaks_english ||= @name.size > 6
does it mean something like below?
if @name.size > 6
@speaks_english ||= @name.size
endEven 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
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 ||= some_value
In many books and articles on Ruby the form:
a <op>= b
Where <op> is an operator like + is described as syntactic sugar for:
a = 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 ||= 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 3:44 AM, Ruby Newbee <rubynewbee@gmail.com> wrote:
For this statement:
@speaks_english ||= @name.size > 6
does it mean something like below?
if @name.size > 6
@speaks_english ||= @name.size
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
2010/1/18 Ammar Ali <ammarabuali@gmail.com>:
Ruby Newbee wrote:
For this statement:
@speaks_english ||= @name.size > 6
does it mean something like below?
if @name.size > 6
@speaks_english ||= @name.size
endEven if I guess that correctly, I'm still surprised why ruby can write
like that way.More like:
if @speaks_english.nil? or @speaks_english == false
if @name > 6
@speaks_english = true
else
@speaks_english = false
end
end
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
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.
On Mon, Jan 18, 2010 at 11:54 AM, Colin Bartlett <colinb2r@googlemail.com> wrote:
As an addition to the other replies to your question, there is a useful
post on this by Rick DeNatale, see below.
--
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
在 2010-01-19二的 01:54 +0900,Colin Bartlett写道:
On Mon, Jan 18, 2010 at 3:44 AM, Ruby Newbee <rubynewbee@gmail.com> wrote:
> For this statement:
> @speaks_english ||= @name.size > 6
> does it mean something like below?
> if @name.size > 6
> @speaks_english ||= @name.size
> endAs 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.
For those people who were coming from Perl, ||= is a pretty clear
operator,
Robert Klemme wrote:
Ruby Newbee wrote:
For this statement:
@speaks_english ||= @name.size > 6
does it mean something like below?
if @name.size > 6
@speaks_english ||= @name.size
endEven if I guess that correctly, I'm still surprised why ruby can write
like that way.
More like:
if @speaks_english.nil? or @speaks_english == false
if @name > 6
@speaks_english = true
else
@speaks_english = false
end
end
I'd shorten that tounless @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
2010/1/18 Ammar Ali <ammarabuali@gmail.com>:
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 5:27 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
unless @speaks_english
@speaks_english = @name.size > 6
end
Robert Klemme wrote:
Ruby Newbee wrote:
For this statement:
@speaks_english ||= @name.size > 6
does it mean something like below?
if @name.size > 6
@speaks_english ||= @name.size
endEven if I guess that correctly, I'm still surprised why ruby can write
like that way.More like:
if @speaks_english.nil? or @speaks_english == false
if @name > 6
@speaks_english = true
else
@speaks_english = false
end
endI'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?
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 Ammar Ali <ammarabuali@gmail.com>:
2010/1/18 Ammar Ali <ammarabuali@gmail.com>:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
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
2010/1/18 Ruby Newbee <rubynewbee@gmail.com>:
On Mon, Jan 18, 2010 at 5:27 PM, Robert Klemme > <shortcutter@googlemail.com> 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.
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/