Skip to content

Commit 4f97a53

Browse files
maihaAry Borenszweig
authored andcommitted
Fixed example codes (semantics)
1 parent 0c2b9c6 commit 4f97a53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+403
-341
lines changed

src/array.cr

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# `new` method and a `<<` method. `Set` is one such type:
3232
#
3333
# ```
34-
# set = Set{1, 2, 3} # => [1, 2, 3]
34+
# set = Set{1, 2, 3} # => Set{1, 2, 3}
3535
# set.class # => Set(Int32)
3636
# ```
3737
#
@@ -313,7 +313,7 @@ class Array(T)
313313
# ary[0] = 5
314314
# p ary # => [5,2,3]
315315
#
316-
# ary[3] = 5 # => IndexError
316+
# ary[3] = 5 # raises IndexError
317317
# ```
318318
@[AlwaysInline]
319319
def []=(index : Int, value : T)
@@ -449,7 +449,7 @@ class Array(T)
449449
# a = ["a", "b", "c", "d", "e"]
450450
# a[1..3] # => ["b", "c", "d"]
451451
# a[4..7] # => ["e"]
452-
# a[6..10] # => Index Error
452+
# a[6..10] # raise IndexError
453453
# a[5..10] # => []
454454
# a[-2...-1] # => ["d"]
455455
# ```
@@ -469,7 +469,7 @@ class Array(T)
469469
# ```
470470
# a = ["a", "b", "c", "d", "e"]
471471
# a[-3, 3] # => ["c", "d", "e"]
472-
# a[6, 1] # => Index Error
472+
# a[6, 1] # raise IndexError
473473
# a[1, 2] # => ["b", "c"]
474474
# a[5, 1] # => []
475475
# ```
@@ -618,7 +618,7 @@ class Array(T)
618618
# a = ["ant", "bat", "cat", "dog"]
619619
# a.delete_at(2) # => "cat"
620620
# a # => ["ant", "bat", "dog"]
621-
# a.delete_at(99) # => IndexError
621+
# a.delete_at(99) # raises IndexError
622622
# ```
623623
def delete_at(index : Int)
624624
index = check_index_out_of_bounds index
@@ -638,7 +638,7 @@ class Array(T)
638638
# a = ["ant", "bat", "cat", "dog"]
639639
# a.delete_at(1..2) # => ["bat", "cat"]
640640
# a # => ["ant", "dog"]
641-
# a.delete_at(99..100) # => IndexError
641+
# a.delete_at(99..100) # raises IndexError
642642
# ```
643643
def delete_at(range : Range(Int, Int))
644644
from, size = range_to_index_and_count(range)
@@ -654,7 +654,7 @@ class Array(T)
654654
# a = ["ant", "bat", "cat", "dog"]
655655
# a.delete_at(1, 2) # => ["bat", "cat"]
656656
# a # => ["ant", "dog"]
657-
# a.delete_at(99, 1) # => IndexError
657+
# a.delete_at(99, 1) # raises IndexError
658658
# ```
659659
def delete_at(index : Int, count : Int)
660660
val = self[index, count]
@@ -1014,7 +1014,7 @@ class Array(T)
10141014
# iter.next # => [2, 3, 1]
10151015
# iter.next # => [3, 1, 2]
10161016
# iter.next # => [3, 2, 1]
1017-
# iter.next # => Iterator::Stop
1017+
# iter.next # => #<Iterator::Stop>
10181018
# ```
10191019
#
10201020
# By default, a new array is created and returned for each permutation.
@@ -1109,8 +1109,8 @@ class Array(T)
11091109
# ```
11101110
# s = [1, 2, 3] # => [1, 2, 3]
11111111
# t = [4, 5, 6, [7, 8]] # => [4, 5, 6, [7, 8]]
1112-
# u = [9, [10, 11].each] # => [9, Indexable#ItemIterator]
1113-
# a = [s, t, u, 12, 13] # => [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
1112+
# u = [9, [10, 11].each] # => [9, #<Indexable::ItemIterator>]
1113+
# a = [s, t, u, 12, 13] # => [[1, 2, 3], [4, 5, 6, [7, 8]], 9, #<Indexable::ItemIterator>, 12, 13]
11141114
# a.flatten # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
11151115
# ```
11161116
def flatten
@@ -1325,7 +1325,7 @@ class Array(T)
13251325
# ```
13261326
# a = ["a", "b"]
13271327
# a.push("c") # => ["a", "b", "c"]
1328-
# a.push(1) # => Errors, because the array only accepts String.
1328+
# a.push(1) # Errors, because the array only accepts String.
13291329
#
13301330
# a = ["a", "b"] of (Int32 | String)
13311331
# a.push("c") # => ["a", "b", "c"]
@@ -1714,12 +1714,12 @@ class Array(T)
17141714
#
17151715
# ```
17161716
# a = ["a", "b"]
1717-
# a.unshift("c") # => ["c", a", "b"]
1718-
# a.unshift(1) # => Errors, because the array only accepts String.
1717+
# a.unshift("c") # => ["c", "a", "b"]
1718+
# a.unshift(1) # Errors, because the array only accepts String.
17191719
#
17201720
# a = ["a", "b"] of (Int32 | String)
17211721
# a.unshift("c") # => ["c", "a", "b"]
1722-
# a.unshift(1) # => [1, "a", "b", "c"]
1722+
# a.unshift(1) # => [1, "c", "a", "b"]
17231723
# ```
17241724
def unshift(obj : T)
17251725
insert 0, obj

src/atomic.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ struct Atomic(T)
104104
#
105105
# ```
106106
# atomic = Atomic.new(5)
107-
# atomic.or(3) # => 5
108-
# atomic.get # => 6
107+
# atomic.xor(3) # => 5
108+
# atomic.get # => 6
109109
# ```
110110
def xor(value : T)
111111
Ops.atomicrmw(:xor, pointerof(@value), value, :sequentially_consistent, false)
@@ -153,8 +153,8 @@ struct Atomic(T)
153153
#
154154
# ```
155155
# atomic = Atomic.new(5)
156-
# atomic.set(10) # => 5
157-
# atomic.get # => 10
156+
# atomic.swap(10) # => 5
157+
# atomic.get # => 10
158158
# ```
159159
def swap(value : T)
160160
Ops.atomicrmw(:xchg, pointerof(@value), value, :sequentially_consistent, false)

src/base64.cr

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# require "base64"
1111
#
1212
# enc = Base64.encode("Send reinforcements") # => "U2VuZCByZWluZm9yY2VtZW50cw==\n"
13-
# plain = Base64.decode(enc) # => "Send reinforcements"
13+
# plain = Base64.decode_string(enc) # => "Send reinforcements"
1414
# ```
1515
#
1616
# The purpose of using base64 to encode data is that it translates any binary
@@ -32,15 +32,14 @@ module Base64
3232
# Line feeds are added to every 60 encoded characters.
3333
#
3434
# ```
35-
# require "base64"
36-
#
3735
# puts Base64.encode("Now is the time for all good coders\nto learn Crystal")
3836
# ```
3937
#
4038
# Generates:
4139
#
4240
# ```text
4341
# Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
42+
# Q3J5c3RhbA==
4443
# ```
4544
def encode(data) : String
4645
slice = data.to_slice
@@ -57,9 +56,7 @@ module Base64
5756
# Line feeds are added to every 60 encoded characters.
5857
#
5958
# ```
60-
# require "base64"
61-
#
62-
# Base64.encode("Now is the time for all good coders\nto learn Crystal", io)
59+
# Base64.encode("Now is the time for all good coders\nto learn Crystal", STDOUT)
6360
# ```
6461
def encode(data, io : IO)
6562
count = 0
@@ -90,8 +87,6 @@ module Base64
9087
# This method complies with RFC 4648.
9188
#
9289
# ```
93-
# require "base64"
94-
#
9590
# puts Base64.strict_encode("Now is the time for all good coders\nto learn Crystal")
9691
# ```
9792
#
@@ -118,9 +113,7 @@ module Base64
118113
# This method complies with RFC 4648.
119114
#
120115
# ```
121-
# require "base64"
122-
#
123-
# Base64.strict_encode("Now is the time for all good coders\nto learn Crystal", io)
116+
# Base64.strict_encode("Now is the time for all good coders\nto learn Crystal", STDOUT)
124117
# ```
125118
def strict_encode(data, io : IO)
126119
strict_encode_to_io_internal(data, io, CHARS_STD, pad: true)

src/benchmark.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require "./benchmark/**"
2727
#
2828
# ```
2929
# Benchmark.ips(warmup: 4, calculation: 10) do |x|
30-
# # …
30+
# x.report("sleep") { sleep 0.01 }
3131
# end
3232
# ```
3333
#

src/big/big_rational.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ require "./big"
66
# denominator is positive. Zero has the unique representation 0/1.
77
#
88
# ```
9+
# require "big_rational"
10+
#
911
# r = BigRational.new(BigInt.new(7), BigInt.new(3))
1012
# r.to_s # => "7/3"
1113
#

src/char.cr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct Char
157157
#
158158
# ```
159159
# 'c'.ascii_lowercase? # => true
160-
# 'ç'.lowercase? # => false
160+
# 'ç'.lowercase? # => true
161161
# 'G'.ascii_lowercase? # => false
162162
# '.'.ascii_lowercase? # => false
163163
# ```
@@ -479,9 +479,9 @@ struct Char
479479
#
480480
# ```
481481
# 'a'.inspect # => "'a'"
482-
# '\t'.inspect # => "'\t'"
482+
# '\t'.inspect # => "'\\t'"
483483
# 'あ'.inspect # => "'あ'"
484-
# '\u0012'.inspect # => "'\u{12}'"
484+
# '\u0012'.inspect # => "'\\u{12}'"
485485
# ```
486486
def inspect
487487
dump_or_inspect do |io|
@@ -507,9 +507,9 @@ struct Char
507507
#
508508
# ```
509509
# 'a'.dump # => "'a'"
510-
# '\t'.dump # => "'\t'"
511-
# 'あ'.dump # => "'\u{3042}'"
512-
# '\u0012'.dump # => "'\u{12}'"
510+
# '\t'.dump # => "'\\t'"
511+
# 'あ'.dump # => "'\\u{3042}'"
512+
# '\u0012'.dump # => "'\\u{12}'"
513513
# ```
514514
def dump
515515
dump_or_inspect do |io|
@@ -557,11 +557,11 @@ struct Char
557557
# ```
558558
# '1'.to_i # => 1
559559
# '8'.to_i # => 8
560-
# 'c'.to_i # => ArgumentError
560+
# 'c'.to_i # raises ArgumentError
561561
# '1'.to_i(16) # => 1
562562
# 'a'.to_i(16) # => 10
563563
# 'f'.to_i(16) # => 15
564-
# 'z'.to_i(16) # => ArgumentError
564+
# 'z'.to_i(16) # raises ArgumentError
565565
# ```
566566
def to_i(base : Int = 10) : Int32
567567
to_i?(base) || raise ArgumentError.new("Invalid integer: #{self}")
@@ -573,11 +573,11 @@ struct Char
573573
# ```
574574
# '1'.to_i # => 1
575575
# '8'.to_i # => 8
576-
# 'c'.to_i # => ArgumentError
576+
# 'c'.to_i # raises ArgumentError
577577
# '1'.to_i(16) # => 1
578578
# 'a'.to_i(16) # => 10
579579
# 'f'.to_i(16) # => 15
580-
# 'z'.to_i(16) # => ArgumentError
580+
# 'z'.to_i(16) # raises ArgumentError
581581
# ```
582582
def to_i?(base : Int = 10) : Int32?
583583
raise ArgumentError.new "invalid base #{base}, expected 2 to 36" unless 2 <= base <= 36
@@ -623,7 +623,7 @@ struct Char
623623
# ```
624624
# '1'.to_i # => 1.0
625625
# '8'.to_i # => 8.0
626-
# 'c'.to_i # => ArgumentError
626+
# 'c'.to_i # raises ArgumentError
627627
# ```
628628
def to_f
629629
to_f64
@@ -635,7 +635,7 @@ struct Char
635635
# ```
636636
# '1'.to_i # => 1.0
637637
# '8'.to_i # => 8.0
638-
# 'c'.to_i # => ArgumentError
638+
# 'c'.to_i # raises ArgumentError
639639
# ```
640640
def to_f?
641641
to_f64?

src/complex.cr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# ```
77
# require "complex"
88
#
9-
# Complex.new(1, 0) # => 1 + 0i
10-
# Complex.new(5, -12) #  => 5 - 12i
9+
# Complex.new(1, 0) # => 1.0 + 0.0i
10+
# Complex.new(5, -12) # => 5.0 - 12.0i
1111
# ```
1212
struct Complex
1313
#  Returns the real part of self
@@ -39,7 +39,7 @@ struct Complex
3939
# Write this complex object to an io
4040
#
4141
# ```
42-
# Complex.new(42, 2).to_s # => 42 + 2i
42+
# Complex.new(42, 2).to_s # => "42.0 + 2.0i"
4343
# ```
4444
def to_s(io : IO)
4545
io << @real
@@ -51,7 +51,7 @@ struct Complex
5151
# Write this complex object to an io, surrounded by parentheses.
5252
#
5353
# ```
54-
# Complex.new(42, 2).inspect # => (42 + 2i)
54+
# Complex.new(42, 2).inspect # => "(42.0 + 2.0i)"
5555
# ```
5656
def inspect(io : IO)
5757
io << '('
@@ -63,8 +63,8 @@ struct Complex
6363
# number form, using the Pythagorean theorem.
6464
#
6565
# ```
66-
# Complex.new(42, 2).abs # => 42.0476
67-
# Complex.new(-42, 2).abs # => 42.0476
66+
# Complex.new(42, 2).abs # => 42.047592083257278
67+
# Complex.new(-42, 2).abs # => 42.047592083257278
6868
# ```
6969
def abs
7070
Math.hypot(@real, @imag)
@@ -91,7 +91,7 @@ struct Complex
9191
# Returns a tuple with the abs value and the phase.
9292
#
9393
# ```
94-
# Complex.new(42, 2).polar # => {2.54311, 0.665774}
94+
# Complex.new(42, 2).polar # => {42.047592083257278, 0.047583103276983396}
9595
# ```
9696
def polar
9797
{abs, phase}
@@ -100,8 +100,8 @@ struct Complex
100100
# Returns the conjugate of self
101101
#
102102
# ```
103-
# Complex.new(42, 2).conj # => 42 - 2i
104-
# Complex.new(42, -2).conj # => 42 + 2i
103+
# Complex.new(42, 2).conj # => 42.0 - 2.0i
104+
# Complex.new(42, -2).conj # => 42.0 + 2.0i
105105
# ```
106106
def conj
107107
Complex.new(@real, -@imag)
@@ -139,7 +139,7 @@ struct Complex
139139
# Calculates the exp of self
140140
#
141141
# ```
142-
# Complex.new(4, 2).exp #  => -22.7208 + 49.646i
142+
# Complex.new(4, 2).exp # => -22.720847417619233 + 49.645957334580565i
143143
# ```
144144
def exp
145145
r = Math.exp(@real)

src/concurrent/future.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ end
120120
# Access to get is synchronized between fibers. *&block* is only called once.
121121
# May be canceled before *&block* is called by calling `cancel`.
122122
# ```
123-
# d = delay(1) { Process.kill(Process.pid) }
124-
# long_operation
123+
# d = delay(1) { Process.kill(Signal::KILL, Process.pid) }
124+
# # ... long operations ...
125125
# d.cancel
126126
# ```
127127
def delay(delay, &block : -> _)
@@ -131,9 +131,9 @@ end
131131
# Spawns a `Fiber` to compute *&block* in the background.
132132
# Access to get is synchronized between fibers. *&block* is only called once.
133133
# ```
134-
# f = future { http_request }
135-
# ... other actions ...
136-
# f.get #=> String
134+
# f = future { `echo hello` }
135+
# # ... other actions ...
136+
# f.get # => "hello\n"
137137
# ```
138138
def future(&exp : -> _)
139139
Concurrent::Future.new &exp

src/crypto/bcrypt/password.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class Crypto::Bcrypt::Password
3535
# ```
3636
# password = Crypto::Bcrypt::Password.new("$2a$10$X6rw/jDiLBuzHV./JjBNXe8/Po4wTL0fhdDNdAdjcKN/Fup8tGCya")
3737
# password.version # => "2a"
38-
# password.salt # => X6rw/jDiLBuzHV./JjBNXe
39-
# password.digest # => 8/Po4wTL0fhdDNdAdjcKN/Fup8tGCya
38+
# password.salt # => "X6rw/jDiLBuzHV./JjBNXe"
39+
# password.digest # => "8/Po4wTL0fhdDNdAdjcKN/Fup8tGCya"
4040
# ```
4141
def initialize(@raw_hash : String)
4242
parts = @raw_hash.split("$")

0 commit comments

Comments
 (0)