From: botp Date: 2010-09-20T14:34:41+09:00 Subject: Re: how to get Mycompute.sum([1,2,3,4]).mean=2.5? On Mon, Sep 20, 2010 at 11:47 AM, Pen Ttt wrote: > class  Mycompute >  def  self.sum(a) >    a.inject(0){|s,i|s+i} >  end >  def  self.mean(a) >    a.inject(0){|s,i|s+i}/(a.size.to_f) >  end > end > Mycompute.sum([1,2,3,4]) > => 10 > Mycompute.mean([1,2,3,4]) > => 2.5 > > how  can i  get  Mycompute.sum([1,2,3,4]).mean=2.5? uh,uh, you cant because you get a conflict: your sum returns a value (not array), and your mean is chaining on sum's return value, but mean in turn requires its values coming fr its arguments.... in short: confusing... my suggestion is, start simple as possible, eg $ irb >> class Mycompute >> def initialize(arr) >> @arr=arr >> end >> ?> def values >> @arr >> end >> ?> def size >> @arr.size >> end >> ?> def sum >> @arr.inject(0.0){|s,i|s+i} >> end >> ?> def mean >> sum/size >> end >> end => nil >> ?> x = Mycompute.new([1,2,3,4]) => # >> p x.sum 10.0 => 10.0 >> p x.mean 2.5 => 2.5 >> p x.values [1, 2, 3, 4] => [1, 2, 3, 4] >> p x.values.min 1 => 1 >> p x.values.count 4 => 4 >> p x.values.map{|x| x*x} [1, 4, 9, 16] => [1, 4, 9, 16] kind regards -botp