branch: externals/org
commit 943ea01df31ea8a66d1797d922578c337ff908d5
Author: Tilmann Singer <[email protected]>
Commit: Ihor Radchenko <[email protected]>
ob-ruby: Allow class definitions inside ruby blocks
* lisp/ob-ruby.el (org-babel-ruby-wrapper-method,
org-babel-ruby-pp-wrapper-method): Wrap evaluated Ruby code inside a
lambda instead of a method. This way, defining constants such as
classes becomes possible.
* testing/lisp/test-ob-ruby.el: Add tests for :result value
and pp.
TINYCHANGE
---
lisp/ob-ruby.el | 10 +++-----
testing/lisp/test-ob-ruby.el | 60 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/lisp/ob-ruby.el b/lisp/ob-ruby.el
index e7e470dc82..003fc809e4 100644
--- a/lisp/ob-ruby.el
+++ b/lisp/ob-ruby.el
@@ -213,20 +213,18 @@ Session settings (`:ruby' header arg value) are taken
from PARAMS."
(defvar org-babel-ruby-wrapper-method
"
-def main()
+results = (lambda do
%s
-end
-results = main()
+end).call
File.open('%s', 'w'){ |f| f.write((results.class == String) ? results :
results.inspect) }
")
(defvar org-babel-ruby-pp-wrapper-method
"
require 'pp'
-def main()
+results = (lambda do
%s
-end
-results = main()
+end).call
File.open('%s', 'w') do |f|
$stdout = f
pp results
diff --git a/testing/lisp/test-ob-ruby.el b/testing/lisp/test-ob-ruby.el
index 27f96f6da3..c4d75a80f5 100644
--- a/testing/lisp/test-ob-ruby.el
+++ b/testing/lisp/test-ob-ruby.el
@@ -78,6 +78,66 @@ s = \"6\"
: 5
")))
+(ert-deftest test-ob-ruby/value ()
+ (should
+ (equal 3
+ (org-test-with-temp-text "#+begin_src ruby :results value
+1 + 2
+#+end_src"
+ (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-ruby/value-with-class-definition ()
+ (should
+ (equal 3
+ (org-test-with-temp-text "#+begin_src ruby :results value
+class Three
+ def result
+ 1 + 2
+ end
+end
+Three.new.result
+#+end_src"
+ (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-ruby/value-with-return ()
+ (should
+ (equal 3
+ (org-test-with-temp-text "#+begin_src ruby :results value
+return 3
+2
+#+end_src"
+ (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-ruby/pp ()
+ (should
+ (equal "3\n"
+ (org-test-with-temp-text "#+begin_src ruby :results pp
+1 + 2
+#+end_src"
+ (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-ruby/pp-with-class-definition ()
+ (should
+ (equal "3\n"
+ (org-test-with-temp-text "#+begin_src ruby :results pp
+class Three
+ def result
+ 1 + 2
+ end
+end
+Three.new.result
+#+end_src"
+ (org-babel-execute-src-block)))))
+
+(ert-deftest test-ob-ruby/pp-with-return ()
+ (should
+ (equal "3\n"
+ (org-test-with-temp-text "#+begin_src ruby :results pp
+return 3
+2
+#+end_src"
+ (org-babel-execute-src-block)))))
+
(provide 'test-ob-ruby)
;;; test-ob-ruby.el ends here