Quoteing bob.news@gmx.net, on Wed, Feb 04, 2004 at 05:35:03PM +0900:
Do you know about this syntax currently available?
a = “aaa”
b = “bbb”
case
when a == “aaa”, b == “bbb”
puts “Lowercase detected”
when a == “AAA”, b == “BBB”
puts “Uppercase detected”
else
puts “Detection failed”
end
I couldn’t find it in the Pickaxe - I’ll file it under “Hidden Treasures
of Ruby”…
There’s a lot of info jammed into the corners of the pickaxe book!
For this syntax, there’s an example in the section “Case Expressions”,
p84, and then in chapter 18 it shows the syntax:
case target
when comparison [, comparison]]… [then]
body
when comparison [, comparison]]… [then]
body
…
[ else
body ]
end
Cheers,
Sam
We are not talking about general “case” syntax, most of folks here are
well aware of it (thanks for refreshing it anyway ;-)). It is rather
about a special case when is not specified in “case” clause. It
looks like it defaults to “true” so that may contain
logical operators like “a == ”. And I could not find it
mentioned in the Pickaxe either.
It’s not tricky at all; it’s consistent with the way that case…when…end
works at all.
The form
case
when expr[, expr]+
when expr[, expr]+
else
end
Is roughly equivalent to
case true
when expr[, expr]+
when expr[, expr]+
else
end
In all cases, it’s going to be cond === expr || cond === epxr2 (etc.), so
that ‘,’ is effectively an “or” condition in all case…when…end
expressions. This is not a special case, so to speak.
-austin
···
On Thu, 5 Feb 2004 03:00:04 +0900, Guoliang Cao wrote:
What you’ve written here will do alternatives checks. Thus, if you
have:
a = “aaa”
b = “BBB”
then “Lowercase detected” will be printed. With “AAA” and “bbb”,
“Lowercase detected” will still be printed.
I think this is very tricky. ‘,’ equals to ‘||’ here. This should be made
very clear otherwise people should avoid use this form.
I'm fine with that. Thanks for taking a look, Matz.
Cao
···
---- Original Message ----
Hi,
In message "Re: RCR draft for enhanced "case..when..else..end" syntax" > on 04/02/05, Guoliang Cao <gcao@lemurnetworks.net> writes:
>Don't mix system design with language capabilities please. I think it's not
>a bad thing to let "case" to take more than one expressions. Just like a
>function can take any number of arguments. Why does "case" have to be
>limited to zero or one? Because all our languages are designed this way?
>(AFAIK only Ruby "case" can take 0 expressions. That's some good stuff
>introduced by Ruby, right? So why not go one step further?) Or because it's
>impossible?
Natural extension is not always a good thing. We can inherit from one
class, hey, why not inheriting from multiple classes? Because it
introduces complexity, mix-in is a better solution.
>Since 2.0 is a major update to 1.x, why can't we introduce new language
>constructs if they really makes sense?
You are right, we can, when (and only when) I agree with the new
design. I personally don't see much usage of your new syntax.
About my example, I don’t think people should do a complex design.
“if…elsif” is the answer in current Ruby capability.
I wonder why those who have informed that case which has no following
expressions is equivalent to `case true’, and when clause can take an
arbitrary expression, doesn’t try:
case
when a == "aaa" && b == "bbb"
puts "lowercase"
when a == "AAA" && b == "BBB"
puts "uppercase"
else
puts "???"
end
That looks nice, doesn’t it?
···
–
kjana@dm4lab.to February 5, 2004
It is comparison that makes men happy or miserable.
What you've written here will do alternatives checks. Thus, if you
have:
a = "aaa"
b = "BBB"
then "Lowercase detected" will be printed. With "AAA" and "bbb",
"Lowercase detected" will still be printed.
I think this is very tricky. ',' equals to '||' here. This should be made
very clear otherwise people should avoid use this form.
It's not tricky at all; it's consistent with the way that case..when..end
works at all.
I'm not saying it's inconsistent. What I mean by tricky is it's easy to
think "when a=='aaa', b=='bbb'" equals to "a=='aaa' and b=='bbb'"
especially when one never writes this kind of code by himself and reads
someone else's code.
Cao
···
---- Original Message ----
On Thu, 5 Feb 2004 03:00:04 +0900, Guoliang Cao wrote:
The form
case
when expr[, expr]+
when expr[, expr]+
else
end
Is roughly equivalent to
case true
when expr[, expr]+
when expr[, expr]+
else
end
In all cases, it's going to be cond === expr || cond === epxr2 (etc.), so
that ',' is effectively an "or" condition in all case..when..end
expressions. This is not a special case, so to speak.
It’s called lookup tables or object oriented programming using virtual
calls, as Robert Klemme implemented.
-austin
···
On Thu, 5 Feb 2004 04:27:06 +0900, Sean O’Dell wrote:
On Wednesday 04 February 2004 11:07 am, Guoliang Cao wrote:
About my example, I don’t think people should do a complex design.
“if…elsif” is the answer in current Ruby capability.
Personally, I like the idea of a more readable way to replace the
if…elsif paradigm. It’s a great idea!
About my example, I don’t think people should do a complex design.
“if…elsif” is the answer in current Ruby capability.
Personally, I like the idea of a more readable way to replace the
if…elsif
paradigm. It’s a great idea!
Hmm… As someone else pointed out, if you are in excessive need of such
a construct, you should consider redesign. Case constructs (or
if…elsif…end cascades) are regarded to be not very OO IMHO.
Regards
robert
···
On Wednesday 04 February 2004 11:07 am, Guoliang Cao wrote:
Please do not CC me on responses to c.l.r or ruby-talk, wherever you’re
reading this from. I will see the response just fine.
···
On Wed, 4 Feb 2004 13:49:15 -0500, Guoliang Cao wrote:
I think this is very tricky. ‘,’ equals to ‘||’ here. This should be
made very clear otherwise people should avoid use this form.
It’s not tricky at all; it’s consistent with the way that
case…when…end works at all.
I’m not saying it’s inconsistent. What I mean by tricky is it’s easy to
think “when a==‘aaa’, b==‘bbb’” equals to “a==‘aaa’ and b==‘bbb’”
especially when one never writes this kind of code by himself and reads
someone else’s code.
Not if one knows anything about the way that ‘case’ works. If I write:
case foo
when :a, :b, :c
I do not expect that to mean “when foo is :a and :b and :c”.