Permalink
Browse files

re-implement doto using Object#tap

  • Loading branch information...
1 parent 2e71d71 commit 1f142c580d78323bd2f4b901231e49f162168d65 @mental committed Feb 22, 2012
Showing with 14 additions and 0 deletions.
  1. +10 −0 src/ruby_syntax/core.clj
  2. +4 −0 test/ruby_syntax/test.clj
@@ -159,6 +159,15 @@
(translate-forms body)
[" end"]))
+(defn translate-doto [expr & body]
+ (let [name (gensym "doto__")]
+ (translate-form (apply list
+ 'with-block (list '.tap expr) [name]
+ (for [form body]
+ (if (seq? form)
+ (concat [(first form) name] (rest form))
+ (list form name)))))))
+
(defn translate-form [form]
(cond
(map? form)
@@ -183,6 +192,7 @@
module (apply translate-module args)
class (apply translate-class args)
singleton-class (apply translate-singleton-class args)
+ doto (apply translate-doto args)
fn* (translate-lambda args)
with-block (apply translate-block-call args)
ruby-syntax.core/block-expr (translate-block-expr (first args))
@@ -98,3 +98,7 @@
(ruby-syntax (class [Foo Bar] baz))))
(is (= "class << foo; bar end")
(ruby-syntax (singleton-class foo bar))))
+
+(deftest doto-tap
+ (is (re-matches #"^\(a \+ b\)\.tap \{ \|doto__\d+\| doto__\d+\.foo\(1, 2\) \}$"
+ (ruby-syntax (doto (+ a b) (.foo 1 2))))))

0 comments on commit 1f142c5

Please sign in to comment.