diff --git a/src/class.cr b/src/class.cr index 1ec95b97dd1f..0128e90be204 100644 --- a/src/class.cr +++ b/src/class.cr @@ -54,7 +54,7 @@ class Class # Number > Number # => false # Number > Object # => false # ``` - def >(other : T.class) forall T + def >(other : T.class) : Bool forall T # This is so that the method is expanded differently for each type {% @type %} other._lt(self) @@ -141,7 +141,7 @@ class Class # Int32.nilable? # => false # Nil.nilable? # => true # ``` - def nilable? + def nilable? : Bool self == ::Nil end diff --git a/src/colorize.cr b/src/colorize.cr index 792faa55e229..1523e8699ecc 100644 --- a/src/colorize.cr +++ b/src/colorize.cr @@ -145,13 +145,13 @@ module Colorize # io << "green and bold if Colorize.enabled" # end # ``` - def self.with + def self.with : Colorize::Object(String) "".colorize end end module Colorize::ObjectExtensions - def colorize + def colorize : Colorize::Object Colorize::Object.new(self) end @@ -278,7 +278,7 @@ struct Colorize::Object(T) end {% end %} - def fore(color : Symbol) + def fore(color : Symbol) : self {% for name in COLORS %} if color == :{{name.id}} @fore = ColorANSI::{{name.camelcase.id}} @@ -289,11 +289,11 @@ struct Colorize::Object(T) raise ArgumentError.new "Unknown color: #{color}" end - def fore(@fore : Color) + def fore(@fore : Color) : self self end - def back(color : Symbol) + def back(color : Symbol) : self {% for name in COLORS %} if color == :{{name.id}} @back = ColorANSI::{{name.camelcase.id}} @@ -304,11 +304,11 @@ struct Colorize::Object(T) raise ArgumentError.new "Unknown color: #{color}" end - def back(@back : Color) + def back(@back : Color) : self self end - def mode(mode : Symbol) + def mode(mode : Symbol) : self {% for name in MODES %} if mode == :{{name.id}} @mode |= MODE_{{name.upcase.id}}_FLAG diff --git a/src/comparable.cr b/src/comparable.cr index 8ea7c057dcad..7f7e0cc7fb8d 100644 --- a/src/comparable.cr +++ b/src/comparable.cr @@ -21,7 +21,7 @@ module Comparable(T) # Compares this object to *other* based on the receiver’s `<=>` method, # returning `true` if it returns a negative number. - def <(other : T) + def <(other : T) : Bool cmp = self <=> other cmp ? cmp < 0 : false end @@ -52,7 +52,7 @@ module Comparable(T) # Compares this object to *other* based on the receiver’s `<=>` method, # returning `true` if it returns a value greater then `0`. - def >(other : T) + def >(other : T) : Bool cmp = self <=> other cmp ? cmp > 0 : false end diff --git a/src/csv.cr b/src/csv.cr index adc099e9bc67..1b8e89e07a1f 100644 --- a/src/csv.cr +++ b/src/csv.cr @@ -218,7 +218,7 @@ class CSV # Advanced the cursor to the next row. Must be called once to position # the cursor in the first row. Returns `true` if a next row was found, # `false` otherwise. - def next + def next : Bool return false if @traversed row = @row ||= [] of String @@ -318,7 +318,7 @@ class CSV end # :nodoc: - def indices + def indices : Hash(String, Int32) @indices || raise(Error.new("Headers not requested")) end @@ -326,7 +326,7 @@ class CSV getter? strip # :nodoc: - def headers? + def headers? : Array(String)? @headers end @@ -405,7 +405,7 @@ class CSV # Returns the number of columns in this row, regardless of the number # of headers (if requested). - def size + def size : Int32 @row.size end diff --git a/src/csv/lexer.cr b/src/csv/lexer.cr index df1a514f3138..2656d53656b1 100644 --- a/src/csv/lexer.cr +++ b/src/csv/lexer.cr @@ -50,7 +50,7 @@ abstract class CSV::Lexer private abstract def current_char # Returns the next `Token` in this CSV. - def next_token + def next_token : CSV::Token if @last_empty_column @last_empty_column = false @token.kind = Token::Kind::Cell diff --git a/src/ecr/lexer.cr b/src/ecr/lexer.cr index acb5740b1260..b5a30cae8e84 100644 --- a/src/ecr/lexer.cr +++ b/src/ecr/lexer.cr @@ -32,7 +32,7 @@ class ECR::Lexer @column_number = 1 end - def next_token + def next_token : Token copy_location_info_to_token case current_char diff --git a/src/ecr/processor.cr b/src/ecr/processor.cr index 7806c53417b2..786879e94273 100644 --- a/src/ecr/processor.cr +++ b/src/ecr/processor.cr @@ -6,12 +6,12 @@ module ECR DefaultBufferName = "__str__" # :nodoc: - def process_file(filename, buffer_name = DefaultBufferName) + def process_file(filename, buffer_name = DefaultBufferName) : String process_string File.read(filename), filename, buffer_name end # :nodoc: - def process_string(string, filename, buffer_name = DefaultBufferName) + def process_string(string, filename, buffer_name = DefaultBufferName) : String lexer = Lexer.new string token = lexer.next_token diff --git a/src/enum.cr b/src/enum.cr index 726cc8acd85c..39eeb79d533f 100644 --- a/src/enum.cr +++ b/src/enum.cr @@ -185,7 +185,7 @@ struct Enum # Color::Red + 2 # => Color::Blue # Color::Red + 3 # => Color.new(3) # ``` - def +(other : Int) + def +(other : Int) : self self.class.new(value + other) end @@ -197,7 +197,7 @@ struct Enum # Color::Blue - 2 # => Color::Red # Color::Blue - 3 # => Color.new(-1) # ``` - def -(other : Int) + def -(other : Int) : self self.class.new(value - other) end @@ -208,7 +208,7 @@ struct Enum # ``` # (IOMode::Read | IOMode::Async) # => IOMode::Read | IOMode::Async # ``` - def |(other : self) + def |(other : self) : self self.class.new(value | other.value) end @@ -219,20 +219,20 @@ struct Enum # ``` # (IOMode::Read | IOMode::Async) & IOMode::Read # => IOMode::Read # ``` - def &(other : self) + def &(other : self) : self self.class.new(value & other.value) end # Returns the enum member that results from applying a logical # "xor" operation between this enum member's value and *other*. # This is mostly useful with flag enums. - def ^(other : self) + def ^(other : self) : self self.class.new(value ^ other.value) end # Returns the enum member that results from applying a logical # "not" operation of this enum member's value. - def ~ + def ~ : self self.class.new(~value) end @@ -276,7 +276,7 @@ struct Enum # mode.includes?(IOMode::Read) # => true # mode.includes?(IOMode::Async) # => false # ``` - def includes?(other : self) + def includes?(other : self) : Bool (value & other.value) != 0 end diff --git a/src/float/printer/cached_powers.cr b/src/float/printer/cached_powers.cr index 7a00dcac019c..19cc1f09457c 100644 --- a/src/float/printer/cached_powers.cr +++ b/src/float/printer/cached_powers.cr @@ -140,7 +140,7 @@ module Float::Printer::CachedPowers Pow10Cache = {0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000} - def self.largest_pow10(n, n_bits) + def self.largest_pow10(n, n_bits) : {Int32, Int32} # 1233/4096 is approximately 1/lg(10). # We increment to skip over the first entry in the powers cache. guess = ((n_bits + 1) * 1233 >> 12) + 1 diff --git a/src/float/printer/diy_fp.cr b/src/float/printer/diy_fp.cr index 9b046d76b791..c41bc41d9a20 100644 --- a/src/float/printer/diy_fp.cr +++ b/src/float/printer/diy_fp.cr @@ -62,7 +62,7 @@ struct Float::Printer::DiyFP # must be greater than the *other*. # # NOTE: This result is not normalized. - def -(other : DiyFP) + def -(other : DiyFP) : self self.class.new(frac - other.frac, exp) end @@ -76,7 +76,7 @@ struct Float::Printer::DiyFP # bits. # # NOTE: This result is not normalized. - def *(other : DiyFP) + def *(other : DiyFP) : self a = frac >> 32 b = frac & MASK32 c = other.frac >> 32 @@ -95,7 +95,7 @@ struct Float::Printer::DiyFP self.class.new(f, e) end - def normalize + def normalize : DiyFP f = frac e = exp @@ -118,13 +118,13 @@ struct Float::Printer::DiyFP DiyFP.new(f, e) end - def self.from_f(d : Float64 | Float32) + def self.from_f(d : Float64 | Float32) : self frac, exp = IEEE.frac_and_exp(d) new(frac, exp) end # Normalize such that the most significant bit of `frac` is set. - def self.from_f_normalized(v : Float64 | Float32) + def self.from_f_normalized(v : Float64 | Float32) : self pre_normalized = from_f(v) f = pre_normalized.frac e = pre_normalized.exp diff --git a/src/float/printer/grisu3.cr b/src/float/printer/grisu3.cr index d4eab572129f..cc01a0df7ab1 100644 --- a/src/float/printer/grisu3.cr +++ b/src/float/printer/grisu3.cr @@ -53,7 +53,7 @@ module Float::Printer::Grisu3 # representable number to the input. # # Modifies the generated digits in the buffer to approach (round towards) *w*. - def round_weed(buffer_ptr, length, distance_too_high_w, unsafe_interval, rest, ten_kappa, unit) + def round_weed(buffer_ptr, length, distance_too_high_w, unsafe_interval, rest, ten_kappa, unit) : Bool buffer = buffer_ptr.to_slice(128) small_distance = distance_too_high_w - unit big_distance = distance_too_high_w + unit diff --git a/src/float/printer/ieee.cr b/src/float/printer/ieee.cr index cfe7bb15e454..db408d3e7913 100644 --- a/src/float/printer/ieee.cr +++ b/src/float/printer/ieee.cr @@ -50,35 +50,35 @@ module Float::Printer::IEEE DENORMAL_EXPONENT_32 = -EXPONENT_BIAS_32 + 1 SIGN_MASK_32 = 0x80000000_u32 - def to_uint(v : Float64) + def to_uint(v : Float64) : UInt64 v.unsafe_as(UInt64) end - def to_uint(v : Float32) + def to_uint(v : Float32) : UInt32 v.unsafe_as(UInt32) end - def sign(d64 : UInt64) + def sign(d64 : UInt64) : Int32 (d64 & SIGN_MASK_64) == 0 ? 1 : -1 end - def sign(d32 : UInt32) + def sign(d32 : UInt32) : Int32 (d32 & SIGN_MASK_32) == 0 ? 1 : -1 end - def special?(d64 : UInt64) + def special?(d64 : UInt64) : Bool (d64 & EXPONENT_MASK_64) == EXPONENT_MASK_64 end - def special?(d32 : UInt32) + def special?(d32 : UInt32) : Bool (d32 & EXPONENT_MASK_32) == EXPONENT_MASK_32 end - def inf?(d64 : UInt64) + def inf?(d64 : UInt64) : Bool special?(d64) && (d64 & SIGNIFICAND_MASK_64 == 0) end - def inf?(d32 : UInt32) + def inf?(d32 : UInt32) : Bool special?(d32) && (d32 & SIGNIFICAND_MASK_32 == 0) end @@ -96,7 +96,7 @@ module Float::Printer::IEEE # exponent as *m_plus*. # # Precondition: the value encoded by this `Float` must be greater than 0. - def normalized_boundaries(v : Float64) + def normalized_boundaries(v : Float64) : {minus: DiyFP, plus: DiyFP} w = DiyFP.from_f(v) m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize @@ -124,7 +124,7 @@ module Float::Printer::IEEE return {minus: m_minus, plus: m_plus} end - def normalized_boundaries(v : Float32) + def normalized_boundaries(v : Float32) : {minus: DiyFP, plus: DiyFP} w = DiyFP.from_f(v) m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize @@ -144,7 +144,7 @@ module Float::Printer::IEEE return {minus: m_minus, plus: m_plus} end - def frac_and_exp(v : Float64) + def frac_and_exp(v : Float64) : {UInt64, Int32} d64 = to_uint(v) if (d64 & EXPONENT_MASK_64) == 0 # denormal float @@ -158,7 +158,7 @@ module Float::Printer::IEEE {frac, exp} end - def frac_and_exp(v : Float32) + def frac_and_exp(v : Float32) : {UInt64, Int32} d32 = to_uint(v) if (d32 & EXPONENT_MASK_32) == 0 # denormal float diff --git a/src/humanize.cr b/src/humanize.cr index 9f2d87640400..85c6adc51c25 100644 --- a/src/humanize.cr +++ b/src/humanize.cr @@ -82,7 +82,7 @@ struct Number end # :nodoc: - def self.prefix_index(i, group = 3) + def self.prefix_index(i, group = 3) : Int32 ((i - (i > 0 ? 1 : 0)) // group) * group end diff --git a/src/int.cr b/src/int.cr index 3e3781370fae..66f4438b6953 100644 --- a/src/int.cr +++ b/src/int.cr @@ -401,7 +401,7 @@ struct Int # 0b1101.bits_set?(0b0111) # => false # 0b1101.bits_set?(0b1100) # => true # ``` - def bits_set?(mask) + def bits_set?(mask) : Bool (self & mask) == mask end diff --git a/src/nil.cr b/src/nil.cr index eb3ec05179b6..1f2d8a8378fa 100644 --- a/src/nil.cr +++ b/src/nil.cr @@ -63,7 +63,7 @@ struct Nil end # Returns `false`. - def same?(other : Reference) + def same?(other : Reference) : Bool false end @@ -102,7 +102,7 @@ struct Nil # Raises `NilAssertionError`. # # See also: `Object#not_nil!`. - def not_nil! + def not_nil! : NoReturn raise NilAssertionError.new end diff --git a/src/range.cr b/src/range.cr index 4644e8fb6593..1f9d59303676 100644 --- a/src/range.cr +++ b/src/range.cr @@ -275,7 +275,7 @@ struct Range(B, E) # (1..10).excludes_end? # => false # (1...10).excludes_end? # => true # ``` - def excludes_end? + def excludes_end? : Bool @exclusive end @@ -289,7 +289,7 @@ struct Range(B, E) # (1...10).includes?(9) # => true # (1...10).includes?(10) # => false # ``` - def includes?(value) + def includes?(value) : Bool begin_value = @begin end_value = @end diff --git a/src/reference.cr b/src/reference.cr index efaa818432e4..f57923e107dd 100644 --- a/src/reference.cr +++ b/src/reference.cr @@ -25,7 +25,7 @@ class Reference # Returns `true` if this reference is the same as *other*. This is only # `true` if this reference's `object_id` is the same as *other*'s. - def same?(other : Reference) + def same?(other : Reference) : Bool object_id == other.object_id end diff --git a/src/time.cr b/src/time.cr index b36d8a4cef1e..2e672042178a 100644 --- a/src/time.cr +++ b/src/time.cr @@ -549,7 +549,7 @@ struct Time # tokyo.inspect # => "2019-01-01 00:00:00.0 +09:00 Asia/Tokyo" # new_york.inspect # => "2019-01-01 00:00:00.0 -05:00 America/New_York" # ``` - def to_local_in(location : Location) + def to_local_in(location : Location) : Time local_seconds = offset_seconds local_seconds -= Time.zone_offset_at(local_seconds, location) @@ -1118,7 +1118,7 @@ struct Time end # Parse time format specified by [RFC 3339](https://tools.ietf.org/html/rfc3339) ([ISO 8601](http://xml.coverpages.org/ISO-FDIS-8601.pdf) profile). - def self.parse_rfc3339(time : String) + def self.parse_rfc3339(time : String) : self Format::RFC_3339.parse(time) end @@ -1139,7 +1139,7 @@ struct Time # ``` # # This is also compatible to [RFC 882](https://tools.ietf.org/html/rfc882) and [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55). - def to_rfc2822 + def to_rfc2822 : String Format::RFC_2822.format(to_utc) end @@ -1154,7 +1154,7 @@ struct Time # Parse time format specified by [RFC 2822](https://www.ietf.org/rfc/rfc2822.txt). # # This is also compatible to [RFC 882](https://tools.ietf.org/html/rfc882) and [RFC 1123](https://tools.ietf.org/html/rfc1123#page-55). - def self.parse_rfc2822(time : String) + def self.parse_rfc2822(time : String) : self Format::RFC_2822.parse(time) end @@ -1393,12 +1393,12 @@ struct Time def_at_end(hour) { Time.local(year, month, day, hour, 59, 59, nanosecond: 999_999_999, location: location) } # Returns a copy of this `Time` representing the end of the minute. - def at_end_of_minute + def at_end_of_minute : Time Time.new(seconds: total_seconds - second + 59, nanoseconds: 999_999_999, location: location) end # Returns a copy of this `Time` representing the end of the second. - def at_end_of_second + def at_end_of_second : Time Time.new(seconds: total_seconds, nanoseconds: 999_999_999, location: location) end diff --git a/src/time/format/custom/http_date.cr b/src/time/format/custom/http_date.cr index 5482d4285e7c..70ca4dba7193 100644 --- a/src/time/format/custom/http_date.cr +++ b/src/time/format/custom/http_date.cr @@ -37,7 +37,7 @@ struct Time::Format # Formats a `Time` into a `String`. # # *time* is always converted to UTC. - def self.format(time : Time) + def self.format(time : Time) : String String.build do |io| format(time, io) end @@ -45,7 +45,7 @@ struct Time::Format end struct Parser - def http_date + def http_date : Time::Location ansi_c_format = http_date_short_day_name_with_comma? if ansi_c_format @@ -74,7 +74,7 @@ struct Time::Format time_zone_gmt_or_rfc2822 end - def http_date_ansi_c + def http_date_ansi_c : Time::Location short_month_name whitespace day_of_month_blank_padded @@ -94,7 +94,7 @@ struct Time::Format !ansi_c_format && current_char.ascii_whitespace? end - def http_date_short_day_name_with_comma? + def http_date_short_day_name_with_comma? : Bool? return unless current_char.ascii_letter? short_day_name diff --git a/src/time/format/custom/iso_8601.cr b/src/time/format/custom/iso_8601.cr index c015fd748b48..6d1331e2ee08 100644 --- a/src/time/format/custom/iso_8601.cr +++ b/src/time/format/custom/iso_8601.cr @@ -132,7 +132,7 @@ struct Time::Format end # Formats a `Time` into a `String`. - def self.format(time : Time) + def self.format(time : Time) : String String.build do |io| format(time, io) end @@ -156,7 +156,7 @@ struct Time::Format end # Formats a `Time` into a `String`. - def self.format(time : Time) + def self.format(time : Time) : String String.build do |io| format(time, io) end diff --git a/src/time/format/custom/rfc_2822.cr b/src/time/format/custom/rfc_2822.cr index 24dc2ca26267..e2d81642f744 100644 --- a/src/time/format/custom/rfc_2822.cr +++ b/src/time/format/custom/rfc_2822.cr @@ -18,7 +18,7 @@ struct Time::Format end # Formats a `Time` into a `String`. - def self.format(time : Time) + def self.format(time : Time) : String String.build do |io| format(time, io) end diff --git a/src/time/format/custom/rfc_3339.cr b/src/time/format/custom/rfc_3339.cr index 7c16669aab9a..cacde53b2da6 100644 --- a/src/time/format/custom/rfc_3339.cr +++ b/src/time/format/custom/rfc_3339.cr @@ -16,7 +16,7 @@ struct Time::Format end # Formats a `Time` into a `String`. - def self.format(time : Time, fraction_digits = 0) + def self.format(time : Time, fraction_digits = 0) : String String.build do |io| format(time, io, fraction_digits: fraction_digits) end diff --git a/src/time/format/custom/yaml_date.cr b/src/time/format/custom/yaml_date.cr index 9ef4808a7a1d..636778e5ea1f 100644 --- a/src/time/format/custom/yaml_date.cr +++ b/src/time/format/custom/yaml_date.cr @@ -32,7 +32,7 @@ struct Time::Format end # Formats a `Time` into a `String`. - def self.format(time : Time) + def self.format(time : Time) : String String.build do |io| format(time, io) end diff --git a/src/time/format/parser.cr b/src/time/format/parser.cr index b29e13bc96c5..8e7d6ae400a5 100644 --- a/src/time/format/parser.cr +++ b/src/time/format/parser.cr @@ -37,7 +37,7 @@ struct Time::Format @nanosecond_offset = 0_i64 end - def time(location : Location? = nil) + def time(location : Location? = nil) : Time if @hour_is_12 if @hour > 12 raise ArgumentError.new("Invalid hour for 12-hour clock") diff --git a/src/time/location.cr b/src/time/location.cr index 8a0f559a00f3..7e8c2ee8671d 100644 --- a/src/time/location.cr +++ b/src/time/location.cr @@ -223,7 +223,7 @@ class Time::Location # Creates a `Location` instance with fixed *offset* in seconds from UTC. # # The formatted *offset* is used as name. - def self.fixed(offset : Int32) + def self.fixed(offset : Int32) : self zone = Zone.new(nil, offset, false) new zone.name, [zone] end diff --git a/src/time/location/loader.cr b/src/time/location/loader.cr index d25789ce3305..fc780927be6d 100644 --- a/src/time/location/loader.cr +++ b/src/time/location/loader.cr @@ -12,21 +12,21 @@ class Time::Location end # :nodoc: - def self.load?(name : String, sources : Enumerable(String)) + def self.load?(name : String, sources : Enumerable(String)) : Time::Location? if source = find_zoneinfo_file(name, sources) load_from_dir_or_zip(name, source) end end # :nodoc: - def self.load(name : String, sources : Enumerable(String)) + def self.load(name : String, sources : Enumerable(String)) : Time::Location? if source = find_zoneinfo_file(name, sources) load_from_dir_or_zip(name, source) || raise InvalidLocationNameError.new(name, source) end end # :nodoc: - def self.load_from_dir_or_zip(name : String, source : String) + def self.load_from_dir_or_zip(name : String, source : String) : Time::Location? if source.ends_with?(".zip") open_file_cached(name, source) do |file| read_zip_file(name, file) do |io| @@ -60,7 +60,7 @@ class Time::Location end # :nodoc: - def self.find_zoneinfo_file(name : String, sources : Enumerable(String)) + def self.find_zoneinfo_file(name : String, sources : Enumerable(String)) : String? sources.each do |source| if source.ends_with?(".zip") path = source @@ -77,7 +77,7 @@ class Time::Location # See https://data.iana.org/time-zones/tz-link.html, https://github.com/eggert/tz, tzfile(5) # :nodoc: - def self.read_zoneinfo(location_name : String, io : IO) + def self.read_zoneinfo(location_name : String, io : IO) : Time::Location raise InvalidTZDataError.new unless io.read_string(4) == "TZif" # 1-byte version, then 15 bytes of padding