powerpack icon indicating copy to clipboard operation
powerpack copied to clipboard

New method #to_boolean for String

Open jwipeout opened this issue 8 years ago • 2 comments

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

jwipeout avatar Jul 15 '17 20:07 jwipeout

Overall this seems like a good idea, but I do wonder if we don't actually need two methods:

  • a to_boolean method that follows the Ruby boolean semantics and would convert anything that's not false and nil to true
  • 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.

bbatsov avatar Jul 17 '17 06:07 bbatsov

There's a related discussion with some other options here.

chocolateboy avatar Apr 01 '18 04:04 chocolateboy