views:

162

answers:

1

Just for interest really - community wiki - how much Python can we get Ruby to understand ? [ Probably be just as interesting to do the reverse as well].

The experiment (such as it is) perhaps to see how much can be written in Ruby-Cross-Python scripts that will result in identical outputs. The only 'cheat' I guess being allowed here is the too allow Ruby statements to precede the eventual 'common' script. [like the 'len' definition below].

For instance, this works in both:

a=[1,2,3]
mystring="hello"
bobby="hello"*3
epoch=1270123200
map={}
map['supamu']='egusu'
map['dedu']='paroto'

keys=map.keys()
values=map.values()

And doing this in Ruby:

class Object
    def len(object)
        object.size
    end
end

class Object
    def str(object)
        object.to_s
    end
end

Means this now works in both:

a=[1,2,3]
mystring="hello"
len(a)
len(mystring)
str(123)

I think the trouble will come with conditionals and loops, due to different syntax requirements. (the ':' at the end of Python lines for instance...)

+1  A: 

To answer the question fully would probably need a bit of analysis. The control structures in both languages are defined to make our lives easier and the codes more readable when we program, but they could be realised with methods like it was in smalltalk. Iterations and conditionals (except the "case" statement) similarly as it was described in this blog: Emulating Smalltalk’s Conditionals in Ruby. For example an "if" would look something like this:

(an_object.nil?).
 if_true  { puts "true" }.
 if_false { puts "false" }

I would suggest to cope with the iteration and conditionals by creation of an extension of the objects in both languages, which define the control structures as methods. This way it would possible to write a code with control structures which run in both languages.

But there other things, which lays deeper in the language design, which would be harder to cope with. In python every instance variable is public, in ruby they are private. What are the differences in changing self..?

Maybe if we started to reduce and omit the syntactical things to method calls we would end up with a very simple syntax... with something similar to smalltalk.

fifigyuri