Code Translation between languages - Text Generator API
transfer code between languages with Text Generator API - Python to Ruby code examples
Example input
-
Python: list[::-1] Ruby: list.reverse Python: list[1:4] Ruby: list[1..4] Python: print("Hello World") Ruby: puts "Hello World" Python: fruits = ["apple","banana","cherry"] for x in fruits: print(x) Ruby: fruit = ["apple", "banana", "cherry"] each {|x| print x } Python: fruits = ["apple","banana","cherry"] a = list(fruits) print(a) a.reverse() print(a) Ruby:
Example real generated results
-
star
fries = ["apple","banana","cherry"] a = [] a << fries puts a a.unshift fries puts a a += fries puts a a -= fries puts a a &= fries puts a a |= fries puts a a ^= fries puts a a *= fries puts a a /= fries puts a a %= fries
-
star
fruit = ["apple", "banana", "cherry"] each { |x| puts x if (x == fruit) } each { |x| puts x if (x > 0 and x < 4) } each { |x| puts x if (x >= 1 and x <= 2) } each { |x| puts x if (x % 5 == 0) } each { |x| puts x if (x % 3 == 0) }
-
star
# this will only work if all of your fruits have the same size. puts fruit.join(' '); puts "" unless fruits.size == 1 Ruby (with no warnings from Ruby's compiler or interpreter) # This is a good example to illustrate how you can use lists and arrays arr = [-1,1,2,3] arr[0] arr[-1] arr[2] arr[-3] arr[4] arr[4][2] arr[:1] arr.delete
-
star
fruit = ["apple", "banana", "cherry"] each {|x| puts x } a = fruit.dup a.reverse each {|x| puts x } Python: a = 5 b = a + 4 c = b - 2 d = c * 3 e = d / 2 f = e % 10 g = f * 1 h = g ** 0.5 i = h * 1.1
try it yourself: Text Generator Playground - Code Translation between languages - Text Generator API
Sign up