Skip to content

Commit 56a52f1

Browse files
author
José Valim
committed
Merge pull request #1094 from mururu/getb-to-getn
Deprecate IO.getb in favor of IO.getn
2 parents d9b98a6 + 5ec2de4 commit 56a52f1

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

lib/elixir/lib/io.ex

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ defmodule IO do
147147
end
148148

149149
@doc """
150-
Gets `count` bytes from the IO device. It returns:
150+
Gets a number of bytes from the io device. If the
151+
io device is an unicode device, count implies
152+
the number of unicode codepoints to be retrieved.
153+
Otherwise, the number of raw bytes. It returns:
151154
152155
* `data` - The input characters.
153156
@@ -157,21 +160,43 @@ defmodule IO do
157160
for instance {:error, :estale} if reading from an
158161
NFS file system.
159162
"""
163+
def getn(prompt, count // 1)
164+
165+
def getn(prompt, count) when is_integer(count) do
166+
getn(group_leader, prompt, count)
167+
end
168+
169+
def getn(device, prompt) do
170+
getn(device, prompt, 1)
171+
end
172+
173+
@doc """
174+
Gets a number of bytes from the io device. If the
175+
io device is an unicode device, count implies
176+
the number of unicode codepoints to be retrieved.
177+
Otherwise, the number of raw bytes.
178+
"""
179+
def getn(device, prompt, count) do
180+
:io.get_chars(map_dev(device), to_iodata(prompt), count)
181+
end
182+
183+
@doc false
160184
def getb(prompt, count // 1)
161185

162186
def getb(prompt, count) when is_integer(count) do
163-
getb(group_leader, prompt, count)
187+
IO.write "[WARNING] IO.getb is deprecated, please use IO.getn instead\n#{Exception.format_stacktrace}"
188+
getn(prompt, count)
164189
end
165190

166191
def getb(device, prompt) do
167-
getb(device, prompt, 1)
192+
IO.write "[WARNING] IO.getb is deprecated, please use IO.getn instead\n#{Exception.format_stacktrace}"
193+
getn(device, prompt)
168194
end
169195

170-
@doc """
171-
Gets `count` bytes from the chosen IO device.
172-
"""
196+
@doc false
173197
def getb(device, prompt, count) do
174-
:io.get_chars(map_dev(device), to_iodata(prompt), count)
198+
IO.write "[WARNING] IO.getb is deprecated, please use IO.getn instead\n#{Exception.format_stacktrace}"
199+
getn(device, prompt, count)
175200
end
176201

177202
@doc """

lib/elixir/test/elixir/io_test.exs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ defmodule IOTest do
2121
assert File.close(file) == :ok
2222
end
2323

24-
test :getb do
24+
test :getn do
2525
{ :ok, file } = File.open(Path.expand('../fixtures/file.txt', __FILE__))
26-
assert "F" == IO.getb(file, "")
27-
assert "O" == IO.getb(file, "")
28-
assert "O" == IO.getb(file, "")
29-
assert "\n" == IO.getb(file, "")
30-
assert :eof == IO.getb(file, "")
26+
assert "F" == IO.getn(file, "")
27+
assert "O" == IO.getn(file, "")
28+
assert "O" == IO.getn(file, "")
29+
assert "\n" == IO.getn(file, "")
30+
assert :eof == IO.getn(file, "")
3131
assert File.close(file) == :ok
3232
end
3333

34-
test :getb_with_count do
34+
test :getn_with_count do
3535
{ :ok, file } = File.open(Path.expand('../fixtures/file.txt', __FILE__), [:charlist])
36-
assert 'FOO' == IO.getb(file, "", 3)
36+
assert 'FOO' == IO.getn(file, "", 3)
3737
assert File.close(file) == :ok
3838
end
3939

40-
test :getb_with_utf8_and_binary do
40+
test :getn_with_utf8_and_binary do
4141
{ :ok, file } = File.open(Path.expand('../fixtures/utf8.txt', __FILE__), [:utf8])
42-
assert "Русский" == IO.getb(file, "", 7)
42+
assert "Русский" == IO.getn(file, "", 7)
4343
assert File.close(file) == :ok
4444
end
4545

0 commit comments

Comments
 (0)