You can dynamically create methods. It's useful for reducing boilerplate. Sometimes you find yourself just having to implement almost identical methods. That's when it's useful to reach for Ruby's metaprogramming.
I've found it useful when doing Ruby and missed it using other languages that don't have it.
Like generics in C# or Java where you make a function work with multiple types thus saving you a lot of boilerplate, if the functions are nearly identical then I assume they do the same to a different type or something, maybe they return a different number type or a slightly different class, for those kinds of things we use generics in C#, those are runtime generated functions and methods just like the ones you’re talking about, it also avoids boxing and unboxing because there’s no casting involved, I don’t know if they’re generated at runtime in Java, they could be generated at compile time, but you get my point
74
u/[deleted] Jan 14 '21
In Ruby you can do this in a nice and useful way.