[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Implanting a new method for a class

Levin Alexander

2/12/2006 8:49:00 PM

On 2/12/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:> Hi there, I have a question regarding how to implant a new method into the> class Array. I would like to write a ruby file that sums all of the digits> in an array. I have tried this:>> class Array> def Array.sum> until (Array.empty?)> arraySum += Array.pop> end> puts arraySum> end> end>> Gave no error when I ran the code. My problem occur when i try to use this> method:>> require "Library/Array_sum.rb"> a = [1..100]> a.sum>> I get a NoMethodError on sum. Can anybody tell me what I did wrong here?> Thanks in advance :)You are confusing classes and instances of classes. You are definingmethods for the Array class, not for actual array instances. class A def A.foo puts "Class method of #{self.inspect}, instance of class #{self.class}" end def foo puts "Instance method for #{self.inspect}, of class #{self.class}" end end A.foo # prints "Class method of A, instance of class Class" a = A.new a.foo # prints "Instance method of <#A:0xb7ce9000> of class A"You can implement sum like so: class Array def sum total = 0 # self refers to the current array instance self.each { |elem| total += elem } total end # but you can also implement sum on the class Array itself. But then # you need to provide the array, that you want to know the sum of # def Array.sum arr arr.inject(0) { |total, elem| total += elem } end end puts [1,2,3,4].sum puts Array.sum([1,2,3,4,5])be careful with "pop", it changes the Array! def sum!(arr) total = 0 total += arr.pop until a.empty? total end arr = [5,4,3] p arr.sum! #=> 12 p arr #=> [] arr is now empty!-Levin
1 Answer

Jeppe Jakobsen

2/12/2006 9:05:00 PM

0

Thank you, for sorting things out for me :)

2006/2/12, Levin Alexander <levin@grundeis.net>:
>
> On 2/12/06, Jeppe Jakobsen <jeppe88@gmail.com> wrote:
> > Hi there, I have a question regarding how to implant a new method into
> the
> > class Array. I would like to write a ruby file that sums all of the
> digits
> > in an array. I have tried this:
> >
> > class Array
> > def Array.sum
> > until (Array.empty?)
> > arraySum += Array.pop
> > end
> > puts arraySum
> > end
> > end
> >
> > Gave no error when I ran the code. My problem occur when i try to use
> this
> > method:
> >
> > require "Library/Array_sum.rb"
> > a = [1..100]
> > a.sum
> >
> > I get a NoMethodError on sum. Can anybody tell me what I did wrong here?
> > Thanks in advance :)
>
> You are confusing classes and instances of classes. You are defining
> methods for the Array class, not for actual array instances.
>
> class A
> def A.foo
> puts "Class method of #{self.inspect}, instance of class #{
> self.class}"
> end
> def foo
> puts "Instance method for #{self.inspect}, of class #{self.class}"
> end
> end
>
> A.foo # prints "Class method of A, instance of class Class"
> a = A.new
> a.foo # prints "Instance method of <#A:0xb7ce9000> of class A"
>
> You can implement sum like so:
>
> class Array
> def sum
> total = 0
> # self refers to the current array instance
> self.each { |elem| total += elem }
> total
> end
>
> # but you can also implement sum on the class Array itself. But then
> # you need to provide the array, that you want to know the sum of
> #
> def Array.sum arr
> arr.inject(0) { |total, elem| total += elem }
> end
> end
>
> puts [1,2,3,4].sum
> puts Array.sum([1,2,3,4,5])
>
> be careful with "pop", it changes the Array!
>
> def sum!(arr)
> total = 0
> total += arr.pop until a.empty?
> total
> end
>
> arr = [5,4,3]
> p arr.sum! #=> 12
> p arr #=> [] arr is now empty!
>
>
> -Levin
>



--
"winners never quit, quitters never win"