SlideShare a Scribd company logo
1 of 47
Download to read offline
What Ruby can learn 
from Python 
(and vice versa) 
Reuven M. Lerner, PhD • reuven@lerner.co.il 
Rails Israel 2014 
November 4th, 2014
Who am I? 
• Programmer, consultant, developer, trainer 
• Long-time Ruby, Python user 
• Linux Journal columnist 
• Author, Practice Makes Python! 
• PhD in Learning Sciences from Northwestern 
• Read (lots) more at http://lerner.co.il/ 
2
Python, Ruby, and me
Sapir-Whorf Hypothesis 
"Cognitive processes, such as thought and 
experience, may be influenced by the categories and 
patterns of the language a person speaks." — 
Wikipedia 
4
Or: 
You are what you speak! 
5
Learning a language 
helps you not only communicate, 
but also to think in new ways 
6
7
你有叉⼦子吗? 
Nǐ yǒu chāzi ma? 
Do you have a fork? 
8
Possible answers 
• "Yes" 
• "No" 
• "What's a fork?" 
• "Your accent is so bad, I have no idea what you're 
saying" 
9
The actual answer? 
• None of the above! 
• Chinese doesn't have words for "yes" and "no." 
10
你有叉⼦子吗? 
Nǐ yǒu chāzi ma? 
Do you have a fork? 
Answer: 有 (yǒu) 
"Have" 
11
Can you think of 
another language that 
works this way? 
12
בראשית כ״ט, ו׳ 
ויאמר, ״השלום לו?״ 
ויאמרו ״שלום.״ 
Genesis 29:6 
And he said, "Is he well?" 
And they said, "Well!" (i.e., "Yes!") 
13
And also… 
CREATE TABLE Foo (id SERIAL, stuff TEXT); 
CREATE TABLE 
INSERT INTO Foo (stuff) VALUES ('abc'); 
INSERT 0 1 
14
Comparing languages 
• Appreciate each one more 
• Reflect on the relative trade-offs 
15
Ruby and Python both… 
• … dynamically and strongly typed 
• … are JIT byte-compiled 
• … are cross-platform 
• … are widely used for a variety of tasks 
• … popular among Web developers 
• … have multiple implementations 
• … have an object system based (in part) on Smalltalk 
16
And yet 
• Python and Ruby feel very different 
• What can we learn from these differences? 
• What can we learn from the Python world? (And 
what can they learn from us?) 
17
General attitude 
• Ruby wants to give you freedom, to allow you to be 
creative and flexible, as in a natural language 
• TMTOWTDI, POLS 
• Python wants to restrict you, for your own good 
• "There should be one — and preferably only one 
— obvious way to do it." 
18
The result? 
• Python is 
• easier to learn 
• easier to debug and maintain 
• Ruby 
• offers you richer options 
• greater expressiveness 
19
Development process 
• Python Enhancement Proposals (PEPs) 
• Ruby could use such a system 
• Heck, Rails could use such a system 
20
Backward compatibility 
• Historically, Python upgrades were conservative 
• But not 2.x -> 3.x 
• Ruby was less conservative, breaking things 
(especially going from 1.8 to 1.9/2.0) 
• Maybe Python emphasis on compatibility and 
inclusiveness hurt the 3.x upgrade 
21
What is truth? 
• Ruby: Everything is true, except for false and nil 
• Python: Everything is true, except for None, False, 
0, and empty data (e.g., '' or []) 
• So: 0 is false in Python, but true in Ruby! 
• But we all like #blank? and #present? in Rails… 
22
Everything is an object… 
• In both Ruby and Python, everything is an object! 
• But in Python, some objects are more equal than 
others… 
• So you can't do this in Python: 
5.times { puts "Hello" } 
23
Blocks 
• Ruby people love blocks! 
• Python doesn't have them. 
• They create functions, and pass those 
• Or they use lambdas (or not) 
24
Namespaces 
• Python has a single namespace for functions and 
variables. So x can be an int, a str, or a function 
• Ruby has two separate namespaces — one for 
methods, and one for data 
• Ruby's approach can lead to surprises! 
x = 10 # set a local variable 
self.x = 10 # invoke an attr_ method 
25
Reserved words 
• Python barely has any: 
True, False = False, True 
• Ruby lets us open built-in classes, but not this! 
26
Modern Python 
• Fine, modern Python outlaws this. But it allows: 
>>> str = [1,2,3] 
>>> type(str) 
<class 'list'> 
27
Scoping 
• Python has clear rules for scoping: LEGB (local, 
enclosing function, global builtin) 
• Ruby's scoping rules feel natural, but the rules 
aren't easy to learn or remember. 
28
Indentation 
• Newcomers to Python hate the indentation rules 
• But I actually like them! 
• Easy to read 
• No annoying "end - end - end - end" all over 
• Easy to see where code begins and ends 
• That said, Ruby's syntax would never work with 
Python's indentation rules 
29
Parentheses 
• Needed in Python to invoke methods and create classes 
• Makes it easier to pass functions as parameters 
• Not needed in Ruby to invoke methods 
• Makes DSLs cleaner, easier to write and read 
• You cannot pass a method just by name; use 
method(:foo) 
• DSLs in non-Ruby languages look far uglier, and look like 
the original language, not a new one 
30
Explicit return 
• In Ruby, the value of the final line evaluated is the 
value of the method. We often don't say "return" 
• In Python, if you don't say "return", then your 
method returns None 
• I don't see an advantage to Python's behavior 
31
Attributes vs. methods 
• In Ruby, object state is private, and access is all done 
through methods. 
• Want to access data? Write a getter or setter 
• Or use attr_*, and Ruby will write them for you 
• In Python, object state is public 
• Getters and setters are discouraged 
• Want the logic of a getter or setter? Use a "property," 
which invokes a method when you access an attribute 
32
In other words 
• Ruby tries to make data look like methods 
• Method calls are the fundamental form of inter-object 
communication 
• Python tries to make methods look like data 
• Attributes are open, and thus there is less need 
for methods to access data 
• Besides, attributes exist in a single namespace 
33
Object model 
• In Ruby, the object model emphasizes methods 
and inheritance of those methods 
• What are your methods, and what are your 
superclass's methods? 
• In Python, the object model emphasizes attributes 
and "inheritance" of those attributes 
• What attributes are defined on an object, and 
what are define on its class? 
34
What is hard to learn? 
• Each object model leads to something that is hard 
for people to understand 
• In Ruby, you have to understand singleton classes 
(aka eigenclasses) 
• In Python, you have to understand how attributes 
work, including attribute scoping rules 
35
Multiple inheritance vs. 
include/extend 
• Python: Multiple inheritance is OK, if you do it right 
• Ruby: You won't get it right. Use modules!. 
• The inheritance tree is very easy to figure out 
• Modules allow for mixins, the only good reason 
for multiple inheritance 
36
for loops vs. Enumerable 
• In Ruby, we want to iterate over everything 
• Implement #each in your class 
• include Enumerable 
• Now you get map, select, and many other 
methods 
37
for loops vs. Enumerable 
• In Python, we also want to iterate over everything 
• We do that with "for" loops and other global 
constructs (e.g., comprehensions) 
• Our object plug into these constructs by 
implementing the iteration protocol 
38
list.sort's return type 
• Python's, list.sort returns None, not the list 
• In Ruby, we almost always return an object from a 
method, so that we can chain the method calls. 
39
Comprehensions 
• Python has comprehensions: 
[str(x) for x in range(5)] 
[x*x for x in range(5)] 
[line.split(":")[0] 
for line in open('/etc/passwd')] 
40
Sets and dicts 
• Set comprehensions 
{word.strip() 
for word in open('/usr/share/dict/words')} 
• Dictionary comprehensions 
{line.split(":")[0] : line.split(":")[2] 
for line in open('/etc/passwd') 
if line.startswith("#")} 
41
Comprehensible? 
No. 
42
Ruby is more consistent 
• In Ruby, we use the block syntax everywhere 
(0..4).map {|n| n.to_s} 
(0..4).map {|n| n * n} 
File.open('/etc/passwd').readlines. 
map {|line| line.split(":")[0]} 
43
The Ruby version 
Hash[File.open('/etc/passwd'). 
readlines. 
select {|line| line[0] != '#'}. 
map {|line| line.split(":")}. 
map {|row| [row[0], row[2]]} ] 
• Longer, but more consistent 
44
Easy metaprogramming 
• Metaprogramming is fun and easy in Ruby 
• Python permits it, but discourages such things 
• Most of the time, use decorators 
45
46 
Oscar 
Felix 
(Python) 
(Ruby)
你们有问题吗? 
יש לכם שאלות? 
(Do you have any questions?) 
reuven@lerner.co.il 
@reuvenmlerner 
http://lerner.co.il/ 
47

More Related Content

What's hot

Introduction to programming with python
Introduction to programming with pythonIntroduction to programming with python
Introduction to programming with pythonPorimol Chandro
 
Introduction about Python by JanBask Training
Introduction about Python by JanBask TrainingIntroduction about Python by JanBask Training
Introduction about Python by JanBask TrainingJanBask Training
 
Sdforum 11-04-2010
Sdforum 11-04-2010Sdforum 11-04-2010
Sdforum 11-04-2010Ted Dunning
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013Gary Short
 
python training | python course | python online training
python training |  python course |  python online trainingpython training |  python course |  python online training
python training | python course | python online trainingNancy Thomas
 

What's hot (7)

Introduction to programming with python
Introduction to programming with pythonIntroduction to programming with python
Introduction to programming with python
 
Introduction about Python by JanBask Training
Introduction about Python by JanBask TrainingIntroduction about Python by JanBask Training
Introduction about Python by JanBask Training
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
 
Sdforum 11-04-2010
Sdforum 11-04-2010Sdforum 11-04-2010
Sdforum 11-04-2010
 
Not Everything is an Object - Rocksolid Tour 2013
Not Everything is an Object  - Rocksolid Tour 2013Not Everything is an Object  - Rocksolid Tour 2013
Not Everything is an Object - Rocksolid Tour 2013
 
python training | python course | python online training
python training |  python course |  python online trainingpython training |  python course |  python online training
python training | python course | python online training
 
The State of #NLProc
The State of #NLProcThe State of #NLProc
The State of #NLProc
 

Viewers also liked

Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Reuven Lerner
 
Big Data — Your new best friend
Big Data — Your new best friendBig Data — Your new best friend
Big Data — Your new best friendReuven Lerner
 
PostgreSQL, your NoSQL database
PostgreSQL, your NoSQL databasePostgreSQL, your NoSQL database
PostgreSQL, your NoSQL databaseReuven Lerner
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methodsReuven Lerner
 
Technical training business talk.key
Technical training business talk.keyTechnical training business talk.key
Technical training business talk.keyReuven Lerner
 
2013 State of Cloud Survey SMB Results
2013 State of Cloud Survey SMB Results2013 State of Cloud Survey SMB Results
2013 State of Cloud Survey SMB ResultsSymantec
 
Breaking through the Clouds
Breaking through the CloudsBreaking through the Clouds
Breaking through the CloudsAndy Piper
 
2013 Future of Cloud Computing - 3rd Annual Survey Results
2013 Future of Cloud Computing - 3rd Annual Survey Results2013 Future of Cloud Computing - 3rd Annual Survey Results
2013 Future of Cloud Computing - 3rd Annual Survey ResultsMichael Skok
 
Intro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, JerusalemIntro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, JerusalemReuven Lerner
 
Can we hack open source #cloud platforms to help reduce emissions?
Can we hack open source #cloud platforms to help reduce emissions?Can we hack open source #cloud platforms to help reduce emissions?
Can we hack open source #cloud platforms to help reduce emissions?Tom Raftery
 
Summer School Scale Cloud Across the Enterprise
Summer School   Scale Cloud Across the EnterpriseSummer School   Scale Cloud Across the Enterprise
Summer School Scale Cloud Across the EnterpriseWSO2
 
Simplifying The Cloud Top 10 Questions By SMBs
Simplifying The Cloud Top 10 Questions By SMBsSimplifying The Cloud Top 10 Questions By SMBs
Simplifying The Cloud Top 10 Questions By SMBsSun Digital, Inc.
 
Penetrating the Cloud: Opportunities & Challenges for Businesses
Penetrating the Cloud: Opportunities & Challenges for BusinessesPenetrating the Cloud: Opportunities & Challenges for Businesses
Penetrating the Cloud: Opportunities & Challenges for BusinessesCompTIA
 
The Inevitable Cloud Outage
The Inevitable Cloud OutageThe Inevitable Cloud Outage
The Inevitable Cloud OutageNewvewm
 
Avoiding Cloud Outage
Avoiding Cloud OutageAvoiding Cloud Outage
Avoiding Cloud OutageNati Shalom
 
LinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud Computing
LinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud ComputingLinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud Computing
LinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud ComputingMark Hinkle
 
Delivering IaaS with Open Source Software
Delivering IaaS with Open Source SoftwareDelivering IaaS with Open Source Software
Delivering IaaS with Open Source SoftwareMark Hinkle
 
Linthicum what is-the-true-future-of-cloud-computing
Linthicum what is-the-true-future-of-cloud-computingLinthicum what is-the-true-future-of-cloud-computing
Linthicum what is-the-true-future-of-cloud-computingDavid Linthicum
 
Best Practices for Architecting in the Cloud - Jeff Barr
Best Practices for Architecting in the Cloud - Jeff BarrBest Practices for Architecting in the Cloud - Jeff Barr
Best Practices for Architecting in the Cloud - Jeff BarrAmazon Web Services
 

Viewers also liked (20)

Rails israel 2013
Rails israel 2013Rails israel 2013
Rails israel 2013
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
Big Data — Your new best friend
Big Data — Your new best friendBig Data — Your new best friend
Big Data — Your new best friend
 
PostgreSQL, your NoSQL database
PostgreSQL, your NoSQL databasePostgreSQL, your NoSQL database
PostgreSQL, your NoSQL database
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methods
 
Technical training business talk.key
Technical training business talk.keyTechnical training business talk.key
Technical training business talk.key
 
2013 State of Cloud Survey SMB Results
2013 State of Cloud Survey SMB Results2013 State of Cloud Survey SMB Results
2013 State of Cloud Survey SMB Results
 
Breaking through the Clouds
Breaking through the CloudsBreaking through the Clouds
Breaking through the Clouds
 
2013 Future of Cloud Computing - 3rd Annual Survey Results
2013 Future of Cloud Computing - 3rd Annual Survey Results2013 Future of Cloud Computing - 3rd Annual Survey Results
2013 Future of Cloud Computing - 3rd Annual Survey Results
 
Intro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, JerusalemIntro to cloud computing — MegaCOMM 2013, Jerusalem
Intro to cloud computing — MegaCOMM 2013, Jerusalem
 
Can we hack open source #cloud platforms to help reduce emissions?
Can we hack open source #cloud platforms to help reduce emissions?Can we hack open source #cloud platforms to help reduce emissions?
Can we hack open source #cloud platforms to help reduce emissions?
 
Summer School Scale Cloud Across the Enterprise
Summer School   Scale Cloud Across the EnterpriseSummer School   Scale Cloud Across the Enterprise
Summer School Scale Cloud Across the Enterprise
 
Simplifying The Cloud Top 10 Questions By SMBs
Simplifying The Cloud Top 10 Questions By SMBsSimplifying The Cloud Top 10 Questions By SMBs
Simplifying The Cloud Top 10 Questions By SMBs
 
Penetrating the Cloud: Opportunities & Challenges for Businesses
Penetrating the Cloud: Opportunities & Challenges for BusinessesPenetrating the Cloud: Opportunities & Challenges for Businesses
Penetrating the Cloud: Opportunities & Challenges for Businesses
 
The Inevitable Cloud Outage
The Inevitable Cloud OutageThe Inevitable Cloud Outage
The Inevitable Cloud Outage
 
Avoiding Cloud Outage
Avoiding Cloud OutageAvoiding Cloud Outage
Avoiding Cloud Outage
 
LinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud Computing
LinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud ComputingLinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud Computing
LinuxFest NW 2013: Hitchhiker's Guide to Open Source Cloud Computing
 
Delivering IaaS with Open Source Software
Delivering IaaS with Open Source SoftwareDelivering IaaS with Open Source Software
Delivering IaaS with Open Source Software
 
Linthicum what is-the-true-future-of-cloud-computing
Linthicum what is-the-true-future-of-cloud-computingLinthicum what is-the-true-future-of-cloud-computing
Linthicum what is-the-true-future-of-cloud-computing
 
Best Practices for Architecting in the Cloud - Jeff Barr
Best Practices for Architecting in the Cloud - Jeff BarrBest Practices for Architecting in the Cloud - Jeff Barr
Best Practices for Architecting in the Cloud - Jeff Barr
 

Similar to What can Ruby learn from Python (and vice versa)?

Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# DevelopersSarah Dutkiewicz
 
What is Python?
What is Python?What is Python?
What is Python?PranavSB
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharUttamKumar617567
 
Day 1 - Intro to Ruby
Day 1 - Intro to RubyDay 1 - Intro to Ruby
Day 1 - Intro to RubyBarry Jones
 
Ruby tutorial
Ruby tutorialRuby tutorial
Ruby tutorialknoppix
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010ssoroka
 
Writing Well-Behaved Unix Utilities
Writing Well-Behaved Unix UtilitiesWriting Well-Behaved Unix Utilities
Writing Well-Behaved Unix UtilitiesRob Miller
 
Perl5 meta programming
Perl5 meta programmingPerl5 meta programming
Perl5 meta programmingkarupanerura
 
"How do you think when you're coding?", Jon Skeet
"How do you think when you're coding?",  Jon Skeet"How do you think when you're coding?",  Jon Skeet
"How do you think when you're coding?", Jon SkeetFwdays
 
Asakusa ruby
Asakusa rubyAsakusa ruby
Asakusa rubypragdave
 
Python programming lab 23
Python programming lab 23Python programming lab 23
Python programming lab 23profbnk
 
PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...
PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...
PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...Puppet
 
Write a better FM
Write a better FMWrite a better FM
Write a better FMRich Bowen
 

Similar to What can Ruby learn from Python (and vice versa)? (20)

Intro to Python for C# Developers
Intro to Python for C# DevelopersIntro to Python for C# Developers
Intro to Python for C# Developers
 
Introduction_to_Python.pptx
Introduction_to_Python.pptxIntroduction_to_Python.pptx
Introduction_to_Python.pptx
 
What is Python?
What is Python?What is Python?
What is Python?
 
Python presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, BiharPython presentation of Government Engineering College Aurangabad, Bihar
Python presentation of Government Engineering College Aurangabad, Bihar
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Day 1 - Intro to Ruby
Day 1 - Intro to RubyDay 1 - Intro to Ruby
Day 1 - Intro to Ruby
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Ruby tutorial
Ruby tutorialRuby tutorial
Ruby tutorial
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
Writing Well-Behaved Unix Utilities
Writing Well-Behaved Unix UtilitiesWriting Well-Behaved Unix Utilities
Writing Well-Behaved Unix Utilities
 
Python1
Python1Python1
Python1
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
bhaskars.pptx
bhaskars.pptxbhaskars.pptx
bhaskars.pptx
 
Perl5 meta programming
Perl5 meta programmingPerl5 meta programming
Perl5 meta programming
 
Python-Basics.pptx
Python-Basics.pptxPython-Basics.pptx
Python-Basics.pptx
 
"How do you think when you're coding?", Jon Skeet
"How do you think when you're coding?",  Jon Skeet"How do you think when you're coding?",  Jon Skeet
"How do you think when you're coding?", Jon Skeet
 
Asakusa ruby
Asakusa rubyAsakusa ruby
Asakusa ruby
 
Python programming lab 23
Python programming lab 23Python programming lab 23
Python programming lab 23
 
PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...
PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...
PuppetConf 2017: Custom Types & Providers: Modeling Modern REST Interfaces an...
 
Write a better FM
Write a better FMWrite a better FM
Write a better FM
 

More from Reuven Lerner

Web APIs: The future of software
Web APIs: The future of softwareWeb APIs: The future of software
Web APIs: The future of softwareReuven Lerner
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Reuven Lerner
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talkReuven Lerner
 
Git talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in IsraelGit talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in IsraelReuven Lerner
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupReuven Lerner
 
Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Reuven Lerner
 
PostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferencePostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferenceReuven Lerner
 

More from Reuven Lerner (14)

Web APIs: The future of software
Web APIs: The future of softwareWeb APIs: The future of software
Web APIs: The future of software
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Rails traps
Rails trapsRails traps
Rails traps
 
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
Modern Web technologies (and why you should care): Megacomm, Jerusalem, Febru...
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talk
 
Git talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in IsraelGit talk from Open 2011 conference in Israel
Git talk from Open 2011 conference in Israel
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship group
 
Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011Modern Web Technologies — Jerusalem Web Professionals, January 2011
Modern Web Technologies — Jerusalem Web Professionals, January 2011
 
PostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferencePostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conference
 
ActiveRecord 2.3
ActiveRecord 2.3ActiveRecord 2.3
ActiveRecord 2.3
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
Rails console
Rails consoleRails console
Rails console
 
Rails tools
Rails toolsRails tools
Rails tools
 
Why ruby and rails
Why ruby and railsWhy ruby and rails
Why ruby and rails
 

Recently uploaded

Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Recently uploaded (20)

Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

What can Ruby learn from Python (and vice versa)?

  • 1. What Ruby can learn from Python (and vice versa) Reuven M. Lerner, PhD • reuven@lerner.co.il Rails Israel 2014 November 4th, 2014
  • 2. Who am I? • Programmer, consultant, developer, trainer • Long-time Ruby, Python user • Linux Journal columnist • Author, Practice Makes Python! • PhD in Learning Sciences from Northwestern • Read (lots) more at http://lerner.co.il/ 2
  • 4. Sapir-Whorf Hypothesis "Cognitive processes, such as thought and experience, may be influenced by the categories and patterns of the language a person speaks." — Wikipedia 4
  • 5. Or: You are what you speak! 5
  • 6. Learning a language helps you not only communicate, but also to think in new ways 6
  • 7. 7
  • 8. 你有叉⼦子吗? Nǐ yǒu chāzi ma? Do you have a fork? 8
  • 9. Possible answers • "Yes" • "No" • "What's a fork?" • "Your accent is so bad, I have no idea what you're saying" 9
  • 10. The actual answer? • None of the above! • Chinese doesn't have words for "yes" and "no." 10
  • 11. 你有叉⼦子吗? Nǐ yǒu chāzi ma? Do you have a fork? Answer: 有 (yǒu) "Have" 11
  • 12. Can you think of another language that works this way? 12
  • 13. בראשית כ״ט, ו׳ ויאמר, ״השלום לו?״ ויאמרו ״שלום.״ Genesis 29:6 And he said, "Is he well?" And they said, "Well!" (i.e., "Yes!") 13
  • 14. And also… CREATE TABLE Foo (id SERIAL, stuff TEXT); CREATE TABLE INSERT INTO Foo (stuff) VALUES ('abc'); INSERT 0 1 14
  • 15. Comparing languages • Appreciate each one more • Reflect on the relative trade-offs 15
  • 16. Ruby and Python both… • … dynamically and strongly typed • … are JIT byte-compiled • … are cross-platform • … are widely used for a variety of tasks • … popular among Web developers • … have multiple implementations • … have an object system based (in part) on Smalltalk 16
  • 17. And yet • Python and Ruby feel very different • What can we learn from these differences? • What can we learn from the Python world? (And what can they learn from us?) 17
  • 18. General attitude • Ruby wants to give you freedom, to allow you to be creative and flexible, as in a natural language • TMTOWTDI, POLS • Python wants to restrict you, for your own good • "There should be one — and preferably only one — obvious way to do it." 18
  • 19. The result? • Python is • easier to learn • easier to debug and maintain • Ruby • offers you richer options • greater expressiveness 19
  • 20. Development process • Python Enhancement Proposals (PEPs) • Ruby could use such a system • Heck, Rails could use such a system 20
  • 21. Backward compatibility • Historically, Python upgrades were conservative • But not 2.x -> 3.x • Ruby was less conservative, breaking things (especially going from 1.8 to 1.9/2.0) • Maybe Python emphasis on compatibility and inclusiveness hurt the 3.x upgrade 21
  • 22. What is truth? • Ruby: Everything is true, except for false and nil • Python: Everything is true, except for None, False, 0, and empty data (e.g., '' or []) • So: 0 is false in Python, but true in Ruby! • But we all like #blank? and #present? in Rails… 22
  • 23. Everything is an object… • In both Ruby and Python, everything is an object! • But in Python, some objects are more equal than others… • So you can't do this in Python: 5.times { puts "Hello" } 23
  • 24. Blocks • Ruby people love blocks! • Python doesn't have them. • They create functions, and pass those • Or they use lambdas (or not) 24
  • 25. Namespaces • Python has a single namespace for functions and variables. So x can be an int, a str, or a function • Ruby has two separate namespaces — one for methods, and one for data • Ruby's approach can lead to surprises! x = 10 # set a local variable self.x = 10 # invoke an attr_ method 25
  • 26. Reserved words • Python barely has any: True, False = False, True • Ruby lets us open built-in classes, but not this! 26
  • 27. Modern Python • Fine, modern Python outlaws this. But it allows: >>> str = [1,2,3] >>> type(str) <class 'list'> 27
  • 28. Scoping • Python has clear rules for scoping: LEGB (local, enclosing function, global builtin) • Ruby's scoping rules feel natural, but the rules aren't easy to learn or remember. 28
  • 29. Indentation • Newcomers to Python hate the indentation rules • But I actually like them! • Easy to read • No annoying "end - end - end - end" all over • Easy to see where code begins and ends • That said, Ruby's syntax would never work with Python's indentation rules 29
  • 30. Parentheses • Needed in Python to invoke methods and create classes • Makes it easier to pass functions as parameters • Not needed in Ruby to invoke methods • Makes DSLs cleaner, easier to write and read • You cannot pass a method just by name; use method(:foo) • DSLs in non-Ruby languages look far uglier, and look like the original language, not a new one 30
  • 31. Explicit return • In Ruby, the value of the final line evaluated is the value of the method. We often don't say "return" • In Python, if you don't say "return", then your method returns None • I don't see an advantage to Python's behavior 31
  • 32. Attributes vs. methods • In Ruby, object state is private, and access is all done through methods. • Want to access data? Write a getter or setter • Or use attr_*, and Ruby will write them for you • In Python, object state is public • Getters and setters are discouraged • Want the logic of a getter or setter? Use a "property," which invokes a method when you access an attribute 32
  • 33. In other words • Ruby tries to make data look like methods • Method calls are the fundamental form of inter-object communication • Python tries to make methods look like data • Attributes are open, and thus there is less need for methods to access data • Besides, attributes exist in a single namespace 33
  • 34. Object model • In Ruby, the object model emphasizes methods and inheritance of those methods • What are your methods, and what are your superclass's methods? • In Python, the object model emphasizes attributes and "inheritance" of those attributes • What attributes are defined on an object, and what are define on its class? 34
  • 35. What is hard to learn? • Each object model leads to something that is hard for people to understand • In Ruby, you have to understand singleton classes (aka eigenclasses) • In Python, you have to understand how attributes work, including attribute scoping rules 35
  • 36. Multiple inheritance vs. include/extend • Python: Multiple inheritance is OK, if you do it right • Ruby: You won't get it right. Use modules!. • The inheritance tree is very easy to figure out • Modules allow for mixins, the only good reason for multiple inheritance 36
  • 37. for loops vs. Enumerable • In Ruby, we want to iterate over everything • Implement #each in your class • include Enumerable • Now you get map, select, and many other methods 37
  • 38. for loops vs. Enumerable • In Python, we also want to iterate over everything • We do that with "for" loops and other global constructs (e.g., comprehensions) • Our object plug into these constructs by implementing the iteration protocol 38
  • 39. list.sort's return type • Python's, list.sort returns None, not the list • In Ruby, we almost always return an object from a method, so that we can chain the method calls. 39
  • 40. Comprehensions • Python has comprehensions: [str(x) for x in range(5)] [x*x for x in range(5)] [line.split(":")[0] for line in open('/etc/passwd')] 40
  • 41. Sets and dicts • Set comprehensions {word.strip() for word in open('/usr/share/dict/words')} • Dictionary comprehensions {line.split(":")[0] : line.split(":")[2] for line in open('/etc/passwd') if line.startswith("#")} 41
  • 43. Ruby is more consistent • In Ruby, we use the block syntax everywhere (0..4).map {|n| n.to_s} (0..4).map {|n| n * n} File.open('/etc/passwd').readlines. map {|line| line.split(":")[0]} 43
  • 44. The Ruby version Hash[File.open('/etc/passwd'). readlines. select {|line| line[0] != '#'}. map {|line| line.split(":")}. map {|row| [row[0], row[2]]} ] • Longer, but more consistent 44
  • 45. Easy metaprogramming • Metaprogramming is fun and easy in Ruby • Python permits it, but discourages such things • Most of the time, use decorators 45
  • 46. 46 Oscar Felix (Python) (Ruby)
  • 47. 你们有问题吗? יש לכם שאלות? (Do you have any questions?) reuven@lerner.co.il @reuvenmlerner http://lerner.co.il/ 47