Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3656

Re: calling methods, beginner help

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail
From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: calling methods, beginner help
Date Thu, 28 Apr 2011 15:29:03 -0500
Organization Service de news de lacave.net
Lines 39
Message-ID <5b9e185484311d8289b9e12e842d1e57@ruby-forum.com> (permalink)
References <626238bd776ed0898a473c7a16e02fb5@ruby-forum.com> <560a5f2c1a7578882b0fd9ce72f43724@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1304023912 74957 65.111.164.187 (28 Apr 2011 20:51:52 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Thu, 28 Apr 2011 20:51:52 +0000 (UTC)
In-Reply-To <560a5f2c1a7578882b0fd9ce72f43724@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 382337
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <5b9e185484311d8289b9e12e842d1e57@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3656

Show key headers only | View raw


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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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