TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Class methods are Ruby's useEffect

19 pointsby monkey_slap7 months ago

5 comments

gerjomarty7 months ago
As an aside, if you need to play with datetime objects in Ruby, you almost certainly want to be using the Time library instead of the DateTime library, even though one of them sounds like it fits better.<p>At the top of the DateTime docs [1]:<p>&gt; DateTime class is considered deprecated. Use Time class.<p>The same doc explains when you should use DateTime by using an anecdote about William Shakespeare and Miguel de Cervantes dying on the same day - except they didn&#x27;t because England and Italy used different calendars at the time. [2]<p>DateTime is great at dealing with historical pre-1970 dates, otherwise just use Time.<p>[1]: <a href="https:&#x2F;&#x2F;docs.ruby-lang.org&#x2F;en&#x2F;3.3&#x2F;DateTime.html" rel="nofollow">https:&#x2F;&#x2F;docs.ruby-lang.org&#x2F;en&#x2F;3.3&#x2F;DateTime.html</a> [2]: <a href="https:&#x2F;&#x2F;docs.ruby-lang.org&#x2F;en&#x2F;3.3&#x2F;DateTime.html#class-DateTime-label-When+should+you+use+DateTime+and+when+should+you+use+Time-3F" rel="nofollow">https:&#x2F;&#x2F;docs.ruby-lang.org&#x2F;en&#x2F;3.3&#x2F;DateTime.html#class-DateTi...</a>
评论 #41874833 未加载
jemmyw7 months ago
I find it very odd to compare with useEffect. There&#x27;s a whole bunch of things you can do in code that you could do another way, or might not be advisable for particular circumstances. And that&#x27;s just about the only thing linking useEffect, a function call in React for component lifecycle, with class methods. I don&#x27;t think it&#x27;s a good idea to think about these two things in the same way.
评论 #41875337 未加载
dzsekijo7 months ago
The post says &quot;Most of our Ruby code reads like English, but that’s not the case when we combine multiple DateTimeFns class methods&quot;, and to illustrate this, gives the following agreeably ugly example:<p><pre><code> DateTimeFns.add_minutes(DateTimeFns.add_hours(DateTime.current, 1), 1) </code></pre> Sure thing, but the syntax can be improved without restorting to refactor into instance methods:<p><pre><code> DateTime.current.then { DateTimeFns.add_hours(_1, 1) }.then { DateTimeFns.add_minutes(_1, 1) }</code></pre>
SebastianKra7 months ago
As an outsider to the Ruby-World but someone who has been burnt enough times by instances unexpectedly mutating themselves, I wonder: is it still a best practice in Ruby to mutate instances when computing values? Does Ruby somehow avoid the problems that you would encounter when doing this in JS&#x2F;Java&#x2F;C#?
ilrwbwrkhv7 months ago
A bit of a stretch to compare them to each other.