HaPK wrote: > Daniel Schierbeck wrote: > >> Range#sum >> ========= This should go into Enumerable if at all. > This could be done with Enumerable#inject. Exactly. > Your implementation assumes > that range is for values of Float class. Even more so as this restriction does not apply when using inject: # the sum of all members enum.inject {|a,b| a + b} Although, if you want to apply some expression to the value even with inject you are going to need some starting value unless you do the mapping beforehand: # all work enum.inject(0) {|s,a| s + expr(a)} enum.inject(0.0) {|s,a| s + expr(a)} enum.map {|a| expr(a)}.inject {|a,b| a + b} >> Integer#faculty >> =============== >> >> Implementation >> -------------- >> >> class Integer >> def faculty >> self == 0 ? 1 : self * (self - 1).faculty >> end >> end >> > (1..self).inject(1) { |result, x| result*x } > > would be more compact. You can even do >> (1..10).inject {|a,b|a*b} => 3628800 >> (2..10).inject {|a,b|a*b} => 3628800 Hint: if there is no argument to #inject the first pair in the block is fed the first and second element. > Am I missing something, but why "faculty" ? Why not factorial ? Maybe because of http://akas.imdb.com/title/tt0133751 ? :-)) To sum up, #sum seems too specialized. #inject is there and does the job quite nicely - and easily. Kind regards robert