“Robert Klemme” bob.news@gmx.net schrieb im Newsbeitrag news:…
“Sean O’Dell” sean@celsoft.com schrieb im Newsbeitrag
news:200402041127.02778.sean@celsoft.com…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.
Just to illustrate what I mean: in the case of the point coloring one
solve the problem with a mapping:
#!/usr/bin/ruby
Point = Struct.new( :x, :y )
class Point
def range_x; x / 50; end
def range_y; y / 50; end
end
colours = {
[0, 0] => “white”,
[0, 1] => “white”,
[0, 2] => “white”,
[0, 3] => “white”,
[3, 0] => “white”,
[3, 1] => “white”,
[3, 2] => “white”,
[3, 3] => “white”,
[1, 0] => “blue”,
[2, 0] => “blue”,
[1, 1] => “grey”,
[1, 2] => “grey”,
[1, 3] => “grey”,
}
colours.default= “black”
points = [
Point.new( 0, 0 ),
Point.new( 100, 0 ),
Point.new( 55, 55 ),
Point.new( 120, 210 ),
]
points.each do |point|
p [point, colours[ [point.range_x, point.range_y] ] ]
end
IMHO this is also more efficient because the number of operations is
smaller than in a case / if…elsif…end cascade. And it doesn’t grow
linea with the number of colors involved.
Regards
robert
···
On Wednesday 04 February 2004 11:07 am, Guoliang Cao wrote: