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.
Perl
lib/Foo.pm
package Foo;
use Foo::Bar;
sub bar {
return Foo::Bar->new()
}
lib/Foo/Bar.pm
package Foo::Bar
sub baz {}
main.pl
use Foo;
my $foo = Foo->new();
my $bar = $foo->bar();
$bar->baz()
Ruby
Modules can't be instantiated, so this code obviously won't work:
lib/foo.rb
require 'foo/bar.rb'
module Foo
def bar
Foo::Bar.new
end
end
lib/foo/bar.rb
module Foo
class Bar
def baz
end
end
end
main.rb
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:
lib/foo.rb
module Foo
class Foo
...
end
end
main.rb
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.