Skip to content

Commit

Permalink
Add zero? to Number, Time::Span, and Complex (#4026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Smith authored and bcardiff committed Jun 23, 2017
1 parent c0b2e33 commit a431a57
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/std/complex_spec.cr
Expand Up @@ -168,4 +168,11 @@ describe "Complex" do
c = Complex.new(4, 6.2)
c.clone.should eq(c)
end

it "test zero?" do
Complex.new(0, 0).zero?.should eq true
Complex.new(0, 3.4).zero?.should eq false
Complex.new(1.2, 0).zero?.should eq false
Complex.new(1.2, 3.4).zero?.should eq false
end
end
9 changes: 9 additions & 0 deletions spec/std/number_spec.cr
Expand Up @@ -166,6 +166,15 @@ describe "Number" do
ary[2].should eq(300.to_u8)
end

it "test zero?" do
0.zero?.should eq true
0.0.zero?.should eq true
0f32.zero?.should eq true
1.zero?.should eq false
1.0.zero?.should eq false
1f32.zero?.should eq false
end

describe "step" do
it "from int to float" do
count = 0
Expand Down
5 changes: 5 additions & 0 deletions spec/std/time/span_spec.cr
Expand Up @@ -240,4 +240,9 @@ describe Time::Span do
it "should sum" do
[1.second, 5.seconds].sum.should eq(6.seconds)
end

it "test zero?" do
Time::Span.new(0).zero?.should eq true
Time::Span.new(123456789).zero?.should eq false
end
end
4 changes: 4 additions & 0 deletions src/complex.cr
Expand Up @@ -223,6 +223,10 @@ struct Complex
def self.zero : Complex
new 0, 0
end

def zero? : Bool
@real == 0 && @imag == 0
end
end

struct Number
Expand Down
10 changes: 10 additions & 0 deletions src/number.cr
Expand Up @@ -245,6 +245,16 @@ struct Number
self
end

# Returns `true` if value is equal to zero.
#
# ```
# 0.zero? # => true
# 5.zero? # => false
# ```
def zero? : Bool
self == 0
end

private class StepIterator(T, L, B)
include Iterator(T)

Expand Down
4 changes: 4 additions & 0 deletions src/time/span.cr
Expand Up @@ -286,6 +286,10 @@ struct Time::Span
def self.zero
new(0)
end

def zero?
@ticks == 0
end
end

struct Int
Expand Down

0 comments on commit a431a57

Please sign in to comment.