Fred Werne
11/4/2003 7:48:00 PM
Hi!
Ruby is great. My English is not good enough to describe it.
(I'm a Newbee, of course.)
I guess a (may be) helpfull method for the class Numeric:
With
------------------------------------
| class Numeric |
| def if(cond) |
| cond ? self : 0 |
| end |
| end |
------------------------------------
it is possible to add Numbers conditionally.
For example:
# -------------------------------------------------------------
# | |
# | [true,false].each { |leapyear| |
# | print((leapyear ? "" :"Kein "),"leapyear\n") |
# | 1.upto(12) { |n| |
# | print(n, "\t", 30 + |
# | ( n - 7.5 ).abs.ceil % 2 - |
# | (leapyear ? 1 : 2).if( n==2), |
# | "\n") |
# | } |
# | } |
# | |
# -------------------------------------------------------------
What do you think? Trivial?
Or dangerous? Because 3 * 4.if(false) --> 0 instead of 3
Perhaps better:
def add_sub_if(cond) cond ? self : 0 end
def mul_div_if(cond) cond ? self : 1 end
Fred from Wuppertal, Germany
--
I'm glad, if you correct my poor English.