One-liner methods in Ruby are useful when you have commonly used variants of classes, but don't want to initialize them with the same arguments or via class methods.
This is a Phlex component that I initialize via `BoxComponent.new`, and variants via `BoxComponent::Small.new`, etc.
This is a Phlex component that I initialize via `BoxComponent.new`, and variants via `BoxComponent::Small.new`, etc.
Comments
Another approach I used in the past would have been a class method like `BoxComponent.small` that called `new` with the desired args.
Putting a bunch of really small methods in a class is a handy way to keep your components composable 🎼
That’s not always the case though—for example you might set `@buffer = https://StringIO.new` in an initializer. That wouldn’t work for an endless Ruby method since it would always return a new instance.
at first glance, it feels like style? like, i guess this is about the shortest way to achieve this, w/o losing clarity 👀
— feels very ✨Ruby✨ to me, tho ultimately subjective; hence ‘style’ 🤔
or is there more to this, @bradgessler.com ?
I think Configuration classes could benefit from this approach too.
Haven’t thought about much beyond that.
thinking on it, i guess i kinda kneejerked a *bUT dOEs iT sCaLE* reaction there, when i damn well know this approach is clean af
also, i rly should start building with Phlex right 👀
🙏
My mental model was, “Ruby methods are probably really fast”, which the benchmarks confirm.