From: Brian Candler Date: 2011-04-29T17:02:08+09:00 Subject: Re: calling methods, beginner help Ronnie Aa wrote in post #995637: > Could this problem be solved with a procedure also??? There are no procedures in Ruby - only methods. If you want a standalone method, which doesn't really belong to any particular object instance, IMO the cleanest way is to make it a module method: module Util def self.product(arr) arr.inject { |x,y| x*y } end end p Util.product([1,2,3]) (Of course, Util is an instance of class Module, so the method *does* actually belong to an object instance. The module is just a useful place to hang the method onto with its own namespace) Then you can put this module into a separate file, util.rb, and it's easy to re-use from other code. There is a slightly different syntax which you will occasionally come across: module Util def product(arr) arr.inject { |x,y| x*y } end module_function :product end # Now you can can call Util.product, but you can also mix the Util # module into your own classes. class Foo include Util def doit(arr) product(arr) end end p Util.product([1,2,3]) # => 6 p Foo.new.doit([4,5,6]) # => 120 Incidentally, a really good source of info on Ruby is http://www.ruby-doc.org/docs/ProgrammingRuby/ This is the free 1st edition, which is old but still relevant. You can also buy fthe 2nd edition for ruby 1.8, or the 3rd edition for ruby 1.9, in paper or PDF form. Regards, Brian. -- Posted via http://www.ruby-forum.com/.