@@ -56,8 +56,8 @@ defmodule Code do
56
56
end
57
57
58
58
@ 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
61
61
environment options. Those options can be:
62
62
63
63
* `:file` - the file to be considered in the evaluation
@@ -71,28 +71,39 @@ defmodule Code do
71
71
* `:requires` - a list of modules required
72
72
* `:functions` - a list of tuples where the first element is a module
73
73
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
75
75
* `:macros` - a list of tuples where the first element is a module
76
76
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
78
78
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
80
80
values. For example, setting `:requires` to `[]`, will no longer
81
81
automatically require the `Kernel` module; in the same way setting
82
82
`:macros` will no longer auto-import `Kernel` macros like `if`, `case`,
83
83
etc.
84
84
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
+
85
90
## Examples
86
91
87
92
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]}
89
100
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:
93
104
94
105
iex> Code.eval_string("a + b", [a: 1, b: 2], __ENV__)
95
- { 3, [ {:a, 1}, {:b, 2} ] }
106
+ {3, [a: 1, b: 2] }
96
107
97
108
"""
98
109
def eval_string ( string , binding // [ ] , opts // [ ] )
@@ -115,21 +126,20 @@ defmodule Code do
115
126
@ doc """
116
127
Evaluates the quoted contents.
117
128
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.
120
130
121
131
## Examples
122
132
123
133
iex> contents = quote(hygiene: [vars: false], do: a + b)
124
134
...> 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] }
126
136
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:
129
139
130
140
iex> contents = quote(hygiene: [vars: false], do: a + b)
131
141
...> Code.eval_quoted(contents, [a: 1, b: 2], __ENV__)
132
- { 3, [ {:a, 1}, {:b, 2} ] }
142
+ {3, [a: 1, b: 2] }
133
143
134
144
"""
135
145
def eval_quoted ( quoted , binding // [ ] , opts // [ ] )
0 commit comments