What's the difference between sending a string and sending a symbol to call a method dynamically, e.g., foo.public_send(:bar)
vs foo.public_send('bar')
? Is there a concrete difference in how these are handled?
If symbols are better, is it worth to do foo.public_send('bar'.to_sym)
if for some reason you need to construct your method name as a string?
There is no difference between them, in fact, when passing a string it is converted to a symbol.
No need to convert it since that same conversion (e.g. 'bar'.to_sym
) will be done if a string is provided.
From the docs:
Invokes the method identified by symbol, passing it any arguments specified. Unlike send,
#public_send
calls public methods only. When the method is identified by a string, the string is converted to a symbol.