D
Daniel Schierbeck
I hereby propose two additions to Ruby. Please come with some comments
before I file an RCR.
Range#sum
=========
Usage
-----
(lower_limit..upper_limit).sum { |i| expression }
Implementation
--------------
class Range
def sum
raise ArgumentError unless block_given?
result = 0.0
each { |i| result += yield(i) }
result
end
end
Notes
-----
Could also be implemented as Math#sum
Integer#faculty
===============
Usage
-----
n.faculty # => n * (n - 1) * (n - 2) ... 1
Implementation
--------------
class Integer
def faculty
self == 0 ? 1 : self * (self - 1).faculty
end
end
Cheers,
Daniel
before I file an RCR.
Range#sum
=========
Usage
-----
(lower_limit..upper_limit).sum { |i| expression }
Implementation
--------------
class Range
def sum
raise ArgumentError unless block_given?
result = 0.0
each { |i| result += yield(i) }
result
end
end
Notes
-----
Could also be implemented as Math#sum
Integer#faculty
===============
Usage
-----
n.faculty # => n * (n - 1) * (n - 2) ... 1
Implementation
--------------
class Integer
def faculty
self == 0 ? 1 : self * (self - 1).faculty
end
end
Cheers,
Daniel