4 ms·
Weeks(2).from_now I don't understand how monkey patching is beneficial beyond personal preference.
by pharrington 5y ago
Weeks(2).from_now
I don't understand how monkey patching is beneficial beyond personal preference.
- eloisius 5y agoThe entire Ruby ethos and surrounding APIs are basically all a result of a particular set of personal preferences. One of the core values of Ruby is programmer happiness. That’s obviously qualitative and varies from person to person. That said, this kind of pattern is extremely _Ruby_ and if you like it, you like it. I love writing Ruby code because of this kind of attention to making code almost like prose. However, I’ve also worked on a lot of terrible Ruby projects that were a mess of metaprogramming and clever dynamic language abuse, so it’s hard to mount a rigorous defense.
- chousuke 5y agoMy first experience with Ruby was writing rspec tests and chef recipes, and I can't say I was very happy when I had to debug that code. It's given me a deep aversion towards any Ruby DSL that still persist. I will work with Ruby if I have to, but I am not happy about it. The way Ruby does metaprogramming makes debugging extremely frustrating because your stacktraces will contain references to methods that exist nowhere. Good luck then trying to find where they're defined so you can understand how they work. I have a very strong opinion that generating methods at runtime is a terrible feature that should be used only when nothing else can be done.
- goatlover 5y agoThat looks like you're having to make a Weeks class just so you can do the same thing, instead of adding it to Integer and having it available on any int.
- eloisius 5y agoIn Ruby at least this example would just be an instance method named Weeks on the kernel class and it wouldn’t really be any more clunky to implement than the ActiveSupport flavor. However because Ruby you’re still reopening a class. You’re just adding a method to Kernel instead of Integer.
- Fire-Dragon-DoL 5y agoThat would just be a global method in ruby. At the top of any file: def Weeks(arg) end
- joppy 5y agoThere are languages which can automatically transform between a.foo(b) and foo(a, b) - for example the D language. Does this make 2.weeks.from_now more palatable? And if it does, then why worry about whether the code “goes on the class” or not? During execution it doesn’t really matter where the code lives, and during development open classes are doing basically the same thing as defining functions overloaded on the first argument.