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


Groups > comp.lang.ruby > #3684

Re: calling methods, beginner help

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.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 Fri, 29 Apr 2011 03:02:08 -0500
Organization Service de news de lacave.net
Lines 60
Message-ID <efd4233bb963bf4f26a8d05e360e6858@ruby-forum.com> (permalink)
References <626238bd776ed0898a473c7a16e02fb5@ruby-forum.com> <5aa298eaa7c92eb99aeadefbca7a9cc6@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 1304064440 48560 65.111.164.187 (29 Apr 2011 08:07:20 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Fri, 29 Apr 2011 08:07:20 +0000 (UTC)
In-Reply-To <5aa298eaa7c92eb99aeadefbca7a9cc6@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 382368
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <efd4233bb963bf4f26a8d05e360e6858@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3684

Show key headers only | View raw


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

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