Skip to content

Commit ba5c2b0

Browse files
author
Ary Borenszweig
committedJan 5, 2017
Let puts and other printing methods return nil
1 parent 904b9fa commit ba5c2b0

File tree

6 files changed

+34
-37
lines changed

6 files changed

+34
-37
lines changed
 

‎src/array.cr

+12-12
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ class Array(T)
9494
# Array.new(3, 'a') # => ['a', 'a', 'a']
9595
#
9696
# ary = Array.new(3, [1])
97-
# puts ary # => [[1], [1], [1]]
97+
# ary # => [[1], [1], [1]]
9898
# ary[0][0] = 2
99-
# puts ary # => [[2], [2], [2]]
99+
# ary # => [[2], [2], [2]]
100100
# ```
101101
def initialize(size : Int, value : T)
102102
if size < 0
@@ -120,9 +120,9 @@ class Array(T)
120120
# Array.new(3) { |i| (i + 1) ** 2 } # => [1, 4, 9]
121121
#
122122
# ary = Array.new(3) { [1] }
123-
# puts ary # => [[1], [1], [1]]
123+
# ary # => [[1], [1], [1]]
124124
# ary[0][0] = 2
125-
# puts ary # => [[2], [1], [1]]
125+
# ary # => [[2], [1], [1]]
126126
# ```
127127
def self.new(size : Int, &block : Int32 -> T)
128128
Array(T).build(size) do |buffer|
@@ -521,12 +521,12 @@ class Array(T)
521521
# ary = [[1, 2], [3, 4]]
522522
# ary2 = ary.clone
523523
# ary[0][0] = 5
524-
# puts ary # => [[5, 2], [3, 4]]
525-
# puts ary2 # => [[1, 2], [3, 4]]
524+
# ary # => [[5, 2], [3, 4]]
525+
# ary2 # => [[1, 2], [3, 4]]
526526
#
527527
# ary2 << [7, 8]
528-
# puts ary # => [[5, 2], [3, 4]]
529-
# puts ary2 # => [[1, 2], [3, 4], [7, 8]]
528+
# ary # => [[5, 2], [3, 4]]
529+
# ary2 # => [[1, 2], [3, 4], [7, 8]]
530530
# ```
531531
def clone
532532
Array(T).new(size) { |i| @buffer[i].clone.as(T) }
@@ -674,12 +674,12 @@ class Array(T)
674674
# ary = [[1, 2], [3, 4]]
675675
# ary2 = ary.dup
676676
# ary[0][0] = 5
677-
# puts ary # => [[5, 2], [3, 4]]
678-
# puts ary2 # => [[5, 2], [3, 4]]
677+
# ary # => [[5, 2], [3, 4]]
678+
# ary2 # => [[5, 2], [3, 4]]
679679
#
680680
# ary2 << [7, 8]
681-
# puts ary # => [[5, 2], [3, 4]]
682-
# puts ary2 # => [[5, 2], [3, 4], [7, 8]]
681+
# ary # => [[5, 2], [3, 4]]
682+
# ary2 # => [[5, 2], [3, 4], [7, 8]]
683683
# ```
684684
def dup
685685
Array(T).build(@capacity) do |buffer|

‎src/big/big_int.cr

+4-4
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ struct BigInt < Int
274274
# Returns a string representation of self.
275275
#
276276
# ```
277-
# puts BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
277+
# BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
278278
# ```
279279
def to_s
280280
String.new(to_cstr)
@@ -289,9 +289,9 @@ struct BigInt < Int
289289
# Returns a string containing the representation of big radix base (2 through 36).
290290
#
291291
# ```
292-
# puts BigInt.new("123456789101101987654321").to_s(8) # => 32111154373025463465765261
293-
# puts BigInt.new("123456789101101987654321").to_s(16) # => 1a249b1f61599cd7eab1
294-
# puts BigInt.new("123456789101101987654321").to_s(36) # => k3qmt029k48nmpd
292+
# BigInt.new("123456789101101987654321").to_s(8) # => 32111154373025463465765261
293+
# BigInt.new("123456789101101987654321").to_s(16) # => 1a249b1f61599cd7eab1
294+
# BigInt.new("123456789101101987654321").to_s(36) # => k3qmt029k48nmpd
295295
# ```
296296
def to_s(base : Int)
297297
raise "Invalid base #{base}" unless 2 <= base <= 36

‎src/bit_array.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct BitArray
8989
#
9090
# ```
9191
# ba = BitArray.new(5)
92-
# puts ba.to_s # => "BitArray[00000]"
92+
# ba.to_s # => "BitArray[00000]"
9393
# ```
9494
def to_s(io : IO)
9595
io << "BitArray["

‎src/enum.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
# An enum can be created from an integer:
4545
#
4646
# ```
47-
# puts Color.new(1) # => "Green"
47+
# Color.new(1).to_s # => "Green"
4848
# ```
4949
#
5050
# Values that don't correspond to an enum's constants are allowed: the value will still be of type Color,
5151
# but when printed you will get the underlying value:
5252
#
5353
# ```
54-
# puts Color.new(10) # => "10"
54+
# Color.new(10).to_s # => "10"
5555
# ```
5656
#
5757
# This method is mainly intended to convert integers from C to enums in Crystal.

‎src/kernel.cr

+14-17
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,20 @@ end
4444
# Prints objects to STDOUT and then invokes `STDOUT.flush`.
4545
#
4646
# See also: `IO#print`.
47-
def print(*objects : _)
47+
def print(*objects : _) : Nil
4848
STDOUT.print *objects
4949
STDOUT.flush
50-
nil
5150
end
5251

5352
# Prints a formatted string to STDOUT.
5453
#
5554
# See also: `IO#printf`.
56-
def printf(format_string, *args)
55+
def printf(format_string, *args) : Nil
5756
printf format_string, args
5857
end
5958

6059
# ditto
61-
def printf(format_string, args : Array | Tuple)
60+
def printf(format_string, args : Array | Tuple) : Nil
6261
STDOUT.printf format_string, args
6362
end
6463

@@ -79,40 +78,38 @@ end
7978
# Prints objects to STDOUT, each followed by a newline.
8079
#
8180
# See also: `IO#puts`.
82-
def puts(*objects)
81+
def puts(*objects) : Nil
8382
STDOUT.puts *objects
8483
end
8584

8685
# Pretty prints *object* to STDOUT followed
87-
# by a newline. Returns *object*.
86+
# by a newline.
8887
#
8988
# See also: `Object#pretty_print(pp)`.
90-
def p(object)
89+
def p(object) : Nil
9190
PrettyPrint.format(object, STDOUT, 79)
9291
puts
93-
object
9492
end
9593

9694
# Pretty prints each object in *objects* to STDOUT, followed
97-
# by a newline. Returns *objects*.
95+
# by a newline.
9896
#
9997
# See also: `Object#pretty_print(pp)`.
100-
def p(*objects)
98+
def p(*objects) : Nil
10199
objects.each do |obj|
102100
p obj
103101
end
104-
objects
105102
end
106103

107104
# Pretty prints each object in *objects* to STDOUT, followed
108-
# by a newline. Returns *objects*.
105+
# by a newline.
109106
#
110107
# ```
111-
# p foo: 23, bar: 42 # => {foo: 23, bar: 42}
108+
# p foo: 23, bar: 42 # prints "{foo: 23, bar: 42}"
112109
# ```
113110
#
114111
# See `Object#pretty_print(pp)`
115-
def p(**objects)
112+
def p(**objects) : Nil
116113
p(objects)
117114
end
118115

@@ -157,15 +154,15 @@ end
157154
# ```text
158155
# goodbye cruel world
159156
# ```
160-
def at_exit(&handler : Int32 ->)
157+
def at_exit(&handler : Int32 ->) : Nil
161158
AtExitHandlers.add(handler)
162159
end
163160

164161
# Terminates execution immediately, returning the given status code
165162
# to the invoking environment.
166163
#
167164
# Registered `at_exit` procs are executed.
168-
def exit(status = 0)
165+
def exit(status = 0) : NoReturn
169166
AtExitHandlers.run status
170167
STDOUT.flush
171168
STDERR.flush
@@ -174,7 +171,7 @@ end
174171

175172
# Terminates execution immediately, printing *message* to STDERR and
176173
# then calling `exit(status)`.
177-
def abort(message, status = 1)
174+
def abort(message, status = 1) : NoReturn
178175
STDERR.puts message if message
179176
exit status
180177
end

‎src/pointer.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require "c/string"
1818
# x = 1
1919
# ptr = pointerof(x)
2020
# ptr.value = 2
21-
# puts x # => 2
21+
# x # => 2
2222
# ```
2323
#
2424
# Note that a pointer is *falsey* if it's null (if it's address is zero).

0 commit comments

Comments
 (0)
Please sign in to comment.