From: Gregory Brown Date: 2007-06-19T06:09:01+09:00 Subject: Re: Another Easy Beginner Question On 6/18/07, Eric I. wrote: > On Jun 18, 3:40 pm, "Gregory Brown" wrote: > > Wouldn't it be: > > > > def fact(n); n > 1 ? n + fact(n-1) : 1; end > > puts (1..12).inject(0) { |s,r| s + fact(r) } > > That certainly works, although I'm personally uncomfortable with the > name of your recursive method; "fact" seems to imply factorial, which > this is not. Dur. You're right. 12! = 12*11*10*.. /me is a math major, too. > If you like inject, how about a pair of 'em: > > puts (1..12).inject(0) { |sum, day| > sum + (1..day).inject { |day_sum, gifts| day_sum + gifts } > } yup, that looks pretty good.