Skip to main content

Overview

If you’ve ever needed to use Ruby for a particular task, but wanted to use Python as your primary language, Rython lets you easily mix the two languages together.

Why would I want to mix Ruby and Python? There are many reasons:

  • you need a Ruby Gem that provides unique functionality which no Python module provides

  • you need a simpler syntax for manipulating regular expressions

  • you want to quickly bridge to code you’ve already written in Ruby

Quickstart

In this example, we will use Watir (http://wtr.rubyforge.org), a high level web application testing Gem that has no equivalent in Python. First, you need to declare a Ruby context that will run the Ruby code.:

import rython

ctx = rython.RubyContext(requires=["rubygems", "watir"])

Next, instantiate a Watir::Browser object and assign it to a Python variable. The object will be an instance of rython.RubyProxy:

my_browser = ctx("Watir::Browser.new")

assert isinstance(my_browser, rython.RubyProxy)

And now we can call any method on the object instance as well. This example grabs a DIV element from the page by CSS ID ‘foobar’. We can then execute more Ruby methods on the div_element if we want:

div_element = my_browser("div(:id, 'foobar')")

assert isinstance(div_element, rython.RubyProxy)

exists_in_browser = div_element("exists?")

Advanced

There are deeper features you can take advantage of in Rython.

Calling methods with RubyProxy arguments

Let’s say you have a Ruby method that takes a complex Ruby object. This object will be proxied in the Python context as a RubyProxy. To pass this object to a method, simply use the repr string substitution. The following example passes a RubyProxied Browser object to a method:

my_browser = ctx("Watir::Browser.new")
my_foobar = ctx("Foobar.new")

my_foobar("method_that_takes_browser(%(browser)r)", browser=my_browser)

Rython will automatically convert a RubyProxy argument to the appropriate Ruby expression that refers to the object in the Ruby context.

Apply monkeypatches to the context

When instantiating a RubyContext, you can specify a ‘setup’ parameter that contains raw Ruby code to execute after doing the require statements. This is perfect for adding monkeypatches to Ruby objects, or just performing additional setup.

This example monkeypatches the String object with a ‘to_safe_string’ method. This makes it easy to convert all strings into printable characters:

import rython

monkeypatches = '''
    class String
        GOOD_NONUNICODE_CHARS = (("A".."Z").to_a + ("a".."z").to_a + ("0".."9").to_a).to_a
        def to_safe_string
            final = ""
            each_char do |ch|
                final += "#{ch}" if GOOD_NONUNICODE_CHARS.include? ch
            end
            final
        end
    end
    '''

ctx = rython.RubyContext(setup=monkeypatches)

Running the context in debug mode

This is helpful when Rython appears to be failing due to problems in the Ruby context. By default, all logging messages are suppressed from the Ruby context. You can reenable them by setting the debug flag:

import rython

ctx = rython.RubyContext(debug=True)

Metadata

Release files for rython 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rython 0.0.1
File Size Uploaded
rython-0.0.1.tar.gz 7.7 kB Details

Release files / rython-0.0.1.tar.gz

Download URL rython-0.0.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2c18b6b40a99b10262b57048c50aa008ca6a03bc935db65bca211a70d1dc2f35
BLAKE2b-256 checksum
How to use checksums
9bb782ce015d732a10ef46417726ae66e314b14655402ad1729ffada7051dcec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page