Module: GalaazUtil
- Defined in:
- lib/util/exec_ruby.rb,
lib/util/inline_file.rb
Overview
Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation, without fee and without a signed licensing agreement, is hereby granted, provided that the above copyright notice, this paragraph and the following two paragraphs appear in all copies, modifications, and distributions.
IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED “AS IS”. RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Class Method Summary collapse
-
.exec_ruby(options) ⇒ R::List
—————————————————————————————- Executes the ruby code with the given options.
-
.inline_file(filename, relative, pwd = Dir.pwd) ⇒ Object
Opens the given filename for reading and returns the file content so that it can be printed as a code chunk in rmarkdown ========================================================================================.
Class Method Details
.exec_ruby(options) ⇒ R::List
Executes the ruby code with the given options. The options are:
-
options.code: the ruby code
-
options[]: evaluate if true
-
options.echo: if true, show the source code of the chunk in the output
-
options.message: if true, show error message if any exception in the ruby code
-
options.warning: if true, show stack trace from the ruby code
-
options.include: if true, include the code output
Note that we need to access the ‘eval’ element of the list by indexing as options[], this is because eval is a Ruby function and doing options.eval will call the eval method on options, which is not what we want
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/util/exec_ruby.rb', line 78 def self.exec_ruby() # RubyChunk.init # read the chunk code code = R.paste(.code, collapse: "\n") >> 0 # the output should be a list with the proper structure to pass to # function engine_output. We first add the souce code from the block to # the list out_list = R.list(R.structure(R.list(src: code), class: 'source')) if .echo >> 0 begin # set $stdout to a new StringIO object so that everything that is # output from instance_eval is captured and can be sent to the # report $stdout = StringIO.new # Execute the Ruby code in the scope of class RubyChunk. This is done # so that instance variables created in one chunk can be used again on # another chunk # RChunk.instance_eval(code) if (options[["eval"]] >> 0) eval(code, RCbinding, __FILE__, __LINE__ + 1) if ([["eval"]] >> 0) # add the returned value to the list # this should have captured everything in the evaluation code # it is not working since at least RC10. out = $stdout.string out_list = R.c(out_list, out) rescue StandardError => e # print the error message if (. >> 0) = R.list(R.structure(R.list(message: e.), class: 'message')) out_list = R.c(out_list, ) end # Print the backtrace of the error message if (.warning >> 0) bt = "" e.backtrace.each { |line| bt << line + "\n"} warning = R.list(R.structure(R.list(message: bt), class: 'message')) out_list = R.c(out_list, warning) end rescue SyntaxError => e STDERR.puts "A syntax error occured in ruby block '#{.label >> 0}'" raise SyntaxError.new(e) ensure # return $stdout to standard output $stdout = STDOUT end (.include >> 0)? out_list : R.list end |
.inline_file(filename, relative, pwd = Dir.pwd) ⇒ Object
Opens the given filename for reading and returns the file content so that it can be printed as a code chunk in rmarkdown
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/util/inline_file.rb', line 37 def self.inline_file(filename, relative, pwd = Dir.pwd) filename << ".rb" if File.extname(filename) == "" file = "#{pwd}/#{filename}" if (relative == false) $LOAD_PATH.each do |path| begin files = Dir.entries(path) rescue Errno::ENOENT next end if files != nil file = "#{path}/#{filename}" # break if File.exist?(file) break if (R.file__exists(file) >> 0) end end end # There is a bug(?) in > RC15 that when the bellow command # is called, there is a call to R Polyglot eval passing to_i # if File.exist?(file) if (R.file__exists(file) >> 0) code = "" File.open(file, "r") do |fileObj| while (line = fileObj.gets) code << line end end else raise Errno::ENOENT, "file #{filename} not found in #{$LOAD_PATH}" end code end |