> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/cast-common-boolean-like-values-to-booleans.md).

# Cast Common Boolean-Like Values To Booleans

Sometimes you have to deal with values that are supposed to represent booleans, but they aren't actually boolean values (i.e. `"t"` instead of `true`). Rail's `ActiveModel` has a helper for casting these common boolean-like values to actual booleans.

```ruby
> ActiveModel::Type::Boolean.new.cast('f')
=> false
> ActiveModel::Type::Boolean.new.cast('t')
=> true
> ActiveModel::Type::Boolean.new.cast('true')
=> true
> ActiveModel::Type::Boolean.new.cast('FALSE')
=> false
> ActiveModel::Type::Boolean.new.cast(0)
=> false
> ActiveModel::Type::Boolean.new.cast(1)
=> true
> ActiveModel::Type::Boolean.new.cast(true)
=> true
> ActiveModel::Type::Boolean.new.cast(false)
=> false
```

This cast method gives you a handy way to handle all thsoe different cases. This is available as of Rails 5+.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/cast-common-boolean-like-values-to-booleans.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
