[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"days=30-2.if(month==2)" a helpfull method, may be

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.

3 Answers

Simon Strandgaard

11/4/2003 9:44:00 PM

0

On Tue, 04 Nov 2003 20:48:13 +0100, Fred Werne wrote:
> Ruby is great. My English is not good enough to describe it.
> (I'm a Newbee, of course.)

Welcome to Ruby.. your english is flawless so far :-)

[snip]
> What do you think? Trivial?

You mix computation and presentation. I prefer separating these
responsibilities from each other.


> 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

What your suggest reminds me of machine code.. hmmm.


I made some modifications to your program (separated computation from
presentation :-)

--
Simon Strandgaard


server> ruby a.rb
month normalyear leapyear
1 31 31
2 28 29
3 31 31
4 30 30
5 31 31
6 30 30
7 31 31
8 31 31
9 30 30
10 31 31
11 30 30
12 31 31
server> expand -t2 a.rb
class Numeric
def if(cond)
cond ? self : 0
end
end

table = [false, true].map{ |leapyear|
screw = leapyear ? 1 : 2
(1..12).to_a.map{|n|
30 + (n - 7.5).abs.ceil % 2 - screw.if(n == 2)
}
}

puts "month normalyear leapyear"
table[0].zip(table[1]).each_with_index{|(normal, leap), month|
print "%2d %2d %2d\n" % [month+1, normal, leap]
}
server>

Simon Strandgaard

11/4/2003 9:51:00 PM

0

On Tue, 04 Nov 2003 22:43:58 +0100, Simon Strandgaard wrote:

> puts "month normalyear leapyear"
> table[0].zip(table[1]).each_with_index{|(normal, leap), month|

table.transpose.each_with_index{|(normal, leap), month|

I should mention that Array#transpose can be used here :-)


> print "%2d %2d %2d\n" % [month+1, normal, leap]
> }


--
Simon Strandgaard

Fred Werne

11/5/2003 9:23:00 PM

0

Hi Simon!

Thanks for your reply.

It takes some moments to understand, what your code does.
I use more parentheses, although they are not nesssesary.

screw = ( leapyear ? 1 : 2 ) # for example

Very helpfull methods: map and transpose.

I named your table daysOfMonths. The idea comes from a question in
de.comp.lang.java. There was a long program, to compute the number of
days in the several causes.
First I wrote a programm in java.

class monatstage {

public static void main(String[] argv){
boolean schaltjahr=true;
for(int n = 1; n <= 12; n++) {
System.out.println(
n + "\t" +
( 30
+ Math.round
( new Float
( Math.abs ( n - 7.5 ) ) . floatValue() ) % 2
- ( n==2 ? 1 : 0 ) * ( schaltjahr ? 1 : 2 )
)
) ;

}
}
}


Then I thought, it must be easier in ruby. And it was.

The method "if" in the Numeric class, I found very helpfull in a lot of
situations. The daysOfMonths is only one example.

Regards

Fred from Wuppertal, Germany