On Mon, Sep 20, 2010 at 11:47 AM, Pen Ttt <myocean135 / yahoo.cn> wrote:
> class =A0Mycompute
> =A0def =A0self.sum(a)
> =A0 =A0a.inject(0){|s,i|s+i}
> =A0end
> =A0def =A0self.mean(a)
> =A0 =A0a.inject(0){|s,i|s+i}/(a.size.to_f)
> =A0end
> end
> Mycompute.sum([1,2,3,4])
> =3D> 10
> Mycompute.mean([1,2,3,4])
> =3D> 2.5
>
> how =A0can i =A0get =A0Mycompute.sum([1,2,3,4]).mean=3D2.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=3Darr
>>   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
=3D> nil
>>
?> x =3D Mycompute.new([1,2,3,4])
=3D> #<Mycompute:0x0000000102cca8 @arr=3D[1, 2, 3, 4]>
>> p x.sum
10.0
=3D> 10.0
>> p x.mean
2.5
=3D> 2.5
>> p x.values
[1, 2, 3, 4]
=3D> [1, 2, 3, 4]
>> p x.values.min
1
=3D> 1
>> p x.values.count
4
=3D> 4
>> p x.values.map{|x| x*x}
[1, 4, 9, 16]
=3D> [1, 4, 9, 16]

kind regards -botp