Why "# {String}" is a common idiom in Ruby
What is the broader context of some of the customs? The only other thing I can think of, beyond what has already been mentioned, is like trying to lose type safety; that is, you can take anything as an argument, and that can ensure that whatever you go for walks is like a duck ... or, well, a string (although string.to_s
it might be clearer though).
All in all, though it's probably a code smell that the person thought was "Best Practice".
a source to share
Interesting answers, everyone. I am the developer who asked the original question. To give some context, I see this sometimes in my current work, and also sometimes in sample code in a Rails list with variables that are known in advance to contain strings. I could understand this as a replacement for to_s, but I don't think what's going on here; I think people just forget that you don't need the interpolation syntax if you are just passing in a string variable.
If anyone tried to tell me it was best practice, I would have escaped as quickly as possible.
a source to share
I could imagine this is useful in cases where the object being interpolated is not always a string, since the interpolation implicitly calls #to_s
:
"#{'bla'}" => "bla"
"#{%r([a-z])}" => "(?-mix:[a-z])"
"#{{:bla => :blub}}" => "blablub"
Might make sense when registering something where you don't care that much about the output format but never want an error due to the wrong argument type.
a source to share