-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ruby-vs-python.txt
99 lines (89 loc) · 5.03 KB
/
ruby-vs-python.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# ruby +++ better blocks + 'full' lambdas !
# ruby ++ if expression works for blocks
# ruby ++ much more forgiving and convenient, i.e. [1]+[2]=[1,2]
# ruby ++ nicer string templates "Variable #{type} #{name}=#{value}"
# // ruby + jruby better, class extensions
# // ruby + mirah: native 'ruby' on jvm
# ruby + def 'properties' def y; @y; end def y=(z); @y=z; end , see (ugly) attr_reader ...
# ruby + clean function approach, e.g. def []=(index,values) ...
# ruby + nicer 'and'+'or' concatenations a?b:c or d and raise Ex
# ruby +- less strict z=map[x] vs if x in map: map[x]
# ruby - less modules, more beautiful language => less need for angle!
# ruby - methods less 1st class citizens than in python! ('ok' via method(:symbol), send)
# ruby - gems are a mess compared to python
# ruby - puts a, b newlines
# ruby -: can't access variables in methods without '@' !!
# ruby -- NO types/type hints!! (vs py3)
# ruby -- no annotations!
# ruby --- no nice dynamic programmatic properties:
a.x=8 NoMethodError: undefined method `x='
WORKAROUNDS:
OpenStruct: def a.x; 8;end
OR a.methods << def x; 8;end (WOW)
OR class << a; attr_accessor :x; end and a.x=8
OR A.send(:attr_accessor, :x) and a.x=8 #CLASS!
OR A.class.send(:define_method, :x, lambda{8}) #CLASS!
OR sometimes method_missing
OR lol: http://www.infoq.com/articles/properties-metaprogramming
# http://rosettacode.org/wiki/Add_a_variable_to_a_class_instance_at_runtime#C.23
# dynamic a = new System.Dynamic.ExpandoObject();
# a.x = 1; // Adding a new property
# java: https://stackoverflow.com/questions/6680674/can-a-java-class-add-a-method-to-itself-at-runtime =>
# http://www.jboss.org/javassist https://github.com/jboss-javassist/javassist
# python ++: more modules
# python ++: dynamic properties
# python ++: ironpython ok!
# python ++: type hints (py3)
# python ++: annotations
# python ++: TO/FRO bytecode, NATIVE (sometimes)
# python ++: Ast better integrated(!!) + cleaner!! + compile,exec,pyc emitter!!
# python ++: Constructors() interchangeable with methods, can be very handy someday, and nice
# python ++: can access variables in methods without '@' !!
# python +: list concatenation
# python +: named parameters
# python +: py2exe
# python +: ternary operator ok = 'Yes' if condition == 'OK' else 'No'
# python +: better reflection + docs (dir, help)
# python +-: functions proper(!) 1st class citizens [problem: () not default!]
# python +-: properties ok'ish: p=property(getter,setter)
# python +: 'in; operator
# python +: LISP dropin syntax https://github.com/hylang/hy
# python +: stricter: i.e. ruby z=map[x] (NIL) vs if x in map: map[x]
# python +-: python 'run' is significantly faster than slooow 'debug' mode
# python -: bizzare super(ast.ClassDef,self).__init__(*kwargs)
# python -- LESS FORGIVING! ValueError: list.remove(x): x not in list , if string.index(x): ValueError: substring not found ETC ETC
# python -- missing SO MANY FUNCTIONS!
# python - unsupported operand type(s) for -: 'list', AttributeError: 'list' object has no attribute 'each' ETC **
# python list,string,... not extendable but OK in angle wrapper xlist!
# python (-) backtrace ?
# python (-) no switch/Case
# python -- AST debugging painful: AST identifier must be of type str (… which one?)
# python - annoying self. (self, : , () -> DANGEROUS!! (ok if generated?)
# python - NO class extensions for builtins! (ok with 'macros' / subclass construction (?))
# python -- stupid list arg explosion def f(*args):pass f(1,2,3) OK l=[1,2,3] f(*l) OK f([1,2,3]) TUPLE([1,2,3]) WTF!
# python -- running angle ok, debugging SLOOOOOW! (same algorithm as ruby, maybe caller() is expensive?)
# python -- STATEMENT RESULTS IGNORED!! REPL >>> 3*3 => 9 >>> _*2 => 18 OK >>> x=2 >>> _ 18 # WAAAAAAHHHHHHHH!!!!!!!!!!!!!!!!!
# python ---- variable scope nightmare!! lambdas + block variable scope !!!!! 'nonlocal' keyword :(
# python ---- explicit global/class(self) self.EVERYTHING!!!
more: http://c2.com/cgi/wiki?PythonProblems
# both -- a.z missing properties throw: undefined method `spec' AttributeError: 'dict' object has no attribute 'z'
# both + bytecode (mruby)
# both ++ mixins python:class Request(BaseRequest, AcceptMixin), ruby: include AcceptMixin
# both ++ alias neu alt python: neu=alt def neu(): alt()
# PyPy Python written in Python, Ruby has Rubinius
# Building PyPy yourself is not recommended. It is time-consuming and requires significant computing resources.
# What separates a mixin from multiple inheritance? Is it just a matter of semantics?
Yes.
** ValueError: list.remove(x): x not in list
** RUBY: # (the.nodes - old_nodes).each{|n| n.invalidate}
** PYTHON: -- for old in old_nodes:
if old in nodes:
nodes.remove(old)
the.nodes.each(lambda n: n.invalidate())
AttributeError: 'list' object has no attribute 'each' WTF -=->
for n in the.nodes: n.invalid()
def dispatch printType(Void x) {
"it's null"
}
to print an empty car:
"it's null"