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/.