From: Brian Candler Date: 2011-04-29T05:29:03+09:00 Subject: Re: calling methods, beginner help Ronnie Aa wrote in post #995580: > For extra clearness: > > My class Do_this and my class Do_that both have to use that method.. > They both use the same variable and both have to apply the same > operations on that variable (amongst other things of course).. One way to share common code is using a module, and including it into those classes where you need it. module MyUsefulMethods def product @foo.inject { |x,y| x*y } end end class DoThis include MyUsefulMethods def initialize @foo = [1,2,3] end end class DoThat include MyUsefulMethods def initialize @foo = [4,5,6] end end p DoThis.new.product p DoThat.new.product Note that I'm using an instance variable (@foo) not a global variable ($foo). Each instance of those classes has its own @foo. -- Posted via http://www.ruby-forum.com/.