Browse Source

ruby can now be run with NO session

Eric Schulte 16 years ago
parent
commit
00365e1fbe
2 changed files with 39 additions and 19 deletions
  1. 30 16
      lisp/langs/org-babel-ruby.el
  2. 9 3
      org-babel.org

+ 30 - 16
lisp/langs/org-babel-ruby.el

@@ -115,10 +115,14 @@ then create.  Return the initialized session."
   "When evaluated by Ruby this returns the return value of the last statement.")
   "When evaluated by Ruby this returns the return value of the last statement.")
 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
 (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
   "Used to indicate that evaluation is has completed.")
   "Used to indicate that evaluation is has completed.")
-(defun org-babel-ruby-last-value-writer (file-name)
-  "Return a ruby statement to write the last value out to
-FILE-NAME."
-  (format "File.open('w', '%s'){|f| f < _}.write" file-name))
+(defvar org-babel-ruby-wrapper-method
+  "
+def main()
+%s
+end
+results = main()
+File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
+")
 
 
 (defun org-babel-ruby-evaluate (buffer body &optional result-type)
 (defun org-babel-ruby-evaluate (buffer body &optional result-type)
   "Pass BODY to the Ruby process in BUFFER.  If RESULT-TYPE equals
   "Pass BODY to the Ruby process in BUFFER.  If RESULT-TYPE equals
@@ -128,22 +132,32 @@ last statement in BODY."
   (let ((full-body (mapconcat #'org-babel-chomp
   (let ((full-body (mapconcat #'org-babel-chomp
                               (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
                               (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
         raw result)
         raw result)
-    (if (and (stringp buffer) (string= buffer "none"))
+    (if (not session)
         ;; external process evaluation
         ;; external process evaluation
-        (let ((tmp-file (make-temp-file "ruby-functional-results")))
-          ()
-          )
-        ;; comint session evaluation
-        (setq raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
-                    (insert full-body) (comint-send-input nil t)))
+        (save-window-excursion
+          (with-temp-buffer
+            (case result-type
+              (output
+               (insert body)
+               ;; (message "buffer=%s" (buffer-string)) ;; debugging
+               (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
+               (buffer-string))
+              (value
+               (let ((tmp-file (make-temp-file "ruby-functional-results")))
+                 (insert (format org-babel-ruby-wrapper-method body tmp-file))
+                 ;; (message "buffer=%s" (buffer-string)) ;; debugging
+                 (shell-command-on-region (point-min) (point-max) "ruby")
+                 (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))
+      ;; comint session evaluation
+      (setq raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
+                  (insert full-body) (comint-send-input nil t)))
       (setq results
       (setq results
             (cdr (member org-babel-ruby-eoe-indicator
             (cdr (member org-babel-ruby-eoe-indicator
                          (reverse (mapcar #'org-babel-ruby-read-string
                          (reverse (mapcar #'org-babel-ruby-read-string
-                                          (mapcar #'org-babel-trim raw)))))))
-    (case result-type
-      (output (mapconcat #'identity (reverse (cdr results)) "\n"))
-      (value (car results))
-      (t (reverse results)))))
+                                          (mapcar #'org-babel-trim raw))))))
+      (case result-type
+        (output (mapconcat #'identity (reverse (cdr results)) "\n"))
+        (value (car results))))))
 
 
 (defun org-babel-ruby-read-string (string)
 (defun org-babel-ruby-read-string (string)
   "Strip \\\"s from around ruby string"
   "Strip \\\"s from around ruby string"

+ 9 - 3
org-babel.org

@@ -497,7 +497,7 @@ tabel
 
 
 Another example is in the [[*operations%20in%20on%20tables][grades example]].
 Another example is in the [[*operations%20in%20on%20tables][grades example]].
 
 
-** PROPOSED add =:none= session argument (for purely functional execution) [0/4]
+** PROPOSED add =:none= session argument (for purely functional execution) [1/4]
 This would allow source blocks to be run in their own new process
 This would allow source blocks to be run in their own new process
 
 
 - These blocks could then also be run in the background (since we can
 - These blocks could then also be run in the background (since we can
@@ -519,15 +519,21 @@ Down-sides to sessions
 - can't run in background
 - can't run in background
 - litter emacs with session buffers
 - litter emacs with session buffers
 
 
-*** TODO ruby
+*** DONE ruby
 
 
 #+srcname: ruby-task-no-session
 #+srcname: ruby-task-no-session
-#+begin_src ruby :session none
+#+begin_src ruby :results replace value
+puts :eric
 puts :schulte
 puts :schulte
 [1, 2, 3]
 [1, 2, 3]
 #+end_src
 #+end_src
 
 
+#+resname: ruby-task-no-session
+| 1 | 2 | 3 |
+
 *** TODO R
 *** TODO R
+
+
 *** TODO python
 *** TODO python
 *** TODO sh
 *** TODO sh
 ** PROPOSED Are we happy with current behaviour regarding vector/scalar output?
 ** PROPOSED Are we happy with current behaviour regarding vector/scalar output?