I'm trying to find a way to evaluate an expression that consists of a function and argument. The problem is, I don't want the argument converted into a string.
Specifically, I have the function x="display_datetime" of type String, which takes an object y of type Time. The function name is created dynamically, so I can't just type, display_datetime(y).
What I've tried:
eval("#{x}(#{y})") - This is how I'd normally do it, but this converts the Time to a string. The whole purpose of "display_datetime" is to do a custom Time to string conversion, so this would not be useful
x.send(y) - This does not compile, because send is meant to send classes functions, not functions arguments.
x.constantize... - also doesn't compile, even if x includes the classname.
- y.send(:to_s(:datetime)) This approach is less than ideal, but I still can't get it to work. display_datetime just calls y.to_s(:datetime), so I tried setting the dynamically generated function name x to simply be .to_s(:datetime), then invoking x on y with the send command. This would work, except for the fact that to_s takes an argument. I don't know how to call send (or try) when the argument being sent has to take an argument
- Another idea I've had is to override the Time class to add a conversion function to it like to_s, but that doesn't take any argument.
Those last two approaches are less than ideal. I feel like there has to be some better way of doing this along the lines of the first 3 approaches.
Any feedback is appreciated.
Thanks
display_datetime