4 ms·
Every time I try and write ruby I end up giving up because the OO is boilerplate heavy and restrictive compared to Moo/Moose. Basically "No method modifiers an
by mst 11y ago
Every time I try and write ruby I end up giving up because the OO is boilerplate heavy and restrictive compared to Moo/Moose.
Basically "No method modifiers and I have to write my own constructors? Really?" always drives me away within a few hours.
- vidarh 11y agoMethod modification is trivially done through "alias" and then just redefining the method in Ruby, if you re-open the same class. If you subclass, then "super" is sufficient. I'm curious what you actually mean here. As for writing your own constructors, you only need to do so if you actually need to initialize something, and since instance variables can be used without having initialized them in a constructor (they'll just default to nil), I'm curious what you mean here too.
- mst 11y ago> Method modification is trivially done through "alias" and then just redefining the method in Ruby I don't know how to convert this into ruby then without needing to manually gensym a bunch of names: use Class::Method::Modifiers; sub foo { 2 } before foo => sub { warn "before foo 1" }; around foo => sub { my ($orig, $self) = (shift, shift); warn "around foo"; (1, $orig->$self(@_), 3); }; before foo => sub { warn "before foo 2" }; > As for writing your own constructors, you only need to do so if you actually need to initialize something Which is required if you're going to use value objects as much as possible rather than spraying mutable state everywhere.
- dragonwriter 11y ago> I don't know how to convert this into ruby then without needing to manually gensym a bunch of names Sounds like you want something like: https://github.com/nicknovitski/modifiers https://github.com/nicknovitski/modifiers > Which is required if you're going to use value objects as much as possible rather than spraying mutable state everywhere. Using Ruby's Struct class for this often avoids needing explicit constructors.
- mst 11y agoExcept that package doesn't provide before/after/around. What I really want is CLOS. But the closest in perl/python/ruby/javascript is Moose/Moo. There's also, btw, a moosex gem where some rubyists are trying to port the perl stuff - but Moose/Moo are pervasive in modern OO perl whereas most existing ruby code is done the ugly, boilerplate-y way.
- steveklabnik 11y agoYou can do this. I tried to draw up a quick gist, but I am late, and I've been writing too much Rust... The basic idea is this: module Mst def self.included(base) # define a method on base using define_method named :before, which takes an # argument and a block # in that method, you grab the method with that name, using .method() # you then define a new method, named method, which calls the block and # then calls the original method end end class Foo include Mst def test puts "test" end before :test do puts "before" end end Module named after you. You can see how the logic only changes a bit for after and around. You could then wrap this up as a library, and all you'd need to do is the include. http://api.rubyonrails.org/classes/ActiveSupport/Callbacks/Filters.html http://api.rubyonrails.org/classes/ActiveSupport/Callbacks/F... has an example, I think.
- mst 11y agoThat's really cute. I hope, however, that you can understand from the POV of somebody who keeps being driven off ruby by ruby being too much work, that "you could make it less work by first porting all the perl5 libraries that make perl5 OO more pleasant" isn't really a convincing solution :)
- steveklabnik 11y agoOne of my Twitter followers posted a gist: https://gist.github.com/paulodiniz/39698ac1fe5f62e3180e https://gist.github.com/paulodiniz/39698ac1fe5f62e3180e Sure, I was just addressing the "I don't know how to do that." Rails offer this for models in certain circumstances, for example.
- mst 11y agoThat's very cute. Yeah, I was responding to a post that claimed it was trivial; that's cute but not trivial - although it does demonstrate that using 'alias' to rename isn't necessary, which is nice to know. I can absolutely see how I'd turn this into a full Class::Method::Modifiers re-implementation, so I'll totally stipulate to 'trivial if' :D
- deleted 11y ago[deleted]