News for hackers. Stuff that matters.
Re:We Fell Out of Love with Next.js and Back in Love with Ru... by t-writescode on Sunday May 04, 2025 @07:58AM (#43886233)
To add, the above code is a pretty near approximation of the literal code inside the devise codebase, which is a very standard Ruby auth system.

See here:

https://github.com/heartcombo/devise/blob/main/lib/devise/co...

        def self.define_helpers(mapping) #:nodoc:
        mapping = mapping.name

        class_eval <<-METHODS, __FILE__, __LINE__ + 1
          def authenticate_#{mapping}!(opts = {})
That code is *literally* calling class_eval with a multi-line string parameter, where it inlines the helper name (like admin, user, whatever), to grow the class at runtime.

It hurts my soul.

Re:We Fell Out of Love with Next.js and Back in Love with Ru... by snkzxbs on Sunday May 04, 2025 @11:46AM (#43887389)
Thank you both for the time you spent explaining this.
Re:We Fell Out of Love with Next.js and Back in Love with Ru... by chris12321 on Sunday May 04, 2025 @09:34PM (#43891193)
It's been widely understood in the Ruby community for some time now that metaprogramming—like in the example above—should generally be limited to framework or library code, and avoided in regular application code.

Dynamically generated methods can provide amazing DX when used appropriately. A classic example from Rails is belongs_to, which dynamically defines methods based on the arguments provided:

class Post < ApplicationRecord belongs_to :user end

This generates methods like:

post.user - retrieves the associated user

post.user=(user) - sets the associated user

post.user_changed? - returns true if the user foreign key has changed.

Re:We Fell Out of Love with Next.js and Back in Love with Ru... by t-writescode on Monday May 05, 2025 @12:06AM (#43891867)
Aren’t all these enhancement methods that are added dynamically to every ActiveRecord a major reason why regular AR calls are painfully slow and it’s better to use .pluck() instead? One builds a whole object from pieces, the other vomits put an array?
Re:We Fell Out of Love with Next.js and Back in Love with Ru... by chris12321 on Monday May 05, 2025 @08:08PM (#43900655)
It's simply not true that "regular AR calls are painfully slow." In the context of a web request, the time spent instantiating Active Record objects is negligible. For example, on my laptop:

Customer.limit(1000).to_a

completes in about 10ms, whereas:

Customer.last(1000).pluck(:id, :name, :tenant_id, :owner_id, :created_at, :updated_at)

runs in around 7ms.

Active Record methods are defined at application boot time as part of the class, they're not rebuilt each time an instance is created. So in a typical web app, there's virtually no performance penalty for working with Active Record objects.

And when you do need raw data without object overhead, .pluck and similar methods are available. It’s just a matter of understanding your use case and choosing the right tool for the job.