Writing a polyglot script

栏目: IT技术 · 发布时间: 6年前

内容简介:So here's the thing - I'm relatively good at Python. I somewhat know Ruby. Not good enough to write something complex, but enough to understand that it's "a bit" different from Python. Yet both of the languages have similar syntaxes, which made me think wh

So here's the thing - I'm relatively good at Python. I somewhat know Ruby. Not good enough to write something complex, but enough to understand that it's "a bit" different from Python. Yet both of the languages have similar syntaxes, which made me think whether I could come up with a script that's valid in both languages.

The shortest valid script could be:

print("hello world")

I thought that was too easy, so I set a goal to write a script that would print the exact language used. After a bit of tinkering, I came up with this:

print(["ruby", "python"][(0 or 1)])

I've remembered that one of the main surprises the developers who switched from one language to another encountered was the fact that 0 is falsey in Python, whereas in Ruby it is truthy . So the expression 0 or 1 evaluates to 0 and 1 in Ruby and Python, accordingly. And if we use just that to get the value at the corresponding index of the specially crafted list, we can achieve the given task. As a side note, we would need to enclose the boolean expression in parentheses, because otherwise it'll throw a syntax error in Ruby.

I could have settled here, but I decided to raise the bar higher. What if I try to come up with a script that could do more sophisticated things than just printing some values? Now that's where things get more complicated, since you can't just do anything in one languages that would be also correct in another. But I've finally managed to come up with a somewhat decent solution with a few neat tricks:

(0 and eval("""
3.times {
  puts 'ruby code goes here'
}
true
""")) or eval(compile("""
for i in range(3):
    print('python code goes here')
""", "", "exec"))

So what's happening in here:

  1. The code above is a single statement of the form (0 and <expr1>) or <expr2> . Both Python and Ruby support short-circuiting for boolean expressions, therefore expr1 evaluates in Ruby and expr2 in Python only.
  2. It heavily relies on metaprogramming, by evaluating only a single function that could contain more complex code inside a string. Ruby's eval needs to have a truthy value as the last statement, which is returned as the function's value. If we fail to do so, Ruby will try to execute expr2 . For Python we could have used expr , but I realised that it's not available in Python 2 (which had not reached the EOL at the time when I was experimenting, so as a well-mannered Pythonista I had to support it as well).
  3. Using triple-quote strings is a nice addition to write more readable code without providing escape sequences for newlines where necessary. In Python it's the only way to create multiline strings. Ruby, on the other hand, supports multiline strings by default with single and double quoted strings, and treats """foo""" as three separate strings "" "foo" "" which then get concatenated to one. This, however, makes using double (or single) quote characters tricky in the code placeholders.

以上所述就是小编给大家介绍的《Writing a polyglot script》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

跟小贤学运营

跟小贤学运营

陈维贤 / 机械工业出版社 / 2016-12-9 / 69.00

这是一部能帮助运营新人快速构建互联网运营方法论和快速掌握互联网运营实操的著作,是小贤在百度贴吧和小红书成长经历和运营经验的复盘。书中包含5大运营主题、40余种运营工具和渠道、50余种运营方法和技巧、100余个真实接地气的运营案例,能迅速帮助运营新人掌握全套实操技能和构建完整运营体系。 本书的视角和知识体系都比较立体化: 既有百度这样的互联网巨头运营规范和思路,又有小红书这样的明星创业公......一起来看看 《跟小贤学运营》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具