From: Rando Christensen Date: 2004-08-05T06:16:52+09:00 Subject: Re: Macros in Ruby Jesse Jones wrote: >>>percent-case >>> 0.75 case >>> normal_hit >>> end >>> >>> 0.20 case >>> critical_hit >>> end >>> >>> otherwise >>> kill_foe >>> end >>>end >> >>I'm sorry, but I don't see how this is more readable than: >> >> case hit_roll >> when (0.0 .. 0.75) >> normal_hit >> when (0.75 .. 0.95) >> critical_hit >> else >> kill_foe >> end >> end > > > A macro (or some clever code like Joel's) is better in two ways: > > 1) It's much easier to glance at numbers like 0.75 and 0.20 and figure > out the relative weighting. For me at least, it's a fair amount harder > to subtract two sets of numbers and compare them. Of course, you could > add comments listing the percentages, but it's always preferable to > rewrite the code so that it doesn't need such comments. I don't think you're right on this point. In the case that was presented: > 0.75 case > normal_hit > end Does this mean 0.75 and above, or 0.75 and below? You won't know this unless you look up the macro's definition. Here, however: > when (0.0 .. 0.75) > normal_hit I don't think you can be confused on that point. The macro version has made the code much less clear in my mind, which is precisely the argument against macros in ruby. -- Rando Christensen