Skip to content

Commit 954a889

Browse files
committed
Add Referencing Values In IEx's History as an elixir til
1 parent 7b86428 commit 954a889

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_512 TILs and counting..._
10+
_513 TILs and counting..._
1111

1212
---
1313

@@ -104,6 +104,7 @@ _512 TILs and counting..._
104104
- [Pattern Matching In Anonymous Functions](elixir/pattern-matching-in-anonymous-functions.md)
105105
- [Quitting IEx](elixir/quitting-iex.md)
106106
- [Range Into List Using Comprehensions](elixir/range-into-list-using-comprehensions.md)
107+
- [Referencing Values In IEx's History](elixir/referencing-values-in-iexs-history.md)
107108
- [Replace Duplicates In A Keyword List](elixir/replace-duplicates-in-a-keyword-list.md)
108109
- [Requiring Keys For Structs](elixir/requiring-keys-for-structs.md)
109110
- [Reversing A List](elixir/reversing-a-list.md)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Referencing Values In IEx's History
2+
3+
Each time we execute a statement in an `iex` session, the counter is
4+
incremented. These numbers are references to the history of the session. We
5+
can use these references to _refer_ to previously executed values using
6+
`v/1`. This is particularly handy for multi-line statements or when we
7+
forget to bind to the result of some function.
8+
9+
Consider the following `iex` session:
10+
11+
```elixir
12+
iex(1)> :one
13+
:one
14+
iex(2)> 1 + 1
15+
2
16+
iex(3)> "three" |> String.to_atom()
17+
:three
18+
```
19+
20+
If we execute `v()` on its own, it is the same as `v(-1)` in that it will
21+
give us the latest value in the history.
22+
23+
```elixir
24+
iex(4)> v()
25+
:three
26+
```
27+
28+
Providing any positive number will refer to the references we see next to
29+
each statement.
30+
31+
```elixir
32+
iex(5)> v(1)
33+
:one
34+
```
35+
36+
Negative numbers, as we saw with `v(-1)`, will count backwards in the
37+
history from where we are.
38+
39+
```elixir
40+
iex(6)> v(-4)
41+
2
42+
```
43+
44+
See `h v` for more details.

0 commit comments

Comments
 (0)