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


Groups > comp.lang.ruby > #7022

Re: Module Pattern in Ruby

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Module Pattern in Ruby
Date Wed, 10 Sep 2014 22:29:08 +0200
Lines 35
Message-ID <c7bqkmFgl70U1@mid.individual.net> (permalink)
References <608f2172-203a-4a3e-9d0b-82a49c626139@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net iou/zt/IihdOpkHcWlpznwO+3HIV4nQfVyfv0IVTqmx5W2uM8=
Cancel-Lock sha1:Q8BGhyUX4pJ1Q1l3cCG9tiZoMvw=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.0
In-Reply-To <608f2172-203a-4a3e-9d0b-82a49c626139@googlegroups.com>
Xref csiph.com comp.lang.ruby:7022

Show key headers only | View raw


On 10.09.2014 11:45, deltanoob wrote:

> I'm looking for a good tutorial on the "ruby way" to implement the module pattern in ruby.
>
> http://en.wikipedia.org/wiki/Module_pattern

"In software engineering, the module pattern is a design pattern used to 
implement the concept of software modules, defined by modular 
programming, in a programming language with incomplete direct support 
for the concept."

Now, Ruby _has_ support for modules - there is even a keyword of that 
name.  :-) If you need to store state in a module in the same way as can 
be done in a Modula or PL/SQL package you can simply define accessors:

irb(main):001:0> module Foo; @x = 1; class <<self; attr_accessor :x; 
end; end
=> nil
irb(main):002:0> Foo.x
=> 1
irb(main):003:0> Foo.x = 99
=> 99
irb(main):004:0> Foo.x
=> 99
irb(main):005:0> Foo.x =3
=> 3
irb(main):006:0> Foo.x
=> 3

Note: the initialization of @x is not necessary.

Kind regards

	robert

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


Thread

Module Pattern in Ruby deltanoob <dexterp@gmail.com> - 2014-09-10 02:45 -0700
  Re: Module Pattern in Ruby Robert Klemme <shortcutter@googlemail.com> - 2014-09-10 22:29 +0200

csiph-web