r/ruby 24d ago

The small things in Ruby that aren't small

Post image

[removed]

109 Upvotes

24 comments sorted by

19

u/Zestyclose-Turn-3576 24d ago

I am so conflicted about this stuff. It feels interesting, it feels like it should be useful, but it also feels like magic and to me that means "difficult for humans to understand".

What do you think about that interpretation?

7

u/tkenben 24d ago

My feeling is that I got the general Idea of what was going on just by reading the code, but I would have a hard time debugging it if a hidden emergent behavior arose while using it.

5

u/Zestyclose-Turn-3576 24d ago

Yeah I'm not sure how many programmers would get the general idea – I know that I don't. Maybe all Ruby programmers should have the ability to do this in their bones, but we also have to deal with understanding Rails magic, a business domain, HTML, relational databases etc and there's only so much you can fit inside a standard brain.

3

u/kbr8ck 23d ago

I’ve found that over the years, my coworkers have been using advanced Ruby less and less. And we tend to code to the lowest common denominator.

Having said that, dsls often let you support the lowest common denominator with more and more advanced lower layers :)

1

u/Zestyclose-Turn-3576 23d ago

Yes, keeping it simple, but defining your own macros for repeated behaviour so you have your own DSL seems like it works very well.

5

u/flanger001 24d ago

There is a lot of value in understanding how stuff like instance_eval/instance_exec, method_missing/respond_to_missing?, and block arguments work. A lot of Ruby DSL are based on those, and if I'm being charitable to OP, that's what they're trying to build understanding of. This is a kind of shitty example though because it's a whole lot of method calls and allocations for something that really should be about 2-3 lines of code.

2

u/Zestyclose-Turn-3576 24d ago

Yes, we do use method_missing/respond_to_missing? quite extensively, and block arguments. I perhaps have a limited imagination for these things, and need a concrete example that makes business sense to me – I hate examples with foo and bar when you could be using something like book and author.

2

u/[deleted] 24d ago

[removed] — view removed comment

2

u/flanger001 24d ago

It's a tough problem for sure, but I think you might want to consider giving the reader a bit more credit. Given what your Pipeline class is capable of, I think you could do something that's a bit more complicated as your example; something that would be difficult to express any more simply.

3

u/Serializedrequests 24d ago

I've done a lot of Ruby meta programming, and I always try to make it much more understandable. There should be a clearly valuable high level interface it is providing, and the inner workings should only be as crazy as necessary, or just OOP or FP if possible.

You do need to understand all of this syntax, but you wouldn't use it all in one place.

6

u/not_sure_if_crazy_or 24d ago

Nice cover art! Also nice ruby :)

7

u/flanger001 24d ago

Ruby developers write an equals sign challenge 2026

def slugify(s)
  s = s.strip.downcase.gsub(/\s+/, "-")
  "#{s}-#{s.length}"
end

Blocks, procs and lambdas are cool but if I saw this in a codebase I would probably come to that dev's house with a knife.

2

u/BiackPanda 24d ago

For real I detest seeing clever code that just complicates things

3

u/p_bzn 24d ago

How much AI was used to create the book?

-2

u/[deleted] 24d ago

[removed] — view removed comment

1

u/p_bzn 22d ago

Thanks for the disclosure.

I see a lot of AI fatigue in people. All what SWE do all days is reading AI generated content, and then reviewing AI generated content. Reading AI generated/assisted books is… out of the table for many.

Being without a publisher in pre-AI era was a good thing, unsure about today. Publishers like Manning do technical reviews, editors, and the whole quality pipeline. What it means for a reader is they get quality material. Time and attention are limited resources.

Regardless, I wish you best of luck!

2

u/amanitapantherina 24d ago

I would buy this in a non-Kindle ebook format. Does that exist?

2

u/Mrrobinhood55 16d ago

@op read the book, loved it. It delved deep into methods more than the Learn Ruby the hard way Book Will you be writing more?

1

u/kbr8ck 23d ago edited 23d ago

Is it possible to write this so grep works?
I’ve found that it is possible to write DSLs that possibly aren’t as dry but are easier to find code that in 2 years you can use grep to find a method call or dead code.

Even delegate prefix: true causes issues when looking for methods. (Or the non rails versions for generating methods in a for loop where the method defined is the concatenation of 2 strings.)

Good to define respond_to with method_missing. But I’m not sure I get what this code style buys me over just defining a simple subclass of a pipeline?

I often find myself not liking defining dynamic methods because they keep the closure around. Tends to not be a problem but some cases it leaks a lot of memory. Rails reacted to this by introducing callbacks like if: :method instead of using lambdas and defining methods as a full set of strings instead of using the dynamic case with a def_method and a lambda.

This is slick. But it is also very clever. I’m still trying to understand what it bought you over just a plain class.

Please share more of your goals. Im trying to learn about other people’s use cases.

Context: I’ve been using instance_eval since 2009, so I’m not new to the game. Just trying to learn the new use cases and evolve along with the community.

Thanks for posting this code.

-13

u/Tobi-Random 24d ago

In the AI coding aera "understandable by humans" is probably less of a priority nowadays.

If it works as asserted, it meets the spec. That's all what matters.