0

script.rb:

puts 'hello'
puts 'foo'

main.rb:

puts `jruby script.rb` # receive the expected result

The question:
How can the same be achieved with reading the "script" before execution?

main.rb:

code=File.open('script.rb', 'r').read.gsub('"', '\"')
# puts `jruby -e '#{code}'` # Does not work for relatively big files;

Windows and unicode are the reasons of this question;

Please note that `jruby script.rb' creates a new process which is essential.

8
  • is main.rb run by jruby or ruby? Commented Aug 5, 2013 at 11:18
  • @PriteshJ, jruby main.rb Commented Aug 5, 2013 at 11:23
  • I was wondering why jruby --eval code is used and not directly eval(code) Commented Aug 5, 2013 at 11:25
  • also please specify what platform are you facing this issue on windows or linux regarding unicode Commented Aug 5, 2013 at 11:35
  • 1
    @ted "Windows and unicode are the reasons of this question" - could you elaborate? Commented Aug 5, 2013 at 12:25

3 Answers 3

1

Store the modified script in a Tempfile and run that instead of passing the whole contents as an eval argument:

require 'tempfile'

code = IO.read('script.rb').gsub('"', '\"')

begin
  tempfile = Tempfile.new 'mytempfile'
  f.write code
  f.close
  puts `jruby '#{f.path}'`
ensure
  f.close
  f.unlink
end

The reason you’re likely getting an error is either a lack of proper escaping or a limit on the maximum argument length in the shell.

Also, beware that in your original implementation you never close the original file. I’ve fixed that by instead using IO.read.

Sign up to request clarification or add additional context in comments.

5 Comments

the problem is in running script from file, even from tmp dir; That's why I'm looking for a option to execute the script when file has been read. This will solve all the problem ` jruby IO.read('script.rb') ` (read and exec)
@ted you should state your actual problem in your question then, instead of a problem with your hacky solution to the real problem.
agreed partially; Still my solution may be hacky, the question states: read and eval code instead of exec the script file. Dunno what's unclear, regards.
@ted My point is that you’re only trying to do that because of some other problem, it seems.
mostly, besides the matter of simply interest. I've been thinking that the file should be read in any case in order to be executed... but nvm
1

In the command line, using

$ getconf ARG_MAX

will give the upper limit on how many bytes can be used for the command line argument and environment variables.

@Andrew Marshall's answer is better, but suppose you don't want to use a temp file, and assuming we can use fork in JRuby,

require 'ffi'
module Exec
  extend FFI::Library
  ffi_lib FFI::Platform::LIBC
  attach_function :fork, [], :int
end

code = IO.read('script.rb')
pid = Exec.fork
if 0 == pid
  eval code
  exit 0
else
  Process.waitpid pid
end

2 Comments

Pretty interesting solution. I'm even afraid to use it (: blog.headius.com/2009/05/… (update section). "Spoon" for a save fork solution gist.github.com/headius/321084
Spoon certainly looks safer, but I think it still requires a temp file.
0

use require

main.rb:

require "script.rb"

1 Comment

will this create a new process while evaluating the script and return the output result as a string?..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.