Namespacing in Perl is pretty straight forward, but I can't seem to find a solution for translating this very simple Perl class hierarchy to Ruby.
package Foo;
use Foo::Bar;
sub bar {
return Foo::Bar->new()
}
package Foo::Bar
sub baz {}
use Foo;
my $foo = Foo->new();
my $bar = $foo->bar();
$bar->baz()
Modules can't be instantiated, so this code obviously won't work:
require 'foo/bar.rb'
module Foo
def bar
Foo::Bar.new
end
end
module Foo
class Bar
def baz
end
end
end
require 'lib/foo.rb'
foo = Foo.new
bar = foo.bar
bar.baz
But trying to declare Foo as a class instead doesn't work either, because there's already a module by that name:
lib/foo.rb:3:in `<top (required)>': Foo is not a class (TypeError)
So I end up with:
module Foo
class Foo
...
end
end
foo = Foo::Foo.new
Which is just not what I want. I have the feeling that I'm missing something very fundamental. :) Thanks for shedding some light on this.
In Ruby, both modules and classes can be used to provide namespace separation. In fact Class
is a subclass of Module
, and most things you can do with a Module
you can also do with a Class
If Foo
needs to be a class, declare it as a class, don't declare it as a module.
E.g.
lib/foo.rb
require 'foo/bar.rb'
class Foo
def bar
Foo::Bar.new
end
end
lib/foo/bar.rb
class Foo
class Bar
def baz
end
end
end
main.rb
require 'lib/foo.rb'
foo = Foo.new
bar = foo.bar
bar.baz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With