Hello everyone,
I would like to now if the following snippets of code are equivalent:
—>
module My; module Little; module Namespace;
class Foo
end;
end; end; end
<—
module My::Little::Namespace;
class Foo
end;
end;
—>
are those snippets exactly the same? Ie i can do:
foo = My::Little::Namespace::Foo.new?
thanks in advance for any help!
George Moschovitis, http://www.joy.gr
HAL_9000
(HAL 9000)
21 January 2004 18:05
2
George Moschovitis wrote:
Hello everyone,
I would like to now if the following snippets of code are equivalent:
—>
module My; module Little; module Namespace;
class Foo
end;
end; end; end
<—
module My::Little::Namespace;
class Foo
end;
end;
—>
are those snippets exactly the same? Ie i can do:
foo = My::Little::Namespace::Foo.new?
First of all, you can only do this when the modules already
exist. (How should the interpreter know whether Little is
a class or module?)
Second of all, I think you need parens because of a
precedence issue:
foo = (My::Little::Namespace::Foo).new?
Refer to a question I asked last week, something like “Nested
classes and the Singleton pattern”, and see Nobu’s reply.
Hal
Robert
(Robert)
21 January 2004 19:24
4
“Hal Fulton” hal9000@hypermetrics.com schrieb im Newsbeitrag
news:400EBE6A.4080106@hypermetrics.com …
George Moschovitis wrote:
Hello everyone,
I would like to now if the following snippets of code are equivalent:
—>
module My; module Little; module Namespace;
class Foo
end;
end; end; end
<—
module My::Little::Namespace;
class Foo
end;
end;
That doesn’t work. However, you can do
irb(main):003:0> module Outer; module Inner; end; end
=> nil
irb(main):004:0> class Outer::Inner::Foo;end
=> nil
irb(main):005:0> Outer::Inner::Foo.new
=> #Outer::Inner::Foo:0x10194548
are those snippets exactly the same? Ie i can do:
foo = My::Little::Namespace::Foo.new?
Yes, you can. But that’s not the same as code snippet 1 and code snippet 2
are “exactly the same”.
First of all, you can only do this when the modules already
exist. (How should the interpreter know whether Little is
a class or module?)
Second of all, I think you need parens because of a
precedence issue:
foo = (My::Little::Namespace::Foo).new?
Refer to a question I asked last week, something like “Nested
classes and the Singleton pattern”, and see Nobu’s reply.
Hmmm…
irb(main):001:0> module Outer; module Inner; class Foo; end; end; end
=> nil
irb(main):002:0> Outer::Inner::Foo.new
=> #Outer::Inner::Foo:0x10199f60
Regards
robert
First of all, you can only do this when the modules already
exist. (How should the interpreter know whether Little is
a class or module?)
Thanks for the info, then perhaps a more flexible way to declare
namespaces should be added in Ruby 1.9? Matz what do you think?
George Moschovitis,
Date: Thu, 22 Jan 2004 03:31:05 +0900
From: Yukihiro Matsumoto matz@ruby-lang.org
Newsgroups: comp.lang.ruby
Subject: Re: Modules as namespace
Hi,
Second of all, I think you need parens because of a
precedence issue:
foo = (My::Little::Namespace::Foo).new?
No, parens are not required. Ruby parser is smart enough to read your
intention (this case at least).
matz.
really? what am i missing?
~ > cat foo.rb
attempts =
lambda do
module A::B::C
class Foo; end
end
end,
lambda do
module A
module B
module C
class Foo; end
end
end
end
end
attempts.each do |block|
begin
p block.call
rescue Exception => e
p e
end
end
~ > ruby foo.rb
#<NameError: uninitialized constant A>
#<A: :C::Foo:0x400a38a4>
-a
···
On Thu, 22 Jan 2004, Yukihiro Matsumoto wrote:
In message “Re: Modules as namespace” > on 04/01/22, Hal Fulton hal9000@hypermetrics.com writes:
A: :C::Foo.new
A: :C::Foo.new
–
ATTN: please update your address books with address below!
===============================================================================
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
STP :: Solar-Terrestrial Physics Data | NCEI
NGDC :: http://www.ngdc.noaa.gov/
NESDIS :: http://www.nesdis.noaa.gov/
NOAA :: http://www.noaa.gov/
US DOC :: http://www.commerce.gov/
The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”
/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================
HAL_9000
(HAL 9000)
22 January 2004 04:43
7
Yukihiro Matsumoto wrote:
Hi,
Second of all, I think you need parens because of a
precedence issue:
foo = (My::Little::Namespace::Foo).new?
No, parens are not required. Ruby parser is smart enough to read your
intention (this case at least).
Sorry to mislead. This is different from the case I was looking at
earlier.
Hal
···
In message “Re: Modules as namespace” > on 04/01/22, Hal Fulton hal9000@hypermetrics.com writes:
Probably the fact A doesn’t exist here.
If you call that block first, you haven’t declared anything yet.
Try evaluating them in the reverse order.
···
On Thu, 22 Jan 2004 04:54:59 +0900 “Ara.T.Howard” ahoward@fattire.ngdc.noaa.gov wrote:
really? what am i missing?
~ > cat foo.rb
attempts =
lambda do
module A::B::C
–
Ryan Pavlik rpav@mephle.com
“He said cheetos are orange, so they’re just as good
for you as real oranges.” - 8BT
What do you mean by “flexible way to declare namespaces”?
instead of
module My; module Little; module Namespace
class MyClass
end
end; end; end;
something like:
namespace My::Little::Namespace
class MyClass
end
end
excuse me for the improper use of ‘flexible’
George Moschovitis
Hi –
What do you mean by “flexible way to declare namespaces”?
instead of
module My; module Little; module Namespace
class MyClass
end
end; end; end;
something like:
namespace My::Little::Namespace
class MyClass
end
end
You can do essentially that, as long as the rightmost one is the only
undefined one (since otherwise it’s impossible to tell whether they’re
supposed to be modules or classes):
module A
class B
end
end
class A: :C
end
etc.
David
···
On Sun, 25 Jan 2004, George Moschovitis wrote:
–
David A. Black
dblack@wobblini.net
Robert
(Robert)
24 January 2004 18:44
13
“George Moschovitis” gm@navel.gr schrieb im Newsbeitrag
news:797f8269.0401240857.21c64834@posting.google.com …
What do you mean by “flexible way to declare namespaces”?
instead of
module My; module Little; module Namespace
class MyClass
end
end; end; end;
something like:
namespace My::Little::Namespace
class MyClass
end
end
Well, you can at least do this:
$ irb --prompt simple
module Kernel
def namespace(nested, bind = binding, &block)
pre = “”
post = “”
?> nested.split(‘::’).each do |sym|
?> raise NameError, “Symbol must start uppercase” unless // =~
sym
pre << "module " << sym << ";"
post << "end;"
end
?> eval( pre + post, bind )
eval( nested, bind )
end
end
=> nil
namespace ‘Foo::Bar’
=> Kernel::Foo::Bar
namespace ‘Bar::Foo’, binding
=> Bar::Foo
excuse me for the improper use of ‘flexible’
:-))
Regards
robert
$ irb --prompt simple
module Kernel
def namespace(nested, bind = binding, &block)
pre = “”
…
=> Bar::Foo
wow, this is a cool idea! I forgot how …flexible Ruby is
Anyway something like this isnt usefull enough to be integrated by
default in Ruby 2.0?
George Moschovitis
$ irb --prompt simple
module Kernel
def namespace(nested, bind = binding, &block)
pre = “”
post = “”
?> nested.split(‘::’).each do |sym|
?> raise NameError, “Symbol must start uppercase” unless // =~
sym
pre << "module " << sym << ";"
post << "end;"
end
?> eval( pre + post, bind )
eval( nested, bind )
end
end
=> nil
namespace ‘Foo::Bar’
=> Kernel::Foo::Bar
namespace ‘Bar::Foo’, binding
=> Bar::Foo
heh, sweet…one can really tell Ruby is inspired by Lisp
nikolai
···
–
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux[“\021%six\012\0”],(linux)[“have”]+“fun”-97);}
nah, you can tell it fromn this
http://www.ping.de/~flori/ruby/programs/functional.html
···
il Tue, 27 Jan 2004 07:30:12 +0900, Nikolai Weibull ruby-talk@pcppopper.org ha scritto::
heh, sweet…one can really tell Ruby is inspired by Lisp
nikolai