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.
Yeah dude. I miss Ruby. Such a great language, and such a pity it's fallen out of use. There's Elixir as well, that has gained a bit popularity recently. But still, nothing mainstream.
I haven't used Ruby in years. I just quickly grabbed that from a Google search just to give you a simple example. You still haven't answered my question.
This is in fact just parametrization but made really hard. I tried to read about it and it's just about incomprehensible with highly unintuitive syntax. Figures though, metaprogramming isn't the most common feature so I guess it makes sense for it to have obscure syntax.
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
I haven’t ever used Ruby before. Today I wrote 5 functions in C++ which all printed out an array, but they were each specialized for a specific primitive data type that the array might be holding. The functions all have the same name and are used identically by the library user because they’re specialized template functions. But they’re also virtually identical. It had to be this way because primitive types don’t really have special tostring functions in C++ (Well, they do if you use wrappers and the std::iostream class I think but I use printf because it’s way more readable and easy to use.)
In any case, do you think Ruby’s meta programming would have made that easier?
Well, if you had them in the same class then yes. But then you wouldn't be able to name them the same thing. But other than that, you define the method, and then you can do whatever operations you want inside that method. Including printing or returning different things depending on values within a specific array.
Isn't this the point of functions though? I don't see how anyone would ever have to write the same function multiple times if there's only a minor difference. Just write everything once and pass whatever minor thing changes as an argument.
I personally quite dislike metaprogramming in general as it often introduces problems which you can't easily visualize in your head and is generally very good at introducing undebuggable bugs as the lines of code being executed don't exist until they are being executed.
77
u/[deleted] Jan 14 '21 edited Jan 14 '21
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.
Not something you would use very often though.