Skip to content

Commit

Permalink
Let puts and other printing methods return nil
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jan 5, 2017
1 parent 904b9fa commit ba5c2b0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
24 changes: 12 additions & 12 deletions src/array.cr
Expand Up @@ -94,9 +94,9 @@ class Array(T)
# Array.new(3, 'a') # => ['a', 'a', 'a']
#
# ary = Array.new(3, [1])
# puts ary # => [[1], [1], [1]]
# ary # => [[1], [1], [1]]
# ary[0][0] = 2
# puts ary # => [[2], [2], [2]]
# ary # => [[2], [2], [2]]
# ```
def initialize(size : Int, value : T)
if size < 0
Expand All @@ -120,9 +120,9 @@ class Array(T)
# Array.new(3) { |i| (i + 1) ** 2 } # => [1, 4, 9]
#
# ary = Array.new(3) { [1] }
# puts ary # => [[1], [1], [1]]
# ary # => [[1], [1], [1]]
# ary[0][0] = 2
# puts ary # => [[2], [1], [1]]
# ary # => [[2], [1], [1]]
# ```
def self.new(size : Int, &block : Int32 -> T)
Array(T).build(size) do |buffer|
Expand Down Expand Up @@ -521,12 +521,12 @@ class Array(T)
# ary = [[1, 2], [3, 4]]
# ary2 = ary.clone
# ary[0][0] = 5
# puts ary # => [[5, 2], [3, 4]]
# puts ary2 # => [[1, 2], [3, 4]]
# ary # => [[5, 2], [3, 4]]
# ary2 # => [[1, 2], [3, 4]]
#
# ary2 << [7, 8]
# puts ary # => [[5, 2], [3, 4]]
# puts ary2 # => [[1, 2], [3, 4], [7, 8]]
# ary # => [[5, 2], [3, 4]]
# ary2 # => [[1, 2], [3, 4], [7, 8]]
# ```
def clone
Array(T).new(size) { |i| @buffer[i].clone.as(T) }
Expand Down Expand Up @@ -674,12 +674,12 @@ class Array(T)
# ary = [[1, 2], [3, 4]]
# ary2 = ary.dup
# ary[0][0] = 5
# puts ary # => [[5, 2], [3, 4]]
# puts ary2 # => [[5, 2], [3, 4]]
# ary # => [[5, 2], [3, 4]]
# ary2 # => [[5, 2], [3, 4]]
#
# ary2 << [7, 8]
# puts ary # => [[5, 2], [3, 4]]
# puts ary2 # => [[5, 2], [3, 4], [7, 8]]
# ary # => [[5, 2], [3, 4]]
# ary2 # => [[5, 2], [3, 4], [7, 8]]
# ```
def dup
Array(T).build(@capacity) do |buffer|
Expand Down
8 changes: 4 additions & 4 deletions src/big/big_int.cr
Expand Up @@ -274,7 +274,7 @@ struct BigInt < Int
# Returns a string representation of self.
#
# ```
# puts BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
# BigInt.new("123456789101101987654321").to_s # => 123456789101101987654321
# ```
def to_s
String.new(to_cstr)
Expand All @@ -289,9 +289,9 @@ struct BigInt < Int
# Returns a string containing the representation of big radix base (2 through 36).
#
# ```
# puts BigInt.new("123456789101101987654321").to_s(8) # => 32111154373025463465765261
# puts BigInt.new("123456789101101987654321").to_s(16) # => 1a249b1f61599cd7eab1
# puts BigInt.new("123456789101101987654321").to_s(36) # => k3qmt029k48nmpd
# BigInt.new("123456789101101987654321").to_s(8) # => 32111154373025463465765261
# BigInt.new("123456789101101987654321").to_s(16) # => 1a249b1f61599cd7eab1
# BigInt.new("123456789101101987654321").to_s(36) # => k3qmt029k48nmpd
# ```
def to_s(base : Int)
raise "Invalid base #{base}" unless 2 <= base <= 36
Expand Down
2 changes: 1 addition & 1 deletion src/bit_array.cr
Expand Up @@ -89,7 +89,7 @@ struct BitArray
#
# ```
# ba = BitArray.new(5)
# puts ba.to_s # => "BitArray[00000]"
# ba.to_s # => "BitArray[00000]"
# ```
def to_s(io : IO)
io << "BitArray["
Expand Down
4 changes: 2 additions & 2 deletions src/enum.cr
Expand Up @@ -44,14 +44,14 @@
# An enum can be created from an integer:
#
# ```
# puts Color.new(1) # => "Green"
# Color.new(1).to_s # => "Green"
# ```
#
# Values that don't correspond to an enum's constants are allowed: the value will still be of type Color,
# but when printed you will get the underlying value:
#
# ```
# puts Color.new(10) # => "10"
# Color.new(10).to_s # => "10"
# ```
#
# This method is mainly intended to convert integers from C to enums in Crystal.
Expand Down
31 changes: 14 additions & 17 deletions src/kernel.cr
Expand Up @@ -44,21 +44,20 @@ end
# Prints objects to STDOUT and then invokes `STDOUT.flush`.
#
# See also: `IO#print`.
def print(*objects : _)
def print(*objects : _) : Nil
STDOUT.print *objects
STDOUT.flush
nil
end

# Prints a formatted string to STDOUT.
#
# See also: `IO#printf`.
def printf(format_string, *args)
def printf(format_string, *args) : Nil
printf format_string, args
end

# ditto
def printf(format_string, args : Array | Tuple)
def printf(format_string, args : Array | Tuple) : Nil
STDOUT.printf format_string, args
end

Expand All @@ -79,40 +78,38 @@ end
# Prints objects to STDOUT, each followed by a newline.
#
# See also: `IO#puts`.
def puts(*objects)
def puts(*objects) : Nil
STDOUT.puts *objects
end

# Pretty prints *object* to STDOUT followed
# by a newline. Returns *object*.
# by a newline.
#
# See also: `Object#pretty_print(pp)`.
def p(object)
def p(object) : Nil
PrettyPrint.format(object, STDOUT, 79)
puts
object
end

# Pretty prints each object in *objects* to STDOUT, followed
# by a newline. Returns *objects*.
# by a newline.
#
# See also: `Object#pretty_print(pp)`.
def p(*objects)
def p(*objects) : Nil
objects.each do |obj|
p obj
end
objects
end

# Pretty prints each object in *objects* to STDOUT, followed
# by a newline. Returns *objects*.
# by a newline.
#
# ```
# p foo: 23, bar: 42 # => {foo: 23, bar: 42}
# p foo: 23, bar: 42 # prints "{foo: 23, bar: 42}"
# ```
#
# See `Object#pretty_print(pp)`
def p(**objects)
def p(**objects) : Nil
p(objects)
end

Expand Down Expand Up @@ -157,15 +154,15 @@ end
# ```text
# goodbye cruel world
# ```
def at_exit(&handler : Int32 ->)
def at_exit(&handler : Int32 ->) : Nil
AtExitHandlers.add(handler)
end

# Terminates execution immediately, returning the given status code
# to the invoking environment.
#
# Registered `at_exit` procs are executed.
def exit(status = 0)
def exit(status = 0) : NoReturn
AtExitHandlers.run status
STDOUT.flush
STDERR.flush
Expand All @@ -174,7 +171,7 @@ end

# Terminates execution immediately, printing *message* to STDERR and
# then calling `exit(status)`.
def abort(message, status = 1)
def abort(message, status = 1) : NoReturn
STDERR.puts message if message
exit status
end
Expand Down
2 changes: 1 addition & 1 deletion src/pointer.cr
Expand Up @@ -18,7 +18,7 @@ require "c/string"
# x = 1
# ptr = pointerof(x)
# ptr.value = 2
# puts x # => 2
# x # => 2
# ```
#
# Note that a pointer is *falsey* if it's null (if it's address is zero).
Expand Down

0 comments on commit ba5c2b0

Please sign in to comment.