[RUBY] Bilingual Hello World.py.rb

What is Bilingual Hello World?

Bilingual Hello World is the source code of Hello, World! That meets the following conditions (named by the author. Probably a similar one has already appeared, but I could not find it by searching).

Fourth, supplementary explanation may be needed. This rule is to disallow the following solutions.

#Bilingual Python and Ruby?
print("Hello, World!")

Sure, this code works for both Python and Ruby, and when run it shows Hello, World!. But only you do not have the scalability degree to change the character string to be at best output to Hello world in this (not interesting than anything else). It's named "Hello World" for convenience, but we aim to make it happen in a way that naturally extends to his FizzBuzz.

Here is an example that was actually devised in the next section.

hello_world.py.rb

Bilingual who can speak both Python and Ruby. I'll post it twice because I want to specify syntax highlighting, but of course it's the same.

As Python

""""
=begin
"""
# Python
print("Hello, World!")
""""
=end
# Ruby
puts "Hello, World!"
"""
""""
"""

** Explanation: **

The first three " "" on the first line are the start tokens of * triple-quoted string *. The last""on the first line is part of that string literal. The second line continues, and the literal ends with " "" on the third line. Lines 4-5 are just mundane Python code. The sixth line, like the first line, starts a triple-quoted string, the first character of which is " . The literal continues from line 7 to line 9, ending at line 10. The 11th and 12th lines also have triple-quoted strings written as before.

In summary, this code has the following structure:

  1. A triple-quoted string whose content is " \ n = begin \ n
  2. Python code
  3. A triple-quoted string whose content is " \ n = end \ n # Ruby \ nputs "Hello, World!" \ N
  4. A triple-quoted string whose content is " \ n

In Python these string literals are simply ignored, resulting in the execution of only the 2. part of the Python code.

As Ruby

""""
=begin
"""
# Python
print("Hello, World!")
""""
=end
# Ruby
puts "Hello, World!"
"""
""""
"""

** Explanation: **

The first line is a series of two empty string literals. Equal to " "" " with spaces in between for clarity. If you put string literals next to each other in Ruby, they will be combined (this is the same in Python). Ruby multi-line comments from the 2nd line = begin to the 7th line = end. Lines 8 and 9 are ordinary Ruby Hello Worlds. The 10th to 12th lines can be interpreted in the same way as the 1st line. There are a total of 10 " `, so there are 5 string literals in a row.

In summary, this code has the following structure:

  1. Empty string literal
  2. Multi-line comment
  3. Ruby code
  4. A string literal whose content is \ n \ n

Even in Ruby, these string literals are simply ignored, resulting in the execution of only the 3. part of the Ruby code.

fizzbuzz.py.rb

Let's try FizzBuzz for the problem to see the first extensibility rule.

As Python

""""
=begin
"""
# Python
for i in range(100):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
""""
=end
# Ruby
100.times do |i|
  case
  when i % 15 == 0
    puts "FizzBuzz"
  when i % 3 == 0
    puts "Fizz"
  when i % 5 == 0
    puts "Buzz"
  else
    puts i
  end
end
"""
""""
"""

As Ruby

""""
=begin
"""
# Python
for i in range(100):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
""""
=end
# Ruby
100.times do |i|
  case
  when i % 15 == 0
    puts "FizzBuzz"
  when i % 3 == 0
    puts "Fizz"
  when i % 5 == 0
    puts "Buzz"
  else
    puts i
  end
end
"""
""""
"""

hello_world.rb.py (another pattern)

There are many possible alternatives, but I'll list one as a pattern that doesn't use = begin and = end (explanation omitted). This can also be expanded beyond Hello World in the same way.

As Ruby

"""
#{(
# Ruby
puts "Hello, World!"
)}
"""
"""
"; <<EOS
"""
# Python
print('Hello, World!')
"""
EOS
"""
""""
"""

As Python

"""
#{(
# Ruby
puts "Hello, World!"
)}
"""
"""
"; <<EOS
"""
# Python
print('Hello, World!')
"""
EOS
"""
""""
"""

Supplement: Q. Isn't "a person who speaks words that can be interpreted as two different languages" not called bilingual? A. Yes, I think so too. I didn't know the proper word to describe inside the brackets.

Recommended Posts

Bilingual Hello World.py.rb
Hello RSpec