[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Module nesting and module vs. instance methods

gga

2/19/2005 1:07:00 AM

Having made the commitment to ruby, I am now finding myself building
bigger frameworks and ruby's limited namespace rules with modules are
potentially showing some issues that I want to either prepare for or
avoid.
Or perhaps it is not entirely clear to me what's the proper Ruby way of
doing it. Basically I am building some libraries and I am trying to be
careful about name pollution.

I have a bunch of functions that, currently, have no particular class
location. Currently, I am placing them in modules.
As I have a bunch of them that are somewhat unrelated, I have been
creating modules following the standard procedure I've used in other
languages. That is, I'm using:

module MyCompany
module Module
def self.func1
end
def func2
end
end
end

Now... the problems I am facing are...

1) Ruby's name pollution on includes.

--- B ---
module B
def bfunc
puts "bfunc"
end
end
--- A ---
require 'B'

module A
def afunc
puts "afunc"
bfunc
end
end
---- some other code
require "A"
class X
def initialize
afunc # this should work
bfunc # this should fail
end
end
===============================
I want to have code that includes A, but does not include B. However,
class X should not have access to functions in module B, which it never
included.

How can I safely work around this? I want to avoid behaviors or
anything like that.


2) I also would like to have some modules that work either as include
modules or as modules with stand-alone methods. Problem is that ruby
will not "include" class methods.
That is, say I have a funcion called runcmd() to run a shell command
and do stdin/out/err piping. This function is rather common, so I
would like to have:

module Shell
def self.runcmd(cmd)
# ....do tons of stuff
end
def runcmd(cmd)
Shell.runcmd(cmd)
end
end

So I can do:

Shell.runcmd("ls")

or:

class Backup
include Shell
def doit
runcmd("ls")
runcmd("cmd2")
end
....etc..
end


The above works but it seems kind of silly and wasteful for something
that to me seems pretty common (not to mention that the instance method
access will likely be slower, too).

6 Answers

Csaba Henk

2/19/2005 2:45:00 AM

0

On 2005-02-19, gga <GGarramuno@aol.com> wrote:
> 2) I also would like to have some modules that work either as include
> modules or as modules with stand-alone methods. Problem is that ruby
> will not "include" class methods.
> That is, say I have a funcion called runcmd() to run a shell command
> and do stdin/out/err piping. This function is rather common, so I
> would like to have:
>
> module Shell
> def self.runcmd(cmd)
> # ....do tons of stuff
> end
> def runcmd(cmd)
> Shell.runcmd(cmd)
> end
> end
>
> So I can do:
>
> Shell.runcmd("ls")
>
> or:
>
> class Backup
> include Shell
> def doit
> runcmd("ls")
> runcmd("cmd2")
> end
> ...etc..
> end
>
>
> The above works but it seems kind of silly and wasteful for something
> that to me seems pretty common (not to mention that the instance method
> access will likely be slower, too).

I've faced this problem too, and I find out a workaround which may be
not very elegant, but results in only a contstant number of extra code
lines (not like the trivial solution shown above, which uses extra code
lines in a linear rate to the number of class methods in Shell).

module Shell
# what would be module methods are rather collected in a separate
# module
module Addons
def runcmd(cmd)
# ....do tons of stuff
end
end

# a custom inclusion method
def useme aMod
aMod.send :include, self
aMod.extend Addons
end
end

class Backup
Shell.useme self
...
end

Backup.runcmd "ls"
#works

If you want instances of Backup to have direct access to runcmd, you can
include Addon in Shell. I don't see though the purpose of turning
class/module methods into instance methods (yeah, you save a few
characters of code by doing that, but it seems to be a bad design,
unless you are about breaking out of the class based object model).

Csaba

Austin Ziegler

2/19/2005 2:49:00 AM

0

On Sat, 19 Feb 2005 10:09:48 +0900, gga <GGarramuno@aol.com> wrote:
[...]
> I have a bunch of functions that, currently, have no particular
> class location. Currently, I am placing them in modules. As I have
> a bunch of them that are somewhat unrelated, I have been creating
> modules following the standard procedure I've used in other
> languages. That is, I'm using:
>
> module MyCompany
> module Module
> def self.func1
> end
> def func2
> end
> end
> end


> Now... the problems I am facing are...
> 1) Ruby's name pollution on includes.
>
> --- B ---
> module B
> def bfunc
> puts "bfunc"
> end
> end
>
> --- A ---
> require 'B'
>
> module A
+ include B
+
> def afunc
> puts "afunc"
> bfunc
> end
> end
>
> ---- some other code
> require "A"
> class X
+ include A
+
> def initialize
> afunc # this should work
> bfunc # this should fail
> end
> end

I presume that you mean the include statements I placed above.

> I want to have code that includes A, but does not include B.
> However, class X should not have access to functions in module B,
> which it never included.

This is not possible without doing some tricks. If you want A to
have access to B, but not allow X (which included A) to have access
to B, then you need to figure out some other way. Perhaps:

module B
def self.bfunc; end
end

module A
def afunc; B.bfunc end
end

class X
include A

def xfunc; afunc; end
end

But if A has included B, then when you include A, you'll get
everything that it knows about mixed into your class. This is the
fundamental capability of Ruby's mix-ins: the mix-in essentially
becomes a private part of the class into which it is mixed.

> 2) I also would like to have some modules that work either as
> include modules or as modules with stand-alone methods. Problem is
> that ruby will not "include" class methods.

try:

module Shell
def runcmd(command); ...; end
module_functtion :runcmd
end

class Backup
include Shell
end

It does the same thing as you did, except nicer.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


William Morgan

2/19/2005 10:50:00 PM

0

Excerpts from Csaba Henk's mail of 18 Feb 2005 (EST):
> module Shell
> # what would be module methods are rather collected in a separate
> # module
> module Addons
> def runcmd(cmd)
> # ....do tons of stuff
> end
> end
>
> # a custom inclusion method
> def useme aMod
> aMod.send :include, self
> aMod.extend Addons
> end
> end
> #works

Use 'append_features' rather than 'useme'. See e.g. [ruby-talk:20021].
Then you can automatically create both class and instance methods.

Or, if the module's methods are all to be class methods, you can extend
rather than include it in the first place.

--
William <wmorgan-ruby-talk@masanjin.net>


watizzit

2/26/2009 3:50:00 AM

0

On Feb 26, 11:27 am, watiz...@gmail.com wrote:
> On Feb 26, 11:01 am, "yan dao" <Zai...@yahoo.com.sg> wrote:
>
>
>
>
>
> > Don't believe too much on this kind of report/statistic, they must have paid
> > them highly to become one of the top on their list. Even Merrill Lynch can
> > put your organisation at the top of their list for highly preferable company
> > worth investing if the price is right.
>
> > <watiz...@gmail.com> wrote in message
>
> >news:52e87301-2cab-465c-937b-c99f782b4c2e@e5g2000vbe.googlegroups.com...
> > Singapore tops world innovation list
>
> >http://www.telecomasia.net/article.php?id_article=12596&id_cat1......
>
> > Feb 26, 2009
> > telecomasia.net
>
> > Asia – particularly Singapore, South Korea and Japan - is “the place
> > to look” for global innovation leaders, a new report from the
> > Information Technology & Innovation Foundation (ITIF) has found.
>
> > Singapore has topped a global list of innovation-based economies,
> > scoring 15% higher than the US and 40% higher than the 15 EU economies
> > (EU-15).  “Singapore has made technological innovation almost a
> > national obsession,” the report said.
>
> > South Korea placed fifth, scoring slightly ahead of the US, but 25%
> > higher than the EU-15.
>
> > “With favorable corporate tax policies and agencies like the South
> > Korea Information Agency, South Korea has made a concerted effort to
> > prosper through technology-led growth,” the report said.
>
> > Japan placed ninth, below the US but still 14% higher than the EU-15.
> > “Many economic pundits have mistakenly written off [Japan],” the
> > report said, but added that the country's slow GDP growth is less due
> > to poor economic performance than it is from a declining working age
> > population.
>
> > Countries are scored on factors such as IT investment, e-government,
> > higher education, and corporate and government R&D spending. The
> > survey covered 36 countries and four regions.
>
> > Singapore, South Korea and Japan were the only Asian nations in the
> > top ten.
>
> The Full Report is here in PDF. Read and make your own judgement.
>
> http://www.itif.org/index....
>
> ITIF uses 16 indicators to assess the global innovation-based
> competitiveness of 36 countries and 4 regions. This report finds that
> while the U.S. still leads the EU in innovation-based competitiveness,
> it ranks sixth overall. Moreover, the U.S. ranks last in progress
> toward the new knowledge-based innovation economy over the last
> decade.
>
> Australia is at 17.
>
> "It is not the strongest of the species that survive,
> nor the most intelligent,
> but the ones most responsive to change."
> — Charles Darwin- Hide quoted text -
>
> - Show quoted text -

Oops ....

Austria is 17

Australia is 19

LOL.

Pro PAP

2/26/2009 9:38:00 AM

0

On Feb 26, 11:49 am, watiz...@gmail.com wrote:
> On Feb 26, 11:27 am, watiz...@gmail.com wrote:
>
>
>
> > On Feb 26, 11:01 am, "yan dao" <Zai...@yahoo.com.sg> wrote:
>
> > > Don't believe too much on this kind of report/statistic, they must have paid
> > > them highly to become one of the top on their list. Even Merrill Lynch can
> > > put your organisation at the top of their list for highly preferable company
> > > worth investing if the price is right.
>
> > > <watiz...@gmail.com> wrote in message
>
> > >news:52e87301-2cab-465c-937b-c99f782b4c2e@e5g2000vbe.googlegroups.com....
> > > Singapore tops world innovation list
>
> > >http://www.telecomasia.net/article.php?id_article=12596&id_cat1......
>
> > > Feb 26, 2009
> > > telecomasia.net
>
> > > Asia – particularly Singapore, South Korea and Japan - is “the place
> > > to look” for global innovation leaders, a new report from the
> > > Information Technology & Innovation Foundation (ITIF) has found.
>
> > > Singapore has topped a global list of innovation-based economies,
> > > scoring 15% higher than the US and 40% higher than the 15 EU economies
> > > (EU-15).  “Singapore has made technological innovation almost a
> > > national obsession,” the report said.
>
> > > South Korea placed fifth, scoring slightly ahead of the US, but 25%
> > > higher than the EU-15.
>
> > > “With favorable corporate tax policies and agencies like the South
> > > Korea Information Agency, South Korea has made a concerted effort to
> > > prosper through technology-led growth,” the report said.
>
> > > Japan placed ninth, below the US but still 14% higher than the EU-15.
> > > “Many economic pundits have mistakenly written off [Japan],” the
> > > report said, but added that the country's slow GDP growth is less due
> > > to poor economic performance than it is from a declining working age
> > > population.
>
> > > Countries are scored on factors such as IT investment, e-government,
> > > higher education, and corporate and government R&D spending. The
> > > survey covered 36 countries and four regions.
>
> > > Singapore, South Korea and Japan were the only Asian nations in the
> > > top ten.
>
> > The Full Report is here in PDF. Read and make your own judgement.
>
> >http://www.itif.org/index....
>
> > ITIF uses 16 indicators to assess the global innovation-based
> > competitiveness of 36 countries and 4 regions. This report finds that
> > while the U.S. still leads the EU in innovation-based competitiveness,
> > it ranks sixth overall. Moreover, the U.S. ranks last in progress
> > toward the new knowledge-based innovation economy over the last
> > decade.
>
> > Australia is at 17.
>
> > "It is not the strongest of the species that survive,
> > nor the most intelligent,
> > but the ones most responsive to change."
> > — Charles Darwin- Hide quoted text -
>
> > - Show quoted text -
>
> Oops ....
>
> Austria is 17
>
> Australia is 19
>
> LOL.

Watizzit all about?
Singapore is L$58B?

Observers

2/26/2009 10:05:00 AM

0

Pro PAP wrote:
> On Feb 26, 11:49 am, watiz...@gmail.com wrote:
>> On Feb 26, 11:27 am, watiz...@gmail.com wrote:
>>
>>
>>
>>> On Feb 26, 11:01 am, "yan dao" <Zai...@yahoo.com.sg> wrote:
>>>> Don't believe too much on this kind of report/statistic, they must have paid
>>>> them highly to become one of the top on their list. Even Merrill Lynch can
>>>> put your organisation at the top of their list for highly preferable company
>>>> worth investing if the price is right.
>>>> <watiz...@gmail.com> wrote in message
>>>> news:52e87301-2cab-465c-937b-c99f782b4c2e@e5g2000vbe.googlegroups.com...
>>>> Singapore tops world innovation list
>>>> http://www.telecomasia.net/article.php?id_article=12596&id_cat1......
>>>> Feb 26, 2009
>>>> telecomasia.net
>>>> Asia ? particularly Singapore, South Korea and Japan - is ?the place
>>>> to look? for global innovation leaders, a new report from the
>>>> Information Technology & Innovation Foundation (ITIF) has found.
>>>> Singapore has topped a global list of innovation-based economies,
>>>> scoring 15% higher than the US and 40% higher than the 15 EU economies
>>>> (EU-15). ?Singapore has made technological innovation almost a
>>>> national obsession,? the report said.
>>>> South Korea placed fifth, scoring slightly ahead of the US, but 25%
>>>> higher than the EU-15.
>>>> ?With favorable corporate tax policies and agencies like the South
>>>> Korea Information Agency, South Korea has made a concerted effort to
>>>> prosper through technology-led growth,? the report said.
>>>> Japan placed ninth, below the US but still 14% higher than the EU-15.
>>>> ?Many economic pundits have mistakenly written off [Japan],? the
>>>> report said, but added that the country's slow GDP growth is less due
>>>> to poor economic performance than it is from a declining working age
>>>> population.
>>>> Countries are scored on factors such as IT investment, e-government,
>>>> higher education, and corporate and government R&D spending. The
>>>> survey covered 36 countries and four regions.
>>>> Singapore, South Korea and Japan were the only Asian nations in the
>>>> top ten.
>>> The Full Report is here in PDF. Read and make your own judgement.
>>> http://www.itif.org/index....
>>> ITIF uses 16 indicators to assess the global innovation-based
>>> competitiveness of 36 countries and 4 regions. This report finds that
>>> while the U.S. still leads the EU in innovation-based competitiveness,
>>> it ranks sixth overall. Moreover, the U.S. ranks last in progress
>>> toward the new knowledge-based innovation economy over the last
>>> decade.
>>> Australia is at 17.
>>> "It is not the strongest of the species that survive,
>>> nor the most intelligent,
>>> but the ones most responsive to change."
>>> ? Charles Darwin- Hide quoted text -
>>> - Show quoted text -
>> Oops ....
>>
>> Austria is 17
>>
>> Australia is 19
>>
>> LOL.
>
> Watizzit all about?
> Singapore is L$58B?

Just keep your mouth shut if you don't know what is it !