From: 7stud -- Date: 2011-04-29T02:23:13+09:00 Subject: Re: calling methods, beginner help Ronnie Aa wrote in post #995566: > Hello Guys, > > I'm fairly new to ruby and I have a question: > > I have an array: > > $xarray = [10,10] > Don't EVER use global variables. Never, ever, ever, never. > 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??? > If you want to use a method anywhere in your program, then you define it at the 'top level' just like you did. However, a method defined at the top level is a private method, and a private method cannot be called with a 'receiver', for instance in this line: $xarray.product $xarray is the receiver. The only way you can call the product() method is like this: product() -- Posted via http://www.ruby-forum.com/.