Skip to content

Commit

Permalink
Bool: Add to_unsafe (#5465)
Browse files Browse the repository at this point in the history
`Bool.to_unsafe` returns `1` for `true` and `0` for false.
  • Loading branch information
woodruffw authored and RX14 committed Jan 2, 2018
1 parent 68a7ce8 commit 56b8edd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spec/std/bool_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ describe "Bool" do
it { true.hash.should_not eq(false.hash) }
end

describe "to_unsafe" do
it { true.to_unsafe.should eq(1) }
it { false.to_unsafe.should eq(0) }
end

describe "to_s" do
it { true.to_s.should eq("true") }
it { false.to_s.should eq("false") }
Expand Down
5 changes: 5 additions & 0 deletions src/bool.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ struct Bool
hasher.bool(self)
end

# Returns `1` for `true` and `0` for `false`.
def to_unsafe
self ? 1 : 0
end

# Returns `"true"` for `true` and `"false"` for `false`.
def to_s
self ? "true" : "false"
Expand Down

0 comments on commit 56b8edd

Please sign in to comment.