3 ms·
I think this is the beauty of Ruby. The core philosophy of Ruby is that it was designed to make "1.day.ago" possible. That's why DHH fell in love with it and us
by spyckie2 5y ago
I think this is the beauty of Ruby. The core philosophy of Ruby is that it was designed to make "1.day.ago" possible. That's why DHH fell in love with it and used it to build Rails. You can't do that with many other languages.
"1.day.ago" is not really a piece of code, it's a sensibly named shortcut to existing code that already solves easy problems. It's is not designed to be built upon; not all code has to be designed that way.
For example:
// How to get last day of month?
// Javascript
var month = 0; // January
var d = new Date(2008, month + 1, 0);
console.log(d.toString()); // last day in January
// monkey patching in ActiveSupport
Date.today.end_of_month
This is the definition of a great DSL. It abstracts away easy problems so you don't have to solve them hundreds of times and allows you to focus on the more unique problems of a domain.
Monkey patching is a powerful tool and just like any powerful tool it should be used carefully. Random monkey patching everywhere is bad, just like polluting the global namespace in javascript is bad. But used well and you can create extremely expressive, powerful, and concise coding experiences. Isn't that the goal of any DSL?
- matsemann 5y agoThink that should be possible in Kotlin as well, fwiw. Extension functions and extension properties are nice. And function with receivers make for some nice DSLs. All without monkey patching, which I consider a better way.
- lkjsaofinwdfsdf 5y agoThis is no longer a testimony of Ruby's strength, or rather this highlights Ruby's age and stagnation. D, Go, C#, Rust, Swift, Kotlin, Scala, Haskell, and (more I'm forgetting) already have this without the hack of monkey patching. If you really find beauty in DSL's you should look into tagless final encoding in Haskell. From what I recall Ruby still doesn't have first-class ADT's. Meanwhile Java will be getting sealed classes (the OO orthogonal to ADTs) with pattern matching.
- spyckie2 5y agoNot to overstate the point, but what many Rubyists want in programming is to mirror their code to a subset of English as much as possible, avoiding the more ambiguous parts of English. This is because if it makes sense in English, it is self documenting, clear, and intuitive. By DSL, I meant an abstract language on top of an existing programming language that serves a domain well and Ruby's monkey patching makes that easy to implement. Haskell's language, with it's custom non English symbols, and functional language with verb object or even operator at the end style... it's like dvorak vs qwerty. Even if it is superior, it's not the preferred one.
- vchuravy 5y agoIn Julia ``` using Dates julia> lastdayofmonth(today()) 2022-02-28 ``` But that's function oriented programming for you.