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

fix example codes (2019-03) #7569

Merged
merged 15 commits into from
Mar 25, 2019
6 changes: 3 additions & 3 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ struct BigDecimal < Number
# defines a maximum number of iterations in case the division is not exact.
#
# ```
# BigDecimal(1).div(BigDecimal(2)) # => BigDecimal(@value=5, @scale=2)
# BigDecimal(1).div(BigDecimal(3), 5) # => BigDecimal(@value=33333, @scale=5)
# BigDecimal.new(1).div(BigDecimal.new(2)) # => BigDecimal(@value=5, @scale=2)
# BigDecimal.new(1).div(BigDecimal.new(3), 5) # => BigDecimal(@value=33333, @scale=5)
# ```
def div(other : BigDecimal, max_div_iterations = DEFAULT_MAX_DIV_ITERATIONS) : BigDecimal
check_division_by_zero other
Expand Down Expand Up @@ -531,7 +531,7 @@ struct Int
# Converts `self` to `BigDecimal`.
# ```
# require "big"
# 1212341515125412412412421.to_big_d
# 12123415151254124124.to_big_d
# ```
def to_big_d
BigDecimal.new(self)
Expand Down
2 changes: 1 addition & 1 deletion src/big/big_int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct BigInt < Int
#
# ```
# BigInt.new("123456789123456789123456789123456789") # => 123456789123456789123456789123456789
# BigInt.new("123_456_789_123_456_789_123_456_789") # => 123456789012345678901234567890
# BigInt.new("123_456_789_123_456_789_123_456_789") # => 123456789123456789123456789
# BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695
# ```
def initialize(str : String, base = 10)
Expand Down
4 changes: 2 additions & 2 deletions src/enumerable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ module Enumerable(T)
# `pattern === element` is false.
#
# ```
# [1, 3, 2, 5, 4, 6].reject(3..5).should eq([1, 2, 6])
# [1, 3, 2, 5, 4, 6].reject(3..5) # => [1, 2, 6]
# ```
def reject(pattern)
reject { |e| pattern === e }
Expand Down Expand Up @@ -1160,7 +1160,7 @@ module Enumerable(T)
# `pattern === element`.
#
# ```
# [1, 3, 2, 5, 4, 6].select(3..5).should eq([3, 5, 4])
# [1, 3, 2, 5, 4, 6].select(3..5) # => [3, 5, 4]
# ```
def select(pattern)
self.select { |e| pattern === e }
Expand Down
4 changes: 3 additions & 1 deletion src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ class Hash(K, V)
#
# ```
# hash = {:a => 1, :b => 2, :c => 3}
# hash.transform_keys { |key| key.to_s } # => {"A" => 1, "B" => 2, "C" => 3}
# hash.transform_keys { |key| key.to_s } # => {"a" => 1, "b" => 2, "c" => 3}
# ```
def transform_keys(&block : K -> K2) forall K2
each_with_object({} of K2 => V) do |(key, value), memo|
Expand All @@ -702,6 +702,7 @@ class Hash(K, V)
# ```
# hash = {:a => 1, :b => 2, :c => 3}
# hash.transform_values { |value| value + 1 } # => {:a => 2, :b => 3, :c => 4}
# ```
def transform_values(&block : V -> V2) forall V2
each_with_object({} of K => V2) do |(key, value), memo|
memo[key] = yield(value)
Expand All @@ -715,6 +716,7 @@ class Hash(K, V)
# hash = {:a => 1, :b => 2, :c => 3}
# hash.transform_values! { |value| value + 1 }
# hash # => {:a => 2, :b => 3, :c => 4}
# ```
def transform_values!(&block : V -> V)
current = @first
while current
Expand Down
4 changes: 2 additions & 2 deletions src/http/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ module HTTP
# Returns `true` if params is empty.
#
# ```
# Params.new.empty? # => true
# Params.parse("foo=bar&foo=baz&qux=zoo").empty? # => false
# HTTP::Params.new.empty? # => true
# HTTP::Params.parse("foo=bar&foo=baz&qux=zoo").empty? # => false
# ```
delegate empty?, to: raw_params

Expand Down
6 changes: 3 additions & 3 deletions src/http/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ class HTTP::Server
#
# ```
# server = HTTP::Server.new { }
# server.bind("tcp://localhost:80") # => Socket::IPAddress.new("127.0.0.1", 8080)
# server.bind("unix:///tmp/server.sock") # => Socket::UNIXAddress.new("/tmp/server.sock")
# server.bind("tls://127.0.0.1:443?key=private.key&cert=certificate.cert&ca=ca.crt) # => Socket::IPAddress.new("127.0.0.1", 443)
# server.bind("tcp://localhost:80") # => Socket::IPAddress.new("127.0.0.1", 8080)
# server.bind("unix:///tmp/server.sock") # => Socket::UNIXAddress.new("/tmp/server.sock")
# server.bind("tls://127.0.0.1:443?key=private.key&cert=certificate.cert&ca=ca.crt") # => Socket::IPAddress.new("127.0.0.1", 443)
# ```
def bind(uri : String) : Socket::Address
bind(URI.parse(uri))
Expand Down
12 changes: 6 additions & 6 deletions src/humanize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct Number
#
# ```
# 1_200_000_000.humanize # => "1.2G"
# 0.000_000_012.humanize # => "12n"
# 0.000_000_012.humanize # => "12.0n"
# ```
#
# If *significant* is `false`, the number of *precision* digits is preserved
Expand Down Expand Up @@ -139,7 +139,7 @@ struct Number
#
# ```
# 1_200_000_000.humanize # => "1.2G"
# 0.000_000_012.humanize # => "12n"
# 0.000_000_012.humanize # => "12.0n"
# ```
#
# If *significant* is `false`, the number of *precision* digits is preserved
Expand Down Expand Up @@ -172,7 +172,7 @@ struct Number
# end
#
# humanize_length(1_420) # => "1.42 km"
# humanize_length(0.23) # => "23 cm"
# humanize_length(0.23) # => "23.0 cm"
# ```
#
# See `Int#humanize_bytes` to format a file size.
Expand Down Expand Up @@ -275,9 +275,9 @@ struct Int
#
# ```
# 1.humanize_bytes # => "1B"
# 1024.humanize_bytes # => "1.0KB"
# 1536.humanize_bytes # => "1.5KB"
# 524288.humanize_bytes # => "512KB"
# 1024.humanize_bytes # => "1.0kiB"
# 1536.humanize_bytes # => "1.5kiB"
# 524288.humanize_bytes # => "512kiB"
# 1073741824.humanize_bytes(format: :IEC) # => "1.0GiB"
# ```
#
Expand Down
18 changes: 9 additions & 9 deletions src/iterator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ module Iterator(T)
# iterators to chain is large (usually greater than 4) or undetermined.
#
# ```
# array_of_iters = [[1], [2, 3], [4, 5, 6]]
# array_of_iters = [[1], [2, 3], [4, 5, 6]].each.map &.each
# iter = Iterator(Int32).chain array_of_iters
# iter.next # => 1
# iter.next # => 2
Expand Down Expand Up @@ -728,7 +728,7 @@ module Iterator(T)
# where `pattern === element` does not hold.
#
# ```
# iter = [2, 3, 1, 5, 4, 6].reject(3..5)
# iter = [2, 3, 1, 5, 4, 6].each.reject(3..5)
# iter.next # => 2
# iter.next # => 1
# iter.next # => 6
Expand Down Expand Up @@ -785,7 +785,7 @@ module Iterator(T)
# where `pattern === element`.
#
# ```
# iter = [1, 3, 2, 5, 4, 6].select(3..5)
# iter = [1, 3, 2, 5, 4, 6].each.select(3..5)
# iter.next # => 3
# iter.next # => 5
# iter.next # => 4
Expand Down Expand Up @@ -1310,7 +1310,7 @@ module Iterator(T)
# iter.next # => ['d', 'E']
# iter.next # => ['F']
# iter.next # => ['g', 'h']
# iter.next # => Iterator::Stop
# iter.next # => Iterator::Stop::INSTANCE
# ```
#
# By default, a new array is created and yielded for each slice when invoking `next`.
Expand Down Expand Up @@ -1338,7 +1338,7 @@ module Iterator(T)
# iter.next # => ['d', 'E']
# iter.next # => ['F']
# iter.next # => ['g', 'h']
# iter.next # => Iterator::Stop
# iter.next # => Iterator::Stop::INSTANCE
# ```
#
# By default, a new array is created and yielded for each slice when invoking `next`.
Expand Down Expand Up @@ -1416,7 +1416,7 @@ module Iterator(T)
# iter.next # => ['C', 'd']
# iter.next # => ['E']
# iter.next # => ['F', 'g', 'h']
# iter.next # => Iterator::Stop
# iter.next # => Iterator::Stop::INSTANCE
# ```
#
# By default, a new array is created and yielded for each slice when invoking `next`.
Expand Down Expand Up @@ -1444,7 +1444,7 @@ module Iterator(T)
# iter.next # => ['C', 'd']
# iter.next # => ['E']
# iter.next # => ['F', 'g', 'h']
# iter.next # => Iterator::Stop
# iter.next # => Iterator::Stop::INSTANCE
# ```
#
# By default, a new array is created and yielded for each slice when invoking `next`.
Expand Down Expand Up @@ -1528,7 +1528,7 @@ module Iterator(T)
# iter.next # => [9, 10, 11, 12]
# iter.next # => [15, 16]
# iter.next # => [19, 20, 21]
# iter.next # => Iterator::Stop
# iter.next # => Iterator::Stop::INSTANCE
# ```
#
# By default, a new array is created and yielded for each slice when invoking `next`.
Expand Down Expand Up @@ -1558,7 +1558,7 @@ module Iterator(T)
# iter.next # => [9, 10, 11, 12]
# iter.next # => [15, 16]
# iter.next # => [19, 20, 21]
# iter.next # => Iterator::Stop
# iter.next # => Iterator::Stop::INSTANCE
# ```
#
# By default, a new array is created and yielded for each slice when invoking `next`.
Expand Down
8 changes: 4 additions & 4 deletions src/kernel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ end
# sprintf "%20.8g", 1234.56789 # => " 1234.5679"
# sprintf "%20.8g", 123456789 # => " 1.2345679e+08"
# sprintf "%20.8G", 123456789 # => " 1.2345679E+08"
# sprintf "%20.8g", -123456789 # => " -1.2345679e+08"
# sprintf "%20.8G", -123456789 # => " -1.2345679E+08"
# sprintf "%20.8g", -123456789 # => " -1.2345679e+08"
# sprintf "%20.8G", -123456789 # => " -1.2345679E+08"
# ```
#
# The field width is an optional integer, followed optionally by a
Expand All @@ -289,7 +289,7 @@ end
# sprintf "%20d", 123 # => " 123"
# sprintf "%+20d", 123 # => " +123"
# sprintf "%020d", 123 # => "00000000000000000123"
# sprintf "%+020d", 12 # => "+0000000000000000123"
# sprintf "%+020d", 123 # => "+0000000000000000123"
# sprintf "% 020d", 123 # => " 0000000000000000123"
# sprintf "%-20d", 123 # => "123 "
# sprintf "%-+20d", 123 # => "+123 "
Expand All @@ -315,7 +315,7 @@ end
# sprintf "%020.8o", 123 # => "00000000000000000173"
# sprintf "%20.8x", 123 # => " 7b"
# sprintf "%020.8x", 123 # => "0000000000000000007b"
# sprintf "%20.8b", 123 # => " 01111011"
# sprintf "%20.8b", 123 # => " 1111011"
# sprintf "%20.8d", -123 # => " -123"
# sprintf "%020.8d", -123 # => "0000000000000000-123"
# sprintf "%20.8o", -123 # => " -173"
Expand Down
4 changes: 2 additions & 2 deletions src/mime.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require "crystal/system/mime"
# ```
# require "mime"
#
# MIME.from_extension(".html") # => "text/html; charset=utf-8"
# MIME.from_filename("path/file.html") # => "text/html; charset=utf-8"
# MIME.from_extension(".html") # => "text/html"
# MIME.from_filename("path/file.html") # => "text/html"
# ```
#
# The registry will be populated with some default values (see `DEFAULT_TYPES`)
Expand Down
2 changes: 1 addition & 1 deletion src/mime/multipart/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module MIME::Multipart
# ```
# io = IO::Memory.new # This is a stub. Actually, any IO can be used.
# builder = MIME::Multipart::Builder.new(io, "a4VF")
# builder.content_type("mixed") # => "multipart/mixed; boundary=\"a4VF\""
# builder.content_type("mixed") # => "multipart/mixed; boundary=a4VF"
# ```
def content_type(subtype = "mixed")
MIME::MediaType.new("multipart/#{subtype}", {"boundary" => @boundary}).to_s
Expand Down
2 changes: 1 addition & 1 deletion src/static_array.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# ints = uninitialized Int32[3]
# ints[0] = 0
# ints[1] = 8
# ints[3] = 15
# ints[2] = 15
# ```
#
# For number types there is also `Number.static_array` which can be used to initialize
Expand Down
8 changes: 5 additions & 3 deletions src/tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ struct Tuple
# Creates a tuple from the given array, with elements casted to the given types.
#
# ```
# Tuple(String, Int64).from(["world", 2]) # => {"world", 2}
# Tuple(String, Int64).from(["world", 2]).class # => {String, Int64}
# Tuple(String, Int64).from(["world", 2_i64]) # => {"world", 2_i64}
# Tuple(String, Int64).from(["world", 2_i64]).class # => {String, Int64}
# ```
#
# See also: `#from`.
Expand All @@ -101,11 +101,13 @@ struct Tuple
# This allows you to easily pass an array as individual arguments to a method.
#
# ```
# require "json"
#
# def speak_about(thing : String, n : Int64)
# "I see #{n} #{thing}s"
# end
#
# data = JSON.parse(%(["world", 2])).as_a
# data = JSON.parse(%(["world", 2])).as_a.map(&.raw)
# speak_about(*{String, Int64}.from(data)) # => "I see 2 worlds"
# ```
def from(array : Array)
Expand Down
2 changes: 1 addition & 1 deletion src/uuid/json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require "uuid"
# example = Example.from_json(%({"id": "ba714f86-cac6-42c7-8956-bcf5105e1b81"}))
#
# uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
# uuid.to_json # => "87b3042b-9b9a-41b7-8b15-a93d3f17025e"
# uuid.to_json # => "\"87b3042b-9b9a-41b7-8b15-a93d3f17025e\""
# ```
struct UUID
def self.new(pull : JSON::PullParser)
Expand Down