Hacker News new | ask | show | jobs
by Moto7451 4337 days ago
Apparently enough of it wasn't built in that someone went ahead and created a Moose for Ruby[1]. I haven't done a lot of Ruby but I imagine it's nice as a day to day language. I enjoy Objective C's message passing syntax and dynamic runtime so Ruby is likely a good fit for me.

Personally the reason to use Perl over Ruby is the community. I gel a lot better with the members of the Perl community I've met compared to when I was poking my head around Ruby. I don't mean this in a bad/flamy way. No one was "mean" to me or anything. My brain is just damaged in the Perl kind of way rather than the Ruby kind of way.

Anyways, the actual question shouldn't be why Ruby over Perl, rather it should be "why don't we have Moose (or something like it) in every language." There's nothing about it that ties it to Perl. A dynamic runtime isn't a bad thing to have but I can't really see why most of the really nice parts couldn't be implemented in other languages at compile time.

Simply put, Moose is awesome. You can build so much functionality with not a lot of code and it actually glues together well. Type coercions are awesome and super useful in day to day programming. I've been doing a lot of API work where I need flexibility in terms of output (JSON, XML, RSS, etc) where a plain object dump to the specified format isn't enough. In the case of the XML there is an XSD it needs to match against and the structure isn't particularly amicable to sane JSON output.

Care of Roles, Type coercions, Attribute meta roles, and some preemptive thinking I basically just create objects with attributes and a few methods containing business logic and not a lick of serializer logic.

  package My::Foo;

  use MyCo::Moose; #Exports a few things I usually need like aliasing,
  use My::Type::Exports qw(:all);

  with 'My::Role::AwesomeSerializer';

  has 'some_attribute' => (
    is          => 'ro',
    isa         => MaybeSuperComplexObjectType,
    coerce      => 1,
    traits      => ['UseMyJsonSerializer'],
    json_name   => 'someAttribute',
    xml_name    => 'My::SomeAttribute',
    alias       => 'some_call_it_this', 
  );

  # yatta

  __PACKAGE__->meta->make_immutable();

  1;
In my types package I have various coerce methods that accept different data formats. The logic in the class doesn't have to change, nor do I need to write a new constructor/factory like I would in other languages, when the data I'm being given changes in some non-trivial way.

[1] http://pacman.blog.br/blog/2014/02/07/moosex-a-new-ruby-dsl-...