{"id":13747373,"url":"https://github.com/prodis/wannabe_bool","last_synced_at":"2025-04-04T16:12:15.937Z","repository":{"id":22339251,"uuid":"25674924","full_name":"prodis/wannabe_bool","owner":"prodis","description":"If string, numeric, symbol and nil values wanna be a boolean value, they can with the new #to_b method (and more).","archived":false,"fork":false,"pushed_at":"2020-11-11T16:47:12.000Z","size":66,"stargazers_count":162,"open_issues_count":5,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-28T15:04:28.789Z","etag":null,"topics":["boolean","conversion","predicate-methods","prodis","ruby","wannabe-bool"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prodis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-24T06:33:32.000Z","updated_at":"2025-03-14T19:09:05.000Z","dependencies_parsed_at":"2022-08-07T10:15:30.672Z","dependency_job_id":null,"html_url":"https://github.com/prodis/wannabe_bool","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodis%2Fwannabe_bool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodis%2Fwannabe_bool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodis%2Fwannabe_bool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodis%2Fwannabe_bool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prodis","download_url":"https://codeload.github.com/prodis/wannabe_bool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208142,"owners_count":20901570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boolean","conversion","predicate-methods","prodis","ruby","wannabe-bool"],"created_at":"2024-08-03T06:01:26.722Z","updated_at":"2025-04-04T16:12:15.916Z","avatar_url":"https://github.com/prodis.png","language":"Ruby","readme":"# Wannabe Bool\n\nIf **string**, **numeric**, **symbol** and **nil** values wanna be a **boolean** value, they can with the new `to_b` method.\nMoreover, you can use `WannabeBool::Attributes` module to create predicate methods in your classes.\n\n[![Gem Version](https://badge.fury.io/rb/wannabe_bool.svg)](http://badge.fury.io/rb/wannabe_bool)\n[![Build Status](https://travis-ci.org/prodis/wannabe_bool.svg?branch=master)](https://travis-ci.org/prodis/wannabe_bool)\n[![Coverage Status](https://coveralls.io/repos/prodis/wannabe_bool/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/prodis/wannabe_bool?branch=master)\n[![Code Climate](https://codeclimate.com/github/prodis/wannabe_bool/badges/gpa.svg)](https://codeclimate.com/github/prodis/wannabe_bool)\n[![Dependency Status](https://gemnasium.com/prodis/wannabe_bool.svg)](https://gemnasium.com/prodis/wannabe_bool)\n[![GitHub license](https://img.shields.io/apm/l/vim-mode.svg)](LICENSE)\n\n\n## Installing\n\n### Gemfile\n```ruby\ngem 'wannabe_bool'\n```\n\n### Direct installation\n```\n$ gem install wannabe_bool\n```\n\n## Using\n\n`to_b` method is available on `String`, `Symbol`, `Numeric`, `TrueClass`, `FalseClass` and `NilClass`.\n\nFor sake of readability (and personal choice), `to_b` has two aliases:\n- `to_bool`\n- `to_boolean`\n\nGiven this example:\n```ruby\n{\n  one: 'value',\n  two: 2,\n  mobile?: params[:mobile].to_b\n}\n```\n\nIt could be \"more readable\" like this:\n```ruby\n{\n  one: 'value',\n  two: 2,\n  mobile?: params[:mobile].to_boolean\n}\n```\n\nDon't forget to require the gem:\n```ruby\nrequire 'wannabe_bool'\n```\n\n### String\n* Returns `true` if string is one of these values: **t**, **true**, **on**, **y**, **yes**, **1**.\n* Returns `false` if string is one of these values: **f**, **false**, **off**, **n**, **no**, **0**.\n* For invalid boolean string representations, returns `false` by default. See \"Invalid Value Behaviour\" section for more options.\n\nIt ignores trailing spaces and letter cases.\n\n```ruby\n'1'.to_b    # =\u003e true\n't'.to_b    # =\u003e true\n'T'.to_b    # =\u003e true\n'true'.to_b # =\u003e true\n'TRUE'.to_b # =\u003e true\n'on'.to_b   # =\u003e true\n'ON'.to_b   # =\u003e true\n'y'.to_b    # =\u003e true\n'yes'.to_b  # =\u003e true\n'YES'.to_b  # =\u003e true\n\n' 1 '.to_b    # =\u003e true\n' t '.to_b    # =\u003e true\n' T '.to_b    # =\u003e true\n' true '.to_b # =\u003e true\n' TRUE '.to_b # =\u003e true\n' on '.to_b   # =\u003e true\n' ON '.to_b   # =\u003e true\n' y '.to_b    # =\u003e true\n'Y'.to_b      # =\u003e true\n' Y '.to_b    # =\u003e true\n' yes '.to_b  # =\u003e true\n' YES '.to_b  # =\u003e true\n\n'0'.to_b     # =\u003e false\n'f'.to_b     # =\u003e false\n'F'.to_b     # =\u003e false\n'false'.to_b # =\u003e false\n'FALSE'.to_b # =\u003e false\n'off'.to_b   # =\u003e false\n'OFF'.to_b   # =\u003e false\n'n'.to_b     # =\u003e false\n'N'.to_b     # =\u003e false\n'no'.to_b    # =\u003e false\n'NO'.to_b    # =\u003e false\n\n' 0 '.to_b     # =\u003e false\n' f '.to_b     # =\u003e false\n' F '.to_b     # =\u003e false\n' false '.to_b # =\u003e false\n' FALSE '.to_b # =\u003e false\n' off '.to_b   # =\u003e false\n' OFF '.to_b   # =\u003e false\n' n '.to_b     # =\u003e false\n' N '.to_b     # =\u003e false\n' no '.to_b    # =\u003e false\n' NO '.to_b    # =\u003e false\n\n''.to_b  # =\u003e false\n' '.to_b # =\u003e false\n```\n#### Invalid Value Behaviour for strings\nYou can configure the result for invalid boolean string representations, using the `WannabeBool.invalid_value_behaviour` option.\n\nThere are 3 predefined behaviours available: to return `false` (default), `nil` or raise an `ArgumentError`:\n\n```ruby\nWannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::False\n'wherever'.to_b # =\u003e false\n\nWannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Nil\n'wherever'.to_b # =\u003e nil\n\nWannabeBool.invalid_value_behaviour = WannabeBool::InvalidValueBehaviour::Error\n'wherever'.to_b # =\u003e ArgumentError: is not a valid boolean representation\n```\n\nMoreover, you can provide your own behaviour for invalid boolean string representations. Just set a proc or lambda, or even any class or object that responds to `call` method.\n\n```ruby\nWannabeBool.invalid_value_behaviour = -\u003e { :prodis }\n'wherever'.to_b # =\u003e :prodis\n```\n\nNote that `WannabeBool.invalid_value_behaviour` is a global configuration. Said that, all the results for `to_b` method with invalid boolean string representations will be affected.\n\n### Symbol\nSame as `symbol.to_s.to_b`.\n\n```ruby\n:'1'.to_b  # =\u003e true\n:t.to_b    # =\u003e true\n:true.to_b # =\u003e true\n:on.to_b   # =\u003e true\n:y.to_b    # =\u003e true\n:yes.to_b  # =\u003e true\n\n:'0'.to_b   # =\u003e false\n:f.to_b     # =\u003e false\n:false.to_b # =\u003e false\n:off.to_b   # =\u003e false\n:n.to_b     # =\u003e false\n:no.to_b    # =\u003e false\n```\n\n### Numeric\nReturns `false` if number is zero. Returns `true` otherwise.\n\n#### Integer\n```ruby\n0.to_b  # =\u003e false\n1.to_b  # =\u003e true\n2.to_b  # =\u003e true\n-1.to_b # =\u003e true\n-2.to_b # =\u003e true\n```\n\n#### Float\n```ruby\n0.0.to_b  # =\u003e false\n0.1.to_b  # =\u003e true\n1.0.to_b  # =\u003e true\n-0.1.to_b # =\u003e true\n-1.0.to_b # =\u003e true\n```\n\n#### BigDecimal\n```ruby\nrequire 'bigdecimal'\n\nBigDecimal('0.0').to_b  # =\u003e false\nBigDecimal('0.1').to_b  # =\u003e true\nBigDecimal('1.0').to_b  # =\u003e true\nBigDecimal('-0.1').to_b # =\u003e true\nBigDecimal('-1.0').to_b # =\u003e true\n```\n\n### TrueClass\nReturns `true`.\n\n```ruby\ntrue.to_b # =\u003e true\n```\n\n### FalseClass\nReturns `false`.\n\n```ruby\nfalse.to_b # =\u003e false\n```\n\n### NilClass\nReturns `false`.\n\n```ruby\nnil.to_b # =\u003e false\n```\n\n## Creating predicate methods\n\n```ruby\nclass Fake\n  include WannabeBool::Attributes\n\n  attr_accessor :name, :main, :published\n  attr_wannabe_bool :main, :published\nend\n\nfake = Fake.new\nfake.main?      # =\u003e false\nfake.published? # =\u003e false\n\nfake.main = true\nfake.main? # =\u003e true\n\nfake.published = 1\nfake.published? # =\u003e true\n\nfake.main = 'true'\nfake.main? # =\u003e true\n\nfake.published = :true\nfake.published? # =\u003e true\n```\n\n## Changelog\n\n[See the changes in each version.](CHANGELOG.md)\n\n## Author\n[Fernando Hamasaki de Amorim (prodis)](http://prodis.blog.br)\n\n![Prodis Logo](http://prodis.net.br/images/prodis_150.gif)\n\n## Contributing to wannabe_bool\n\n[See the contributing guide.](CONTRIBUTING.md)\n\n","funding_links":[],"categories":["Ruby","Gems"],"sub_categories":["Misc"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodis%2Fwannabe_bool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprodis%2Fwannabe_bool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodis%2Fwannabe_bool/lists"}