String already converts itself to other types.  "4".to_i becomes the 
Integer 4.  Invalid values, other than the ones listed (like "foobar") 
would raise an argument error.

Wael, thanks for the reference to ActiveModel!  This is definitely similar. 
 Would this implementation be better for everyone?  I would like to be 
explicit about true/false values, whereas the implementation in ActiveModel 
only has explicit false types.  I feel this belongs on ActiveSupport 
because it's commonly included on other projects (including ActiveModel), 
and has no further dependencies.

On Monday, April 4, 2016 at 4:23:51 AM UTC-5, Mohamed Wael Khobalatte wrote:
>
> Conversion between types is fairly common in Ruby. With that said I'd 
> recommend you look into `ActiveModel::Type::Boolean.new.cast(my_value)`, 
> which handles the all cases above.
>
> On Mon, Apr 4, 2016 at 10:31 AM, Greg Navis <[email protected] 
> <javascript:>> wrote:
>
>> Hey!
>>
>> I think this would cause confusion. First, the logical values of `string` 
>> and `string.to_bool` would be different. Second, what would you return for 
>> a string "foobar"?
>>
>> Best regards
>> -- 
>> Greg Navis
>> I help small software companies to scale Heroku-hosted Rails apps.
>> Free, biweekly scalability newsletter for SaaS CEOs 
>> <http://www.gregnavis.com/newsletter/>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby on Rails: Core" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/rubyonrails-core.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Wael Khobalatte
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to