powerpack
powerpack copied to clipboard
New method #to_boolean for String
Occasionally in rails projects I will find myself doing some comparison logic on a string that is returning 'true' or 'false'. Sometimes I will create a helper method or use locales to convert this, but thought it would be great to have a method that changes the data type.
Similar to
'1'.to_i #=> 1
Overall this seems like a good idea, but I do wonder if we don't actually need two methods:
- a
to_booleanmethod that follows the Ruby boolean semantics and would convert anything that's notfalseandniltotrue - this method you suggest, but named
as_boolean(or something like this)
I'm saying this, because I'd normally expect to_boolean to basically do what !! does.
There's a related discussion with some other options here.