Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python to Ruby
- ## functions
- ### define
- ~~~python
- # Python
- def add3(x1, x2, x3):
- return x1 + x2 + x3
- ~~~
- ~~~ruby
- # Ruby
- def add3(x1, x2, x3)
- x1 + x2 + x3
- end
- ~~~
- ----------
- ~~~python
- # Python
- add3(1, 2, 3)
- ~~~
- ~~~ruby
- # Ruby
- add3(1, 2, 3)
- # parens are optional:
- add3 1, 2, 3
- ~~~
- ----------
- ~~~python
- # Python
- import math
- def my_log(x, base=10):
- return math.log(x) / math.log(base)
- my_log(42)
- my_log(42, math.e)
- ~~~
- ~~~ruby
- # Ruby
- def my_log(x, base=10)
- Math.log(x) / Math.log(base)
- end
- my_log(42)
- my_log(42, Math::E)
- ~~~
- ----------
- ~~~python
- # Python
- def first_and_last(*a):
- if len(a) >= 1:
- print('first: ' + str(a[0]))
- if len(a) >= 2:
- print('last: ' + str(a[-1]))
- ~~~
- ~~~ruby
- # Ruby
- def first_and_last(*a)
- if a.size >= 1
- puts "first: #{a[0]}"
- end
- if a.size >= 2
- puts "last: #{a[-1]}"
- end
- end
- ~~~
- ----------
- ~~~python
- # Python
- a = [2, 3]
- add3(1, *a)
- ~~~
- ~~~ruby
- # Ruby
- a = [2, 3]
- add3(1, *a)
- ~~~
- ----------
- ~~~python
- # Python
- def fequal(x, y, eps=0.01):
- return abs(x - y) < eps
- fequal(1.0, 1.001)
- fequal(1.0, 1.001, eps=0.1**10)
- ~~~
- ~~~ruby
- # Ruby
- def fequal(x, y, opts={})
- eps = opts[:eps] || 0.01
- (x - y).abs < eps
- end
- fequal(1.0, 1.001)
- fequal(1.0, 1.001, :eps=>0.1**10)
- # Ruby 2.0:
- def fequals(x, y, eps: 0.01)
- (x - y).abs < eps
- end
- fequals(1.0, 1.001)
- fequals(1.0, 1.001, eps: 0.1**10)
- ~~~
- ----------
- ~~~python
- # Python
- def first_and_second(a):
- return a[0], a[1]
- x, y = first_and_second([1, 2, 3])
- ~~~
- ~~~ruby
- # Ruby
- def first_and_second(a)
- return a[0], a[1]
- end
- x, y = first_and_second([1, 2, 3])
- ~~~
- ----------
- ~~~python
- # Python
- # body must be an expression:
- sqr = lambda x: x * x
- ~~~
- ~~~ruby
- # Ruby
- sqr = lambda { |x| x * x }
- ~~~
- ----------
- ~~~python
- # Python
- sqr(2)
- ~~~
- ~~~ruby
- # Ruby
- sqr.call(2) or
- sqr[2]
- ~~~
- ----------
- ~~~python
- # Python
- func = add
- ~~~
- ~~~ruby
- # Ruby
- func = lambda { |*args| add(*args) }
- ~~~
- ----------
- ~~~python
- # Python
- # state not private:
- def counter():
- counter.i += 1
- return counter.i
- counter.i = 0
- print(counter())
- ~~~
- ~~~ruby
- # Ruby
- none
- ~~~
- ----------
- ~~~python
- # Python 3:
- def make_counter():
- i = 0
- def counter():
- nonlocal i
- i += 1
- return i
- return counter
- nays = make_counter()
- ~~~
- ~~~ruby
- # Ruby
- def make_counter
- i = 0
- return lambda { i +=1; i }
- end
- nays = make_counter
- puts nays.call
- ~~~
- ----------
- ~~~python
- # Python
- # The itertools library contains
- # standard generators.
- # c.f. itertools.count()
- def make_counter():
- i = 0
- while True:
- i += 1
- yield i
- nays = make_counter()
- print(nays.next())
- ~~~
- ~~~ruby
- # Ruby
- def make_counter
- return Fiber.new do
- i = 0
- while true
- i += 1
- Fiber.yield i
- end
- end
- end
- nays = make_counter
- puts nays.resume
- ~~~
- ----------
- ~~~python
- # Python
- def logcall(f):
- def wrapper(*a, **opts):
- print('calling ' + f.__name__)
- f(*a, **opts)
- print('called ' + f.__name__)
- return wrapper
- @logcall
- def square(x):
- return x * x
- ~~~
- ~~~ruby
- # Ruby
- ~~~
- ----------
- ~~~python
- # Python
- import operator
- operator.mul(3, 7)
- a = ['foo', 'bar', 'baz']
- operator.itemgetter(2)(a)
- ~~~
- ~~~ruby
- # Ruby
- 3.*(7)
- a = ['foo', 'bar', 'baz']
- a.[](2)
- ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement