Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3635
| From | Markus Schirp <mbj@seonic.net> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: calling methods, beginner help |
| Date | 2011-04-28 12:14 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <20110428171426.GA8851@mbj> (permalink) |
| References | <626238bd776ed0898a473c7a16e02fb5@ruby-forum.com> |
Hello Ronnie,
When you want to add the method #product to the Array class you have to
explictly reopen the class.
Example:
-------
class Array
def product
inject(1) { |s, v| s *= v }
end
end
Now you could call #product on Array instances.
[1,2].product # => 2
But:
----
(Monkey-)Patching core classes (like Array) is normaly a bad thing!
It adds more problems than it solves!
An better example for your use case would be:
class Test
def initialize
@array = [10,10]
end
def product
@array.inject(1) { |s,v| s *= v }
end
end
In this case product is defined as an instance method of your own Test
class (not as an instance method of array).
You could call product this way:
object = Test.new
object.product # => 100
--
Markus
On Fri, Apr 29, 2011 at 02:06:13AM +0900, Ronnie Aa wrote:
> Hello Guys,
>
> I'm fairly new to ruby and I have a question:
>
> I have an array:
>
> $xarray = [10,10]
>
> I have this method:
>
> def product
>
> inject(1) { |s, v| s *= v }
>
> end#def
>
>
>
> And I have a class:
>
>
> class Test
>
> def initialize()
>
> tt= $xarray.product
>
> UI.messagebox tt
>
>
> end#def
> end#class
>
>
>
>
> I want to use the method 'product' in my class. How do I do that ??
> Or more in general how do I define methods that I can use all through my
> ruby script???
>
> Thnx for your help..
>
> --
> Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
calling methods, beginner help Ronnie Aa <liquid98@gmail.com> - 2011-04-28 12:06 -0500
Re: calling methods, beginner help Markus Schirp <mbj@seonic.net> - 2011-04-28 12:14 -0500
Re: calling methods, beginner help 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 12:23 -0500
Re: calling methods, beginner help Ronnie Aa <liquid98@gmail.com> - 2011-04-28 12:44 -0500
Re: calling methods, beginner help Markus Schirp <mbj@seonic.net> - 2011-04-28 13:03 -0500
Re: calling methods, beginner help 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 13:03 -0500
Re: calling methods, beginner help Brian Candler <b.candler@pobox.com> - 2011-04-28 15:29 -0500
Re: calling methods, beginner help Ronnie Aa <liquid98@gmail.com> - 2011-04-28 13:40 -0500
Re: calling methods, beginner help Ronnie Aa <liquid98@gmail.com> - 2011-04-28 16:05 -0500
Re: calling methods, beginner help Brian Candler <b.candler@pobox.com> - 2011-04-29 03:02 -0500
Re: calling methods, beginner help Ronnie Aa <liquid98@gmail.com> - 2011-04-29 04:27 -0500
Re: calling methods, beginner help Brian Candler <b.candler@pobox.com> - 2011-04-29 13:19 -0500
Re: calling methods, beginner help Stu <stu@rubyprogrammer.net> - 2011-04-29 15:24 -0500
csiph-web