ruby to python translator

Is it possible?
I think it is.

One thing that may help us in this quest would be to use a few simplifying assumptions:
1) at some point in time the code will be “frozen” [i.e. no new methods defined, no more eval will be called].
2) procs won’t have “#eval” called on them–rarely used, anyway, I’d imagine.

Now our first task is to attempt to translate blocks into something that Python can handle. Functions.

In Ruby we have


def go
 yield 3
 print "here"

end

def start
  go {|arg| print "in block with arg" + arg.to_s }
end

start

In Ruby using functions only [which we’ll assume are translatable to Python].


def go method_to_call_and_where
 method_to_call, where = method_to_call_and_where
 where.send method_to_call, 3
 print "here"

end

def block1 arg
 print "in block with arg" + arg.to_s
end

def start

 go [:block1, self]
end

start

Both output the same thing:
“in block with arg3here”
Now a more complex example, with local variables


def go
 yield 3
 print "here"

end

def start
  local_var = 4
  go {|arg| print "in block with local_var currently #{local_var}"; local_var = 5 }
  print "local var is now #{local_var}"
end

start

And its function equivalent [remember that with ParseTree we can rewrite things this way].


def go method_to_call_and_where
 method_to_call, where, shared_locals = method_to_call_and_where
 where.send method_to_call, shared_locals # yield
 print "here"

end

def block1 shared_locals
 print "in block with local_var currently #{shared_locals[:local_var]}"
 shared_locals[:local_var] = 5
end

def start
 shared_locals = {}
 shared_locals[:local_var] = 4 # initialize
 go [:block1, self, shared_locals]
 print "local var is now #{shared_locals[:local_var]}"
end

start

They both output: “in block with local_var currently 4herelocal var is now 5”

So as long as we assume that blocks won’t be called later, we can actually save away a block as an array of [:self, :method_name, :shared_locals].

Note that you can also simulate a “return” from a block by raising a unique class to that Block and rescuing it [and returning] at that point.
Seems to me that blocks can easily be translated to Python [well, almost easily]. How hard can the rest be…

10 thoughts on “ruby to python translator”

  1. link:
    https://www.pypi.info/index.php?option=com_content&view=article&catid=55:python-resources&id=220:python-resources-05-06-2008-01&Itemid=171
    could use unholy -> python -> decompyle -> Python [whoa hard! hard hard!]

    _why on unholy:
    Instead of asking “can unholy run Rails?” try asking “can unholy run 4 plus 7?” And my answer to that is: no, no, no, no, it probably can’t!!

    someone else on this type of project [from same page]:
    Ruby —> Python —> Effort City —> Bizarre Software State

  2. from mentalguy:
    But I think the main thing for practical use is going to be figuring out how to deliver, package, and use the Ruby core classes and libraries in the context of a Python runtime environment.

    that is quite possibly what we want here…

  3. may need something like:
    thread[level][var] for the constants, but…I don’t think so.

    There’s a problem of translating existing proc’s into Python–assume there won’t be any 🙂

  4. First step:
    simple simple progs -> Python 🙂

    Second step:
    somehow step through and convert a single AR query to Python. A simple one. Compare speeds.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.