Python vs. Ruby1
To Ruby From Python
Python vs. Ruby: Comparing Two Top Scripting Languages
Python has list/dict generators
1 |
res = [x*x for x in range(1, 10)] |
Ruby’s is less pretty, but more functional-style:
1 |
res = (0..9).map { |x| x * x } |
Ruby is more functional
1 |
myList.map(&:description).reject(&:empty?).join("\n") |
Reversing words in a sentence:
1 |
sentence.split.reverse.join ' ' |
Python:
1 2 |
descriptions = (f.description() for f in mylist) "\n".join(filter(len, descriptions)) |
Reversing words in a sentence:
1 |
" ".join(reversed(sentence.split())) |
Python has generators
1 2 3 |
def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] |
Ruby has a class reference in the class body
In Python you don’t have a reference to the class until after the class construction is finished.
1 2 3 |
class Foo puts self end |
Ruby has blocks2
1 2 3 4 5 6 7 8 9 10 11 12 13 |
=begin Ruby Code blocks are chunks of code between braces or between do..end that you can associate with method invocations =end def call_block puts 'Start of method' # you can call the block using the yield keyword yield yield puts 'End of method' end # Code blocks may appear only in the source adjacent to a method call call_block {puts 'In the block'} |
The output is:
1 2 3 4 |
Start of method In the block In the block End of method |
Python has multiple inheritance, Ruby doesn’t
Ruby has Perl-like first-class regular expressions
1 2 3 |
if ( line =~ /^INFO(.*)/ ) puts "line begins with INFO" end |
(But they aren’t exactly the same!)
Python vs. Perl
See the Perl Docs for Regular Expressions and the Quote-Like Operators.
JavaScript vs. Dart
From Seth Ladd’s blog: I ported a JavaScript app to Dart. Here’s what I learned.