Writing a polyglot script

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

深度探索Linux操作系统

深度探索Linux操作系统

王柏生 / 机械工业出版社 / 2013-10-15 / 89.00

《深度探索linux操作系统:系统构建和原理解析》是探索linux操作系统原理的里程碑之作,在众多的同类书中独树一帜。它颠覆和摒弃了传统的从阅读linux内核源代码着手学习linux操作系统原理的方式,而是基于实践,以从零开始构建一个完整的linux操作系统的过程为依托,指引读者在实践中去探索操作系统的本质。这种方式的妙处在于,让读者先从宏观上全面认清一个完整的操作系统中都包含哪些组件,各个组件的......一起来看看 《深度探索Linux操作系统》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换