My professional career mostly consists of using PHP. However I often do a side step to a different language. Lately I have been playing around with ruby The first concept that strikes me as odd, are the modules. They seem to have multiple responsibilities, they have the possibility to be used as mixin, but in the meanwhile supply you with a namespace.

However the language itself is quite interesting, due to its nature in duck-typing, verbosity in the language is a bit suppressed. For example creating a mapper that maps some value:

In PHP it would be something like:

<?php
class Mapper
{
    private $collection;

    public function __construct(array $collection)
    {
        $this->collection = $collection;
    }

    public function to_hash(): array
    {
        return array_map(
            function (array $something) {
                return [
                    'something' => $something['name']
                ];
            },
            $this->collection
        );
    }
}

In Ruby

      class Mapper
        def initialize(collection)
          @collection = collection
        end

        def to_hash
          @collection.each.collect do |something|
            {
              'something' => something.name
            }
          end
        end

However for ruby, no members, no public or private. And you are able to pass anything to collection, as long as it is consistent with the collection “interface”

When using ruby for a bit, i get the feel that namespaces are not really a defactor standard to use. Because you get something like:

module X
	module Y
		module Z
			class Something
			end
		end
	end
end

This seems like an aweful lot of indenting, which in itself would be less readable when doing this with loads of namespaces.

While PHP keeps it quite clean

<?php
namespace X\Y\Z;
class Something {
}

Therefor i don’t think modules are actually ment for namespacing in the traditional OOP way and is more like a side effect of the mixin concept. And due to the mixin concept and duck-typing, it feels ruby is more a functional language then a imperitive language and should probably be used that way.