Skip to content

Commit 9200327

Browse files
committed
Fixed Issue #1506, added description of return values for Code.eval_string and Code.eval_quoted
1 parent 12f6a9a commit 9200327

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

lib/elixir/lib/code.ex

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ defmodule Code do
5656
end
5757

5858
@doc """
59-
Evaluates the contents given by string. The second argument is the
60-
binding (which should be a keyword), followed by a keyword list of
59+
Evaluates the contents given by `string`. The second argument is
60+
a keyword list of bindings, followed by a keyword list of
6161
environment options. Those options can be:
6262
6363
* `:file` - the file to be considered in the evaluation
@@ -71,28 +71,39 @@ defmodule Code do
7171
* `:requires` - a list of modules required
7272
* `:functions` - a list of tuples where the first element is a module
7373
and the second a list of imported function names and arity. The list
74-
of function names and arity must be sorted;
74+
of function names and arity must be sorted
7575
* `:macros` - a list of tuples where the first element is a module
7676
and the second a list of imported macro names and arity. The list
77-
of function names and arity must be sorted;
77+
of function names and arity must be sorted
7878
79-
Notice that setting any of the values above overrides Elixir default
79+
Notice that setting any of the values above overrides Elixir's default
8080
values. For example, setting `:requires` to `[]`, will no longer
8181
automatically require the `Kernel` module; in the same way setting
8282
`:macros` will no longer auto-import `Kernel` macros like `if`, `case`,
8383
etc.
8484
85+
Returns a tuple of the form `{ value, bindings }`,
86+
where `value` is the the value returned from evaluating `string`; `bindings`
87+
is a keyword list with the value of all variable bindings after evaluating
88+
`string`. If an error occurs while evaluating `string` an exception will be raised.
89+
8590
## Examples
8691
8792
iex> Code.eval_string("a + b", [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
88-
{ 3, [ {:a, 1}, {:b, 2} ] }
93+
{3, [a: 1, b: 2]}
94+
95+
iex> Code.eval_string("c = a + b", [a: 1, b: 2], __ENV__)
96+
{3, [a: 1, b: 2, c: 3]}
97+
98+
iex> Code.eval_string("a = a + b", [a: 1, b: 2])
99+
{3, [a: 3, b: 2]}
89100
90-
For convenience, you can pass `__ENV__` as an argument and
91-
all imports, requires and aliases will be automatically carried
92-
over:
101+
For convenience, you can pass `__ENV__` as the `opts` argument and
102+
all imports, requires and aliases defined in the current environment
103+
will be automatically carried over:
93104
94105
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
95-
{ 3, [ {:a, 1}, {:b, 2} ] }
106+
{3, [a: 1, b: 2]}
96107
97108
"""
98109
def eval_string(string, binding // [], opts // [])
@@ -115,21 +126,20 @@ defmodule Code do
115126
@doc """
116127
Evaluates the quoted contents.
117128
118-
This function accepts a list of environment options.
119-
Check `Code.eval_string` for more information.
129+
See `eval_string/3` for a description of arguments and return values.
120130
121131
## Examples
122132
123133
iex> contents = quote(hygiene: [vars: false], do: a + b)
124134
...> Code.eval_quoted(contents, [a: 1, b: 2], file: __ENV__.file, line: __ENV__.line)
125-
{ 3, [ {:a, 1}, {:b, 2} ] }
135+
{3, [a: 1, b: 2]}
126136
127-
For convenience, you can pass `__ENV__` as an argument and
128-
all options will be automatically extracted from the environment:
137+
For convenience, you can pass `__ENV__` as the `opts` argument and
138+
all options will be automatically extracted from the current environment:
129139
130140
iex> contents = quote(hygiene: [vars: false], do: a + b)
131141
...> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
132-
{ 3, [ {:a, 1}, {:b, 2} ] }
142+
{3, [a: 1, b: 2]}
133143
134144
"""
135145
def eval_quoted(quoted, binding // [], opts // [])

0 commit comments

Comments
 (0)