Skip to content

Commit b9d6f4f

Browse files
fertapricJosé Valim
authored andcommitted
Fix errors with ref/4 IEx helper (#8016)
* Fix errors with ref/4 IEx helper In addition, include documentation for that function. * Update helpers.ex * Add IEx.Helpers.ref/* tests * Use examples that do not return ArgumentError Signed-off-by: José Valim <[email protected]>
1 parent b0cf76c commit b9d6f4f

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,25 +1204,34 @@ defmodule IEx.Helpers do
12041204
12051205
## Examples
12061206
1207-
iex> ref("0.21.32.43")
1208-
#Reference<0.21.32.43>
1207+
iex> ref("0.1.2.3")
1208+
#Reference<0.1.2.3>
12091209
12101210
"""
12111211
@doc since: "1.6.0"
12121212
def ref(string) when is_binary(string) do
12131213
:erlang.list_to_ref('#Ref<#{string}>')
12141214
end
12151215

1216+
@doc """
1217+
Creates a Reference from its 4 non-negative integers components.
1218+
1219+
## Examples
1220+
1221+
iex> ref(0, 1, 2, 3)
1222+
#Reference<0.1.2.3>
1223+
1224+
"""
12161225
@doc since: "1.6.0"
12171226
def ref(w, x, y, z)
12181227
when is_integer(w) and w >= 0 and is_integer(x) and x >= 0 and is_integer(y) and y >= 0 and
12191228
is_integer(z) and z >= 0 do
12201229
:erlang.list_to_ref(
1221-
'<' ++
1230+
'#Ref<' ++
12221231
Integer.to_charlist(w) ++
12231232
'.' ++
12241233
Integer.to_charlist(x) ++
1225-
'.' ++ '.' ++ Integer.to_charlist(y) ++ '.' ++ '.' ++ Integer.to_charlist(z) ++ '.' ++ '>'
1234+
'.' ++ Integer.to_charlist(y) ++ '.' ++ Integer.to_charlist(z) ++ '>'
12261235
)
12271236
end
12281237

lib/iex/test/iex/helpers_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,26 @@ defmodule IEx.HelpersTest do
10901090
end
10911091
end
10921092

1093+
describe "ref" do
1094+
test "builds a ref from string" do
1095+
assert inspect(ref("0.419006.1377304581.1")) == "#Reference<0.419006.1377304581.1>"
1096+
assert inspect(ref("0.1.2.3")) == "#Reference<0.1.2.3>"
1097+
1098+
assert_raise ArgumentError, fn ->
1099+
ref("0.6.6.-6")
1100+
end
1101+
end
1102+
1103+
test "builds a ref from integers" do
1104+
assert inspect(ref(0, 419_006, 1_377_304_581, 1)) == "#Reference<0.419006.1377304581.1>"
1105+
assert inspect(ref(0, 1, 2, 3)) == "#Reference<0.1.2.3>"
1106+
1107+
assert_raise FunctionClauseError, fn ->
1108+
ref(0, 6, 6, -6)
1109+
end
1110+
end
1111+
end
1112+
10931113
describe "i" do
10941114
test "prints information about the data type" do
10951115
assert capture_io(fn -> i(:ok) end) =~ """

0 commit comments

Comments
 (0)