Skip to content

Commit 56b8edd

Browse files
woodruffwRX14
authored andcommittedJan 2, 2018
Bool: Add to_unsafe (#5465)
`Bool.to_unsafe` returns `1` for `true` and `0` for false.
1 parent 68a7ce8 commit 56b8edd

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
 

‎spec/std/bool_spec.cr

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe "Bool" do
3131
it { true.hash.should_not eq(false.hash) }
3232
end
3333

34+
describe "to_unsafe" do
35+
it { true.to_unsafe.should eq(1) }
36+
it { false.to_unsafe.should eq(0) }
37+
end
38+
3439
describe "to_s" do
3540
it { true.to_s.should eq("true") }
3641
it { false.to_s.should eq("false") }

‎src/bool.cr

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ struct Bool
4646
hasher.bool(self)
4747
end
4848

49+
# Returns `1` for `true` and `0` for `false`.
50+
def to_unsafe
51+
self ? 1 : 0
52+
end
53+
4954
# Returns `"true"` for `true` and `"false"` for `false`.
5055
def to_s
5156
self ? "true" : "false"

0 commit comments

Comments
 (0)
Please sign in to comment.