4 ms·
Rails and Ruby bake in a lot of really nice, ergonomic features for every day programming, not just larger architectural decisions. For instance I am working on
by prescriptivist 5y ago
Rails and Ruby bake in a lot of really nice, ergonomic features for every day programming, not just larger architectural decisions. For instance I am working on a React Native app in my spare time and so much of Javascript seems tedious. Example is dealing with date ranges.
Contrived example:
import { isWithinInterval, subDays, addDays } from 'date-fns';
const today = new Date();
let range = {
start: subDays(today, 1),
end: addDays(today, 1)
}
isWithinInterval(today, range)
The functions for this kind of calculation has to be imported into any file I want to use them? There seems like a lot of copy pasting of this kind of stuff everywhere in a robust javascript app. I think it's reasonably easy to read and understand, so I don't fault that, it just seems tedious to use tons of libraries that you have to explicitly import everywhere to accomplish simple things. Feels like death by a thousand cuts.
Meanwhile in Ruby (with Rails) this is functionally equivalent and is usable everywhere in the code:
Range.new(Date.yesterday, Date.tomorrow).include?(Date.today)
edit: fixed some bugs :)
- jmondi 5y agoNot copying and pasting that code chunk and wrapping it into a utility function :red-flag: :red-flag: :red-flag:
- prescriptivist 5y agoIt's a contrived example, as I say. The point is constructing intervals and doing the interval range checks requires importing those functions everywhere and constructing shape objects for them to use. Don't read the code literally. Obviously you can wrap them in a your own interval function which does the same thing. So like: function myOwnIntervalBetweenFunction(date, offsetBefore, offsetAfter) Which is more hygienic but my point is I don't have to do that with Ruby ever.
- gls2ro 5y agoIn Ruby it also works a simpler version: (Date.yesterday .. Date.tomorrow).include?(Date.today)
- akarlsten 5y agoIn my opinion having to be explicit about what imports are used where makes the code more readable. There's never any "mystery" methods suddenly being called in the middle of a complex function (sometimes without a namespace so you're not even sure which library it's from), and I suppose it makes stuff like tree-shaking easier as well.
- Too 5y agoUse https://momentjs.com https://momentjs.com instead moment('2022-02-05').isBetween(moment().startOf('day').subtract(1, 'day'), moment().endOf('day')); Still requires one import but that's hard to avoid. Don't be afraid of them, the IDE will usually add them automatically and hide them anyway. It's a good way to increase tracability maintainability, instead of magic stuff happening in the background.
- kavaruka 5y agomoment is dead, nowadays there are better libraries https://momentjs.com/docs/#/-project-status/ https://momentjs.com/docs/#/-project-status/