Your sample code has not expanded to part of your topic, namely characters, and so part of the question has remained unanswered.
If you have two lines: foo and bar, and both can be either a string or a character, you can check for equality with
foo.to_s == bar.to_s
Itβs a bit more efficient to skip string conversions on operands with a known type. Therefore, if foo is always a string
foo == bar.to_s
But the gain in efficiency is almost certainly not worth the extra work on behalf of the caller.
Prior to Ruby 2.2, avoid using uncontrolled input strings for comparison (with strings or characters), since characters are not garbage collected, and therefore you can open yourself up for denial of service by running out of resources. Limit the use of characters to the characters you control, i.e. Literals in your code and reliable configuration properties.
Ruby 2.2 introduced garbage collection of characters .
sheldonh Nov 11 '09 at 11:10 2009-11-11 11:10
source share