Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pyruby module

A Python module written in C that provides an embedded Ruby interpreter within Python to allow evaluation of Ruby code in a sandboxed sub environment.

  • Note that the return values of pyrb.eval are Python-ized Ruby objects (Ruby arrays become Python lists, Ruby exceptions are converted to Python runtime exceptions and rethrown properly, Ruby's nil becomes Python's None, etc.)

  • The module can't be generated unless it's statically linked with Ruby's libraries. To that end, an x86_64 libruby-static.a archive is included (from Ruby v1.9.1).

What purpose does this serve?

It has very few practical uses (I was a bored undergraduate).

Multithreading

Python's GIL (global interpreter lock) is completely independent of any executing Ruby code; meaning that Ruby, without a global interpeter lock, is free to run as many threads as it likes. It may work, but there are much better methods of parallelization.

Functions

  • pyrb.eval_s evaluate a string
  • pyrb.eval_f evaluate a file
  • pyrb.eval evaluate whichever it looks like

Usage

import pyrb

>>> pyrb.eval_s("""def use_a_block(arg = %|Top o' the morning, gov'na!|)
...                yield(arg); end
...                use_a_block { |str| puts str.inspect }
...             """)
"Top o' the morning, gov'na!"

>>> pyrb.eval_f("RubyScriptFile.rb")
"Pretend I did something super cool, mmkay?"

>>> pyrb.eval("Run this file if it exists otherwise evaluate the string")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pyrb.ruby_error: #<NameError: undefined local variable or method `string' for main:Object>
#!/usr/bin/env python2
import pyrb

ruby_script_array="""
    def make_an_array()
        array = []
        3.times { |i| array.push(i) }
        array.push(3.14159265)
        return array
    end

    make_an_array()
"""

ruby_script_exception="""
    raise "This Exception was raised in Ruby"
"""


print("\n- Executing this Ruby code:")
print(ruby_script_array)

list_from_ruby = pyrb.eval(ruby_script_array)

print("\n- The value returned by Ruby to Python is (Ruby returns the value of the last expression by default):")
print("\n    {}".format(list_from_ruby))
print("\n\n- Executing this Ruby code:")
print(ruby_script_exception)

try:
    pyrb.eval(ruby_script_exception)
except Exception as exc:
    print("\n- Caught the exception raised from Ruby in a Python try/catch block!  Re-raising for traceback...\n")
    raise exc
- Executing this Ruby code:

    def make_an_array()
        array = []
        3.times { |i| array.push(i) }
        array.push(3.14159265)
        return array
    end

    make_an_array()


- The value returned by Ruby to Python is (Ruby returns the value of the last expression by default):

    [0, 1, 2, 3.14159265]


- Executing this Ruby code:

    raise "This Exception was raised in Ruby"


- Caught the exception raised from Ruby in a Python try/catch block!  Re-raising for traceback...

Traceback (most recent call last):
  File "test.py", line 34, in <module>
    raise exc
pyrb.ruby_error: #<RuntimeError: This Exception was raised in Ruby>

About

A Python module that allows executing Ruby code from within the Python interpreter.

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Contributors

Languages