GithubHelp home page GithubHelp logo

sam0x17 / truthy Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 14 KB

Adds intelligent to_b (to_boolen) to all objects in crystal

License: MIT License

Crystal 100.00%
boolean crystal-language crystal shard crystal-shard crystal-shards truthy

truthy's Introduction

truthy

Truthy patches all objects in crystal with a to_b (to boolean) method that will intelligently return a boolean based on the value of the object. This will differ from !!(expr) in a number of cases including the handling of "" and '\0' as false, and proper handling of class instances and arrays (an empty array => false, an array with elements => true).

expr.to_b will always return true or false no matter what you pass to it. !!(expr) is used as a fallback for any language features that are not explictly covered.

An alias is provided so that you can also use *.truthy? if you think this makes more sense than to_b. Both methods function in exactly the same way and are interchangable.

Installation

Add this to your application's shard.yml:

dependencies:
  truthy:
    github: sam0x17/truthy

Usage

require "truthy"

# Booleans
true.to_b # => true
false.to_b # => false

# Objects
nil.to_b # => false
SomeClass.new.to_b # => true

# Strings / Characters
"".to_b # => false
"hey".to_b # => true
'\0'.to_b # => false
'c'.to_b # => true
"0".to_b # => false
"1".to_b # => true
"200".to_b # => true
"1.0".to_b # => true
"-1.0".to_b # => false
"true".to_b # => true
"false".to_b # => false
"yes".to_b # => true
"no".to_b # => false
"nil".to_b # => false
"null".to_b # => false
"none".to_b # => false
"True".to_b # => true
"[]".to_b # => false
"{}".to_b # => false


# Integers
0.to_b # => false
-1.to_b # => false
1.to_b # => true
10.to_b # => true
0_i64.to_b # => false
1_i8.to_b # => true
-1_i16.to_b # => false
0_u64.to_b # => false
3_u16.to_b # => true

# Floats
0.0.to_b # => false
1.0.to_b # => true
0.001.to_b # => true
-0.001.to_b # => false
-1.0.to_b # => false
23.342.to_b # => true

# Arrays
[1, 2, 3].to_b # => true
([] of Int32).to_b # => false

# truthy? alias
true.truthy? # => true
false.truthy? # => false
0.truthy? # => false
1.truthy? # => true

truthy's People

Contributors

sam0x17 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.