Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. # Python to Ruby
  2. ## functions
  3. ### define
  4. ~~~python
  5. # Python
  6. def add3(x1, x2, x3):
  7. return x1 + x2 + x3
  8. ~~~
  9. ~~~ruby
  10. # Ruby
  11. def add3(x1, x2, x3)
  12. x1 + x2 + x3
  13. end
  14. ~~~
  15. ----------
  16. ~~~python
  17. # Python
  18. add3(1, 2, 3)
  19. ~~~
  20. ~~~ruby
  21. # Ruby
  22. add3(1, 2, 3)
  23.  
  24. # parens are optional:
  25. add3 1, 2, 3
  26. ~~~
  27. ----------
  28. ~~~python
  29. # Python
  30. import math
  31.  
  32. def my_log(x, base=10):
  33. return math.log(x) / math.log(base)
  34.  
  35. my_log(42)
  36. my_log(42, math.e)
  37. ~~~
  38. ~~~ruby
  39. # Ruby
  40. def my_log(x, base=10)
  41. Math.log(x) / Math.log(base)
  42. end
  43.  
  44. my_log(42)
  45. my_log(42, Math::E)
  46. ~~~
  47. ----------
  48. ~~~python
  49. # Python
  50. def first_and_last(*a):
  51.  
  52. if len(a) >= 1:
  53. print('first: ' + str(a[0]))
  54.  
  55. if len(a) >= 2:
  56. print('last: ' + str(a[-1]))
  57. ~~~
  58. ~~~ruby
  59. # Ruby
  60. def first_and_last(*a)
  61.  
  62. if a.size >= 1
  63. puts "first: #{a[0]}"
  64. end
  65.  
  66. if a.size >= 2
  67. puts "last: #{a[-1]}"
  68. end
  69. end
  70. ~~~
  71. ----------
  72. ~~~python
  73. # Python
  74. a = [2, 3]
  75.  
  76. add3(1, *a)
  77. ~~~
  78. ~~~ruby
  79. # Ruby
  80. a = [2, 3]
  81.  
  82. add3(1, *a)
  83. ~~~
  84. ----------
  85. ~~~python
  86. # Python
  87. def fequal(x, y, eps=0.01):
  88. return abs(x - y) < eps
  89.  
  90. fequal(1.0, 1.001)
  91. fequal(1.0, 1.001, eps=0.1**10)
  92. ~~~
  93. ~~~ruby
  94. # Ruby
  95. def fequal(x, y, opts={})
  96. eps = opts[:eps] || 0.01
  97. (x - y).abs < eps
  98. end
  99.  
  100. fequal(1.0, 1.001)
  101. fequal(1.0, 1.001, :eps=>0.1**10)
  102.  
  103. # Ruby 2.0:
  104. def fequals(x, y, eps: 0.01)
  105. (x - y).abs < eps
  106. end
  107.  
  108. fequals(1.0, 1.001)
  109. fequals(1.0, 1.001, eps: 0.1**10)
  110. ~~~
  111. ----------
  112. ~~~python
  113. # Python
  114. def first_and_second(a):
  115. return a[0], a[1]
  116.  
  117. x, y = first_and_second([1, 2, 3])
  118. ~~~
  119. ~~~ruby
  120. # Ruby
  121. def first_and_second(a)
  122. return a[0], a[1]
  123. end
  124.  
  125. x, y = first_and_second([1, 2, 3])
  126. ~~~
  127. ----------
  128. ~~~python
  129. # Python
  130. # body must be an expression:
  131. sqr = lambda x: x * x
  132. ~~~
  133. ~~~ruby
  134. # Ruby
  135. sqr = lambda { |x| x * x }
  136. ~~~
  137. ----------
  138. ~~~python
  139. # Python
  140. sqr(2)
  141. ~~~
  142. ~~~ruby
  143. # Ruby
  144. sqr.call(2) or
  145. sqr[2]
  146. ~~~
  147. ----------
  148. ~~~python
  149. # Python
  150. func = add
  151. ~~~
  152. ~~~ruby
  153. # Ruby
  154. func = lambda { |*args| add(*args) }
  155. ~~~
  156. ----------
  157. ~~~python
  158. # Python
  159. # state not private:
  160. def counter():
  161. counter.i += 1
  162. return counter.i
  163.  
  164. counter.i = 0
  165. print(counter())
  166. ~~~
  167. ~~~ruby
  168. # Ruby
  169. none
  170. ~~~
  171. ----------
  172. ~~~python
  173. # Python 3:
  174. def make_counter():
  175. i = 0
  176. def counter():
  177. nonlocal i
  178. i += 1
  179. return i
  180. return counter
  181.  
  182. nays = make_counter()
  183. ~~~
  184. ~~~ruby
  185. # Ruby
  186. def make_counter
  187. i = 0
  188. return lambda { i +=1; i }
  189. end
  190.  
  191. nays = make_counter
  192. puts nays.call
  193. ~~~
  194. ----------
  195. ~~~python
  196. # Python
  197. # The itertools library contains
  198. # standard generators.
  199. # c.f. itertools.count()
  200.  
  201. def make_counter():
  202. i = 0
  203. while True:
  204. i += 1
  205. yield i
  206.  
  207. nays = make_counter()
  208. print(nays.next())
  209. ~~~
  210. ~~~ruby
  211. # Ruby
  212. def make_counter
  213. return Fiber.new do
  214. i = 0
  215. while true
  216. i += 1
  217. Fiber.yield i
  218. end
  219. end
  220. end
  221.  
  222. nays = make_counter
  223. puts nays.resume
  224. ~~~
  225. ----------
  226. ~~~python
  227. # Python
  228. def logcall(f):
  229. def wrapper(*a, **opts):
  230. print('calling ' + f.__name__)
  231. f(*a, **opts)
  232. print('called ' + f.__name__)
  233. return wrapper
  234.  
  235. @logcall
  236. def square(x):
  237. return x * x
  238. ~~~
  239. ~~~ruby
  240. # Ruby
  241. ~~~
  242. ----------
  243. ~~~python
  244. # Python
  245. import operator
  246.  
  247. operator.mul(3, 7)
  248.  
  249. a = ['foo', 'bar', 'baz']
  250. operator.itemgetter(2)(a)
  251. ~~~
  252. ~~~ruby
  253. # Ruby
  254. 3.*(7)
  255.  
  256. a = ['foo', 'bar', 'baz']
  257. a.[](2)
  258. ~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement