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

F

Fred Werne

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
 
S

Simon Strandgaard

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>
 
S

Simon Strandgaard

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]
}
 
F

Fred Werne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top