case true
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
Sorta like cond in lisp…
Nice!! You should add it to the RubyIdioms wiki page.
At Sun, 8 Dec 2002 06:18:52 +0900, > Joel VanderWerf wrote:
x = 1.5
case
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
If no expression after “case”, “when” clause evaled as true
will be executed.
At Sun, 8 Dec 2002 06:18:52 +0900, > Joel VanderWerf wrote:
x = 1.5
case
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
If no expression after “case”, “when” clause evaled as true
will be executed.
Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.
(This is based on my >20 year old memory of FORTRAN 77, so I could be
off in the details. I believe “computed GOTO” is deprecated in current
FORTRANs.
I hope it’s deprecated. I take me 3 days to debug one of my FORTRAN 4
program written with computed GOTO (ACP program) and it’s worst than a
case statement. But I always write stupid things :-))
The ocaml bytecode interpreter on gcc runs 50% faster than the equivalent on
MSVC despite the fact that MSVC is generally optimizing better than gcc. The
reason? gcc supports computed gotos which apparently allows the interpreter
to translate opcodes to addresses of subroutines, thus avoiding an extra
computational step for each instruction.
Another thing that would be neat to see, in my opinion, is a ‘caseall’
···
–
joe = 50
jane = 45
caseall
when joe > jane
puts “Joe is older than Jane”
when jane > joe
puts “Jane is older than Joe”
when 40 < joe && joe < 60
puts “Joe is middle aged”
when 40 < jane && jane < 60
puts “Jane is middle aged”
else
puts “Joe and Jane are the same age, and neither is middle aged”
end
–
In this case, it should print
“Joe is older than Jane”
“Joe is middle aged”
“Jane is middle aged”
The ‘else’ clause is only executed if none of the rest match.
I’ve used a construct like that in other languages. While it’s the same as
a bunch of if statements, it is a bit cleaner.
(This is based on my >20 year old memory of FORTRAN 77, so I could be
off in the details. I believe “computed GOTO” is deprecated in current
FORTRANs.
I hope it’s deprecated. I take me 3 days to debug one of my FORTRAN 4
program written with computed GOTO (ACP program) and it’s worst than a
case statement. But I always write stupid things :-))
The ocaml bytecode interpreter on gcc runs 50% faster than the equivalent on
MSVC despite the fact that MSVC is generally optimizing better than gcc. The
reason? gcc supports computed gotos which apparently allows the interpreter
to translate opcodes to addresses of subroutines, thus avoiding an extra
computational step for each instruction.
lisp and clipper are greate languages but… Charles Wetherell in
“Etudes for Programmers” (1978) describes language Mini which has
operator closest to Ruby case:
select str of
case (“on”): return 1;
case (“off”,“”): return 0;
otherwise: return -1;
end select
it’s the beast SELECT construction i ever seen and i was very pleased
what ruby includes just the same SELECT operator
Tuesday, December 10, 2002, 8:54:18 PM, you wrote:
···
If the latter, it has already existed in another language and looks like
this:
One example where I have seen continuous improvement is with Chuck
Moore’s version of Forth (please ignore the broken record ;-).
His latest effort, called ColorForth, is smaller, faster, and
arguably, more elegant and powerful than previous incarnations.
By way of example, imagine a full VLSI CAD system, graphics, simulator,
and all, in around 500 lines of code (under 20k).
The Forth community, which likes ANSI stuff, is headed in a different
direction. And most people find the “continuous improvements” take
away their “favourite features”.
Chuck is not sentimental, from what I can tell. And not attached to
convention (a 24-key keyboard? Huffman encoding, not ASCII? Color
instead of punctuation?). Are these “less good”? Hard to say,
unless you just look at the results.
Regards,
-mark.
···
At 05:23 AM 12/8/2002 +0900, Dave Thomas wrote:
Unfortunately, many folks seem to feel that continuous improvement means
continuous accretion of features. I have a theory that in really, really
good designs, the total amount of stuff stays the same over time. As
improvements are added, less good things are discarded.
That may be true, within limits… I think even a good design
has some elasticity in it. I certainly don’t favor endless
expansion, but neither would I favor dumping something every
time a feature was added.
And no design is so good that it does not ultimately lead to
greater things.
Ruby is my favorite language – for what my opinion is worth,
as I’m not a language fanatic. But I don’t expect it will always be my favorite language. In 2016, I hope to be
programming in something I like better, if my brain has not
fossillized. And I hope it is something that borrows heavily
from Ruby, and learns from mistakes that we don’t even yet
perceive.
But now I’m just indulging in idle dreaming.
Hal
···
----- Original Message -----
From: “Dave Thomas” dave@pragprog.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, December 07, 2002 2:23 PM
Subject: Re: (OT) Re: elseif?
On Sat, 2002-12-07 at 13:04, Hal E. Fulton wrote:
But of course, it’s the kai-zen principle at work. Take a hundred
baby steps forward, and suddenly it looks like a giant quantum leap.
That, I think, is why we like Ruby – or part of the reason – or
that’s true for me, anyway.
Unfortunately, many folks seem to feel that continuous improvement means
continuous accretion of features. I have a theory that in really, really
good designs, the total amount of stuff stays the same over time. As
improvements are added, less good things are discarded.
This isn’t easy in a programming language, where throwing stuff away
breaks backward compatibility.
I’d like to learn LISP and Latin too. Maybe if someone coded a LISP
environment whose interface (and code) was entirely in Latin… two birds one
stone???
There’s a perl module that does almost precisely that.
case true
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
Sorta like cond in lisp…
Nice!! You should add it to the RubyIdioms wiki page.
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
If no expression after “case”, “when” clause evaled as true
will be executed.
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
If no expression after “case”, “when” clause evaled as true
will be executed.
Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.
Oh, and in case that wasn’t enough proof:
puts case
when x < 0; “x < 0”
when 0 <= x && x < 1; “x in interval [0,1)”
when 1 <= x && x < 2; “x in interval [1,2)”
when 2 <= x; “x >= 2”
end
I’d like to learn LISP and Latin too. Maybe if someone coded a LISP
environment whose interface (and code) was entirely in Latin… two birds one
stone???
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
If no expression after “case”, “when” clause evaled as true
will be executed.
Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.
Oops, I think some more code just disappeared from my screen!!!
case
when x < 0; puts “x < 0”
when x < 1; puts “x in interval [0,1)”
when x < 2; puts “x in interval [1,2)”
else puts “x >= 2”
end
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end
If no expression after “case”, “when” clause evaled as true
will be executed.
Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.
Oops, I think some more code just disappeared from my screen!!!
case
when x < 0; puts “x < 0”
when x < 1; puts “x in interval [0,1)”
when x < 2; puts “x in interval [1,2)”
else puts “x >= 2”
end
And using David’s suggestion in another post and adding a a twist we
lose even more!!!
puts "x " + case
when x < 0; “< 0”
when x < 1; “in interval [0,1)”
when x < 2; “in interval [1,2)”
else “>= 2”
end
Although I kind of think I might have gone a little too far and lost
some readability in my quest to eliminate duplication. Fore!!!