Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zero? to Number, Time::Span, and Complex #4026

Merged
merged 5 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spec/std/complex_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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