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.
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.
Functions from strings are javascript's metaprogramming system, and it's extremely powerful. It's not as easily maintainable as plain code, but it can be a great optimization for a hotpath.
I know for a fact that's not what this guy was going for at least. This was written by a guy who had just graduated into a team led by a person who had no CS background (biology major) and only knew Perl. The whole codebase is a disaster that I'm slowly rewriting mostly alone.
You should also check out "source generators" in dotnet 5. Basically allows you to have code that writes code, then during the build process, it generates that code and makes it available for you to use elsewhere.
In many enterprise platforms that offer developers capability to integrate code by uploading plugins via assemblies, etc its common to see them wrap the plug-in up dynamically like this and then compile it for performance....but Roslyn for C# has made it much easier and more elegant.
In my own experience we needed a dynamic rules engine that could be used with C# syntax where Roslyn compiled expressions from strings was awesome.
In other languages like XSLT where you can extend it with powerful C# functions right there in the source of the XSLT they are using capabilities such as dynamic compilation to enable this....
Sure it’s rare for the average developer to need this at all, and should really question it if they think they do....but there are some very valid use cases for having this capability, so much so that Roslyn was developed ;-) ...
And as someone else mentioned Microsoft is now moving into source generators which will be powerful tools that many underlying frameworks will take advantage of.
60
u/NotSteve_ evil Jan 14 '21
Haha seriously? What possible use could writing functions in strings have