frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Build a minimal decorator with Ruby in 30 minutes

https://remimercier.com/minimal-decorator-ruby/
48•unripe_syntax•8mo ago

Comments

inopinatus•8mo ago
consisting of:

1. spend 30 minutes beating around the bush, reinventing the wheel etc for your own education and interest; then

2. throw that away and use SimpleDelegator from the standard library

and to be clear, I don’t mean this negatively at all.

pantulis•8mo ago
Sounds similar to begin building your own class and end up using an OpenStruct.
byroot•8mo ago
As a rule of thumb you never want to use OpenStruct. It's basically soft deprecated at this point because of its atrocious performance.
Mystery-Machine•8mo ago
Just use the new Data class: https://docs.ruby-lang.org/en/3.2/Data.html
estsauver•8mo ago
Doesn't this thrash the ruby VM's virtual method cache? (It's been ~7 years since I was working on production ruby code, but I remember it being a really painful performance issue for us when using dynamic method missing routing. I might be getting it confused with class extensions though.)
julienbourdeau•8mo ago
Very good question! The docs doesn't mention anything (unlike OpenStruct for instance)
byroot•8mo ago
Not certain what you mean by "virtual method cache", the Ruby VM has multiple layers of method cache (or call cache) but I've never heard any of them referred to as the "virtual method cache".

For the inline call caches in the interpreter loop, they are monomorphic, so if you call the same codepath with the decorator and the actual object, they will indeed flip flop.

The second layer of cache is class based, so after the inline cache is defeated you will end up doing a single hash-table lookup on the class.

As for YJIT, IIRC it does handle polymorphic call caches, so it won't mind such situation at all unless you have more than a handful of different implementations of that method being called at a given callsite.

firecall•8mo ago
Maybe I'm doing it wrong, but I've regretted my previous choice of using the Draper Gem and Decorators when coming back to old projects.

I found Decorators just obscured display logic away in an inconvenient way.

RangerScience•8mo ago
Might've been wise at the time, but nowadays I think the recommendation is to use View Components to package up view logic.

(I'm personally so-so on VCs; I think the core idea is pretty good but I'm not super sold on some of the implementation details - too OOP, not enough `yield`)

pmontra•8mo ago
Rails has view helpers for that. Instead of

  <td class="<%= teacher.colour_coded_availability %>">
there would be

  <td class="<%= colour_coded_availability(teacher) %>">
but no metaprogramming, no decorators/ directory, no new concepts, only standard plain Rails. As a bonus it's even an instance of functional programming.
arthurlewis•7mo ago
Well, until you need different `colour_coded_availability` methods for different contexts. Helpers get a lot done, but there are absolutely use cases for a separate presentation layer.
ht85•8mo ago

    (maximum_number_of_students <=> students.size).clamp(0..)
Holy... Is this better or worse than write-once perl regexes?
ricardobeat•8mo ago
I don’t get how this works. Won’t the spaceship operator always return 1, 0, -1?
onli•8mo ago
Yes. And then clamp(0..) removes the -1, maps it to a 0. Why you'd want that? No idea.
mdoliwa•8mo ago
so this just says if we have free places or not? students.size < maximum_number_of_students
jackbracken•8mo ago
It gives the number of free places, hinted by the method name `available_places`

Needlessly ugly way to write it imo though

onli•8mo ago
But it doesn't. The code will only ever output 0 or 1. With a regular <=> operator at least.
mdoliwa•8mo ago
yeah, so basically 1 we have available places, 0 we do not.
vidarh•8mo ago
I think it probably made sense to the author because they've used all three (-1,0,1) for other examples, and would've been fine until separated out to a method reused to show the actual number.

I think they tried to be a bit too clever, basically.

onli•8mo ago
I started writing a comment wanting to defend it, because the expressions themselves are not that unreadable. <=> is a comparator (and used in many languages), clamp sets min and max sizes (and that can be guessed from the english meaning of the word), and 0.. is a range that goes from 0 to infinity (admittedly not that common an expression, but completely logical and intuitive way to express a range once you understood once that it is about ranges).

But then I realized that's nonsensical, and not what the code is supposed to do given the usage in the template. I assume something got mangled there when changing the code for the blog post.

Or I'm just understanding the ruby code wrong despite checking just now in irb, in that case that's a point for the intention of your comment, and a vote for "worse".

sathishmanohar•8mo ago
The same can be achieved with

  [maximum_number_of_students - students.size, 0].max
sathishmanohar•8mo ago
Wait. are we confusing decorator pattern where a Teacher can be AvailableTeacher or UnavailableTeacher with decorating the web page which is the role of view and view helpers?
berkes•8mo ago
What annoys me (only slightly) in the Ruby community, is it's loose and often "wrong" usage of design patterns.

IMHO, the whole idea of "design patterns" is that they are a standardized way of doing stuff. So that when someone, regardless of language, says "use an abstract factory" we know what it does, how it can be used, what it doesn't do, it's pitfalls etc.

What ruby calls "decorators" aren't "decorators". That's for this DIY implementation, but even more so for "Draper".

It's not only that an actual Decorator should not introduce new methods, but also how it's set-up and used. And therefore, what it's pitfalls and downsides are.

In the case of Draper or this DIY: the views now rely on a concrete and specific subclass - losing both LSP and DIP. Worse: the layer using it -views- aren't the one setting it up or controlling it - the controllers. So it introduces very hard dependencies between different domains - the views/partials/serializers now depend on whether or not you've remembered to actually decorate the model. Something that in my experience will go wrong and will crash in production - or if you're lucky and have lots of E2E tests, in CI.

The same "imprecise use of design patterns" happen with MVC, Models, ActiveRecord, "interfaces" and so forth. Often because of limitations of Ruby, but rather often because the Ruby community just uses the term "wrong".

baobun•8mo ago
> Often because of limitations of Ruby

wat.

If anything I think Rubys lack of limitations could be the issue. There's a thousand different ways to do anything, and people do.

berkes•7mo ago
Ruby doesn't have "interfaces" - obviously, because it doesn't have types or a type-enforcement.

So any design pattern, architecture or concept that relies on types, or interfaces, are "limited" in that sense. Ports, Adapters, Strategy, for example " require" interfaces in their definition. Their benefits rely on interfaces, so if a language lacks this, you really only get the downsides. Factory, Observer, Decorators, etc mention them, and use them, but can be implemented without them.

Maybe "limitations" isn't the best word, because e.g. "an interface" is a deliberate limitation, imposed and designed by the developer.

nsonha•8mo ago
yeah I'm not familiar with rails and when I read this I was like what do they think a decorator is? Something that literally decorates with colors?
berkes•7mo ago
Yes, that's very much how the Rails community thinks about "decorators": something that "decorates" the UI elements.
arthurlewis•7mo ago
> It's not only that an actual Decorator should not introduce new methods

Maybe I'm misreading you, but I've never seen a definition of the Decorator pattern that doesn't involve new methods on the Decorator. If that's what you mean, can you say more? If not, what do you mean?

arthurlewis•7mo ago
OH WAIT I SEE IT I'M THINKING OF THE FACADE PATTERN. Yes, the Ruby community does tend to conflate Decorator, Facade, and Presenter in very confusing ways.
Fire-Dragon-DoL•7mo ago
I mean, the ruby community thinks of draper when they talk about Ruby's decorators, so they have a unified definition, even if it conflicts with the definition outside of ruby world
berkes•7mo ago
FWIW: I was referring to this:

> Note that decorators and the original class object share a common set of features. In the previous diagram, the operation() method was available in both the decorated and undecorated versions.

and

>

GoF Book, "Design Patterns: Elements of Reusable Object-Oriented Software", p.166:

> "A decorator object conforms to the interface of the component it decorates so that its presence is transparent to clients."

RangerScience•7mo ago
> IMHO, the whole idea of "design patterns" is that they are a standardized way of doing stuff

To disagree - Design patterns are a communication language. You use them to talk about code; if you use them as strict recipes they become dogma, with all the problems that entails.

Beyond that - most of the canonical design patterns are coping mechanisms for the limitations of pre-modern Java / strict OOP. You just plain don’t need them in a language like Ruby - IMO, mostly because of ‘yeild’.

The “imprecise use” is a consequence of either trying to use an unnecessary design pattern, or trying too dogmatically to adhere to one.

berkes•7mo ago
> communication language

I very much agree.

But that means when we both say "decorator pattern", we must mean the same. The thing that in the Ruby community is a "Decorator" isn't a "decorator pattern".

It's not following the details, but also not even meant for the use-cases or to solve limitations of ruby or strict OOP. It's simply not a "decorator" that we both think of (or would google, or read in e.g. GoF). It's something entirely different. In ruby/Rails (draper gem, most notably), they happen to use the word "Decorator" for a system that makes "view helpers" OOP rather than the normal rails way of a large bucket of random global helper-functions.

I've had this conversation that shows the problem:

- Me: If we'd use the decorator pattern, we could easily tame that large clumsy tree of subclasses in our authorization logic and make it much easier testable and understandable.

- Wat? Decorators? But our authn has nothing to do with views! Why would we want to pull views-logic into our authn layer?

So, by using the term wrong, at least N=1 shows how the "communication language" of Design PAtterns fails.

arthurlewis•7mo ago
Couldn’t get this idea out of my mind so I went back to the GoF text:

> Decorator subclasses are free to add operations for specific functionality. For example, ScrollDecorator’s ScrollTo operation lets other objects scroll the interface if they know there happens to be a ScrollDecorator object in the interface.

berkes•7mo ago
but also the next sentence:

> ...The important aspect of this pattern is that it lets decorators appear anywhere a VisualComponent can. That way clients generally can't tell the difference between a decorated component and an undecorated one, and so they don't depend at all on the decoration.

and further on:

> The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component's clients.

lmz•8mo ago
Reminds me of this recent comment in another thread re: Rails:

https://news.ycombinator.com/item?id=44253645

byroot•8mo ago
The `method_missing` signature isn't correct since Ruby 3.0, as it doesn't handle keyword arguments.

It should be:

    def method_missing(name, *args, **kwargs, &block)
Starting from 3.1 it can be:

    def method_missing(name, ...)
sirk390•8mo ago
All that complexity when you could simply do:

  <td class="<%= @teacher.available_places> 0 ? 'bg-colour-green' : 'bg-colour-red' %>">

Reverse Engineering Medium.com's Editor: How Copy, Paste, and Images Work

https://app.writtte.com/read/gP0H6W5
1•birdculture•3m ago•0 comments

Go 1.22, SQLite, and Next.js: The "Boring" Back End

https://mohammedeabdelaziz.github.io/articles/go-next-pt-2
1•mohammede•9m ago•0 comments

Laibach the Whistleblowers [video]

https://www.youtube.com/watch?v=c6Mx2mxpaCY
1•KnuthIsGod•10m ago•1 comments

I replaced the front page with AI slop and honestly it's an improvement

https://slop-news.pages.dev/slop-news
1•keepamovin•14m ago•1 comments

Economists vs. Technologists on AI

https://ideasindevelopment.substack.com/p/economists-vs-technologists-on-ai
1•econlmics•17m ago•0 comments

Life at the Edge

https://asadk.com/p/edge
2•tosh•22m ago•0 comments

RISC-V Vector Primer

https://github.com/simplex-micro/riscv-vector-primer/blob/main/index.md
3•oxxoxoxooo•26m ago•1 comments

Show HN: Invoxo – Invoicing with automatic EU VAT for cross-border services

2•InvoxoEU•27m ago•0 comments

A Tale of Two Standards, POSIX and Win32 (2005)

https://www.samba.org/samba/news/articles/low_point/tale_two_stds_os2.html
2•goranmoomin•30m ago•0 comments

Ask HN: Is the Downfall of SaaS Started?

3•throwaw12•31m ago•0 comments

Flirt: The Native Backend

https://blog.buenzli.dev/flirt-native-backend/
2•senekor•33m ago•0 comments

OpenAI's Latest Platform Targets Enterprise Customers

https://aibusiness.com/agentic-ai/openai-s-latest-platform-targets-enterprise-customers
1•myk-e•36m ago•0 comments

Goldman Sachs taps Anthropic's Claude to automate accounting, compliance roles

https://www.cnbc.com/2026/02/06/anthropic-goldman-sachs-ai-model-accounting.html
2•myk-e•38m ago•5 comments

Ai.com bought by Crypto.com founder for $70M in biggest-ever website name deal

https://www.ft.com/content/83488628-8dfd-4060-a7b0-71b1bb012785
1•1vuio0pswjnm7•39m ago•1 comments

Big Tech's AI Push Is Costing More Than the Moon Landing

https://www.wsj.com/tech/ai/ai-spending-tech-companies-compared-02b90046
4•1vuio0pswjnm7•41m ago•0 comments

The AI boom is causing shortages everywhere else

https://www.washingtonpost.com/technology/2026/02/07/ai-spending-economy-shortages/
2•1vuio0pswjnm7•43m ago•0 comments

Suno, AI Music, and the Bad Future [video]

https://www.youtube.com/watch?v=U8dcFhF0Dlk
1•askl•45m ago•2 comments

Ask HN: How are researchers using AlphaFold in 2026?

1•jocho12•48m ago•0 comments

Running the "Reflections on Trusting Trust" Compiler

https://spawn-queue.acm.org/doi/10.1145/3786614
1•devooops•52m ago•0 comments

Watermark API – $0.01/image, 10x cheaper than Cloudinary

https://api-production-caa8.up.railway.app/docs
1•lembergs•54m ago•1 comments

Now send your marketing campaigns directly from ChatGPT

https://www.mail-o-mail.com/
1•avallark•57m ago•1 comments

Queueing Theory v2: DORA metrics, queue-of-queues, chi-alpha-beta-sigma notation

https://github.com/joelparkerhenderson/queueing-theory
1•jph•1h ago•0 comments

Show HN: Hibana – choreography-first protocol safety for Rust

https://hibanaworks.dev/
5•o8vm•1h ago•1 comments

Haniri: A live autonomous world where AI agents survive or collapse

https://www.haniri.com
1•donangrey•1h ago•1 comments

GPT-5.3-Codex System Card [pdf]

https://cdn.openai.com/pdf/23eca107-a9b1-4d2c-b156-7deb4fbc697c/GPT-5-3-Codex-System-Card-02.pdf
1•tosh•1h ago•0 comments

Atlas: Manage your database schema as code

https://github.com/ariga/atlas
1•quectophoton•1h ago•0 comments

Geist Pixel

https://vercel.com/blog/introducing-geist-pixel
2•helloplanets•1h ago•0 comments

Show HN: MCP to get latest dependency package and tool versions

https://github.com/MShekow/package-version-check-mcp
1•mshekow•1h ago•0 comments

The better you get at something, the harder it becomes to do

https://seekingtrust.substack.com/p/improving-at-writing-made-me-almost
2•FinnLobsien•1h ago•0 comments

Show HN: WP Float – Archive WordPress blogs to free static hosting

https://wpfloat.netlify.app/
1•zizoulegrande•1h ago•0 comments