mirror of
https://github.com/ms-ati/docile
synced 2023-03-27 23:21:52 -04:00
Update README.md to show extracting helpers that call DSL methods
This commit is contained in:
parent
745b317e12
commit
54c8568471
1 changed files with 23 additions and 0 deletions
23
README.md
23
README.md
|
@ -50,6 +50,29 @@ end
|
|||
|
||||
Easy!
|
||||
|
||||
### Next step: Allow helper methods to call DSL methods
|
||||
|
||||
What if, in our use of the methods of Array as a DSL, we want to extract
|
||||
helper methods which in turn call DSL methods?
|
||||
|
||||
```ruby
|
||||
def pop_sum_and_push(n)
|
||||
sum = 0
|
||||
n.times { sum += pop }
|
||||
push sum
|
||||
end
|
||||
|
||||
Docile.dsl_eval([]) do
|
||||
push 5
|
||||
push 6
|
||||
pop_sum_and_push(2)
|
||||
end
|
||||
#=> [11]
|
||||
```
|
||||
|
||||
Without Docile, you may find this sort of code extraction to be more
|
||||
challenging.
|
||||
|
||||
### Wait! Can't I do that with just `instance_eval` or `instance_exec`?
|
||||
|
||||
Good question!
|
||||
|
|
Loading…
Reference in a new issue