From 54c85684715e562bb7aa7e57a9eaacd91a74186c Mon Sep 17 00:00:00 2001 From: Marc Siegel Date: Wed, 7 Feb 2018 17:20:41 -0500 Subject: [PATCH] Update README.md to show extracting helpers that call DSL methods --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 5e2882c..1c1c987 100644 --- a/README.md +++ b/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!