Talks
Python for Ruby Programmers
Summarized using AI

Python for Ruby Programmers

by Mike Leone

The video titled "Python for Ruby Programmers" features Mike Leone, who discusses the similarities and differences between Python and Ruby programming languages. It is designed for Ruby programmers who are considering learning Python, emphasizing the transition as approachable given Ruby's dynamic features.

Key points covered in the presentation include:

  • Introduction to Mike: Speaker is a founder of Panoptic Development and has experience in both Ruby and Python.
  • Overview of Topics: The presentation plans to cover language design similarities, differences, and use cases applicable to both programming languages.
  • Similarities between Python and Ruby:
    • Both languages are dynamically typed and treat most entities as objects.
    • Similar syntax for functions and method calls, and they both support arrays (lists in Python) and hash tables (dictionaries in Python).
    • Support for functional programming paradigms, including anonymous functions and error handling mechanisms.
  • Differences between Python and Ruby:
    • Python does not support blocks (a feature in Ruby) but uses decorators instead.
    • Indentation is significant in Python, which can be seen as a barrier to some transitioning from Ruby.
    • Python utilizes tuples and has a more explicit import system compared to Ruby.
    • The presence of first-class functions and the 'self' parameter in Python which is different from Ruby's conventions.
  • Problem Domains: Python excels in scientific programming with libraries like NumPy, while Ruby is predominantly used in web development with Rails. Additionally, Python has broader applicability for Windows applications.
  • Conclusion and Takeaways:
    • While both languages have unique strengths and weaknesses, they are more similar than different.
    • Transitioning to Python can enhance a Ruby developer's skill set.
    • The broader community support and hiring opportunities for Python developers may be advantageous.

The video encourages Ruby developers to consider Python, recognizing that both languages have their place in software craftsmanship.

00:00:24.320 I'm Mike Leone, starting off with Python for Ruby programmers. One thing about the Ruby community that I have always loved is the emphasis on learning and not getting religiously hung up on our language or tools.
00:00:30.519 The goal is to take software craftsmanship and apply it to improve what we're doing, even if that means at times moving away from something we held sacred in the past.
00:00:39.520 Next, I’ll briefly outline the upcoming talks: This is Mike Leone on Python for Ruby programmers. Following this, we have a talk about Backbone, and then a third talk that delves into JRuby. All these topics are important tools that help contribute to software development overall, and they focus on craftsmanship and a desire to improve.
00:01:16.880 I would like to introduce Mike. He is a founder of Panoptic Development in Rhode Island, which he came to us from the East Coast.
00:01:34.680 I’m going to turn the mic over to Mike. That was a little weird, turning the mic over to Mike, and I’ll let him get started with his presentation. Thank you.
00:01:53.479 Great, thank you. Welcome to Python for Ruby programmers. First, I’d like to apologize for the formatting of this presentation. I had to switch laptops right before I started, so I will post the correct formatting of the slides online right after this.
00:02:05.240 This presentation is going to be fast-paced. I will try to teach the basics of Python in about 30 minutes, but don’t worry, it’s going to be very approachable coming from a Ruby background.
00:02:22.480 A little bit about me: I am a founder and principal engineer at Panoptic Development. We build web, mobile, and embedded applications. The languages we primarily use are Ruby, JavaScript, and of course, Python.
00:02:34.120 This gives us a different perspective than most folks. We are based in the beautiful state of Rhode Island, known for excellent seafood, massive political corruption, Family Guy, and not much else.
00:02:46.800 I started doing Ruby professionally in 2006, and then when a specific client application required Python, I began working with it in 2010. This experience has given me a new appreciation for the design decisions in each programming language, and I believe it has made me a better Ruby developer.
00:03:06.319 Today, we will look at a quick example program in both languages. We will cover 13 language design similarities, 13 differences, and discuss five different problem domains and how they apply to each language.
00:03:18.160 I will also share my opinions, which means I can make some bold statements with no need for proof. What we won't cover are specifics about implementations of either language.
00:03:29.440 We won’t really focus on performance, except to say that performance with both tends to be sufficient for the problem domains they address, and Python typically performs slightly better in most cases.
00:03:54.720 I won't discuss parallelism or multi-threaded programming in either language as I am not an expert. However, the C implementations of both languages feature a global interpreter lock, making it difficult to write performant multi-threaded programs.
00:04:05.400 We also won’t touch on minor syntax differences between the two languages, as I believe you will quickly grasp them and be able to read the code with ease.
00:04:22.800 What I will try to convince you of today is that the two languages are quite similar, and despite some significant differences, they resemble each other more than they differ.
00:04:37.319 You are already prepared to start writing Python because of your Ruby experience; the transition is not significant. Learning Python will enhance your skills as a Rubyist.
00:04:50.240 I also hope to persuade you to consider hiring Python programmers, even for Ruby programming positions. Here are a few principles from the Zen of Python that I believe are relevant to this presentation.
00:05:11.040 One: Explicit is better than implicit. Two: Flat is better than nested. Three: Readability counts. Four: There should usually be one obvious way to do something. Five: Namespaces are a great idea; let’s do more of those.
00:05:29.960 For example, let's create a Person class in both languages. The requirements are: a person should have a name and an age, be able to greet you, know if they are Justin Bieber, and be able to greet some folks from the Google board of directors.
00:05:41.839 The Ruby implementation is straightforward. You have a Person class, declare an array of Google directors as a constant within that class, define an initialize method to set the name and age, and include a few instance methods.
00:06:04.560 The method that checks if the name and age are those of Justin Bieber is a simple print statement, and greeting the Google directors involves looping through the array and printing a message for each.
00:06:28.439 Now let's take a look at the same concept in Python. You create a Person object, define the Google directors constant, and have an initializer function to set the name and age, just like in Ruby.
00:06:48.560 Although you will notice a few differences, such as the 'self' parameter being used throughout. The methods in Python are defined using 'def' rather than ending with 'end'.
00:07:05.480 Overall, the design in both languages is quite similar. Now let's review 13 similarities between them.
00:07:28.160 Both languages are dynamically typed. In Python, you can define a function that takes an object as a parameter and, as long as it responds to the expected method, it works seamlessly.
00:07:52.160 Similar to Ruby, most entities in Python are objects. You could argue that Python's consistency with objects is even greater than Ruby's because even functions are treated as objects.
00:08:07.520 For example, in both languages, adding two numbers can be achieved with the plus symbol, which serves as syntactic sugar provided by the interpreter, although method calls can also be used to achieve the same.
00:08:26.920 Both languages support arrays. In Ruby, this is called an 'array'; in Python, it’s a 'list'. You can store any type of object in these structures and access elements using typical integer syntax.
00:08:43.080 Although Ruby offers some additional features, such as methods for accessing the first or last elements of an array, the basics of how they function are very similar.
00:09:09.000 Both languages have support for hash tables. In Ruby, this is termed a hash, and in Python, it’s called a dictionary (or dict). You can access values corresponding to keys in much the same way.
00:09:23.600 You may notice some slight syntax differences, for instance, that strings are used rather than symbols for keys in Python. Additionally, Python does not utilize special line terminators such as semicolons.
00:09:36.679 Both languages support strong functional programming paradigms. For example, the 'map' function, reminiscent of Ruby's enumerable approach, allows processing elements on a list.
00:09:46.760 In Python, 'map' is a built-in function that requires supplying a function and a list. Similarly, the 'filter' function allows filtering a list of numbers based on a condition.
00:10:17.640 Both languages use anonymous functions called lambdas. However, in Python, lambdas must be written in a single line, which is considered a feature of the language.
00:10:29.840 Regarding function parameters, both languages support optional arguments. In Python, you can define default parameters that allow for flexible function calls.
00:10:51.760 For example, a greet function could have a default greeting value, which can be overridden when calling. Both languages also support argument unpacking.
00:11:23.120 Error handling is quite similar in both languages. In Ruby, you can raise an ArgumentError with a conditional check, whereas Python uses a similar raise statement to trigger exceptions.
00:11:51.240 When dealing with exceptions, Ruby employs the begin-rescue-end structure, while Python uses try-except, signaling similar control flows.
00:12:18.639 Both languages support elegant oneline conditionals, although Python places the conditional keyword at the beginning of the expression.
00:12:30.720 Each language features interactive shells. In Python, invoking Python with no arguments gives you an interactive console, whereas Ruby has a separate program for its shell.
00:12:56.120 Both languages provide strong object reflection capabilities. You can call various built-in functions to collect attributes and methods defined on classes.
00:13:07.679 Lastly, I would like to highlight that both languages are generally simpler compared to C++ or Java. Now, let’s discuss the 13 differences between Python and Ruby.
00:13:42.720 One major difference is that Python does not support blocks, which many consider a killer feature of Ruby. This absence changes the nature of metaprogramming paradigms in Python.
00:14:13.360 To fill this gap, Python uses decorators—functions that can modify or extend the behavior of functions—but they are not as flexible as Ruby's blocks.
00:14:34.960 The second notable difference is that Python has first-class functions—something not available in Ruby. Python allows you to pass functions as arguments.
00:14:54.720 For example, you can define addition and subtraction functions, then create a higher-order function that processes two numbers based on the passed function.
00:15:16.280 Lambdas in Python also differ from those in Ruby, as they must be defined in a single line. This design choice aims to promote more concise methods in Python.
00:15:45.679 Additionally, Python utilizes tuples, which are immutable lists, although they are not necessarily enforced to be homogeneous.
00:16:05.200 Python allows for more granular importing of libraries. When importing, you specify the components you need, promoting explicit programming.
00:16:30.880 Python enforces indentation, which can be confusing for some developers but promotes consistent coding styles.
00:16:48.680 In Python, more values evaluate to false. For instance, False, None, an empty list, a tuple, a string, or zero will evaluate to false in Python, while Ruby only recognizes false and nil.
00:17:47.240 Python lacks the enumerable feature found in Ruby. While Python has built-in functions like filter and map, they are not as powerful as Ruby’s enumerable module.
00:18:10.560 Python uses simpler conditional statements with no case switch; instead, it employs an if-elif-else ladder.
00:18:33.440 Unlike Ruby, Python does not have an 'unless' keyword, which some developers find confusing.
00:18:56.800 Python functions return None if no explicit return statement is provided, which can be confusing for Ruby developers if they are transitioning between languages.
00:19:14.640 In Python, every instance method must include 'self' as the first parameter, which provides clarity but can seem verbose compared to Ruby.
00:19:36.560 Ruby supports powerful module mixins; however, Python primarily relies on multiple inheritance, which can be less intuitive.
00:19:58.440 Lastly, Ruby features stronger metaprogramming capabilities. Python does have some, but it lacks the extensive features like define_method or method_missing that Ruby provides.
00:20:14.680 Now, let’s look briefly at five problem domains. First is web development. Is there anyone here who writes Ruby for their day job but does not consider themselves a web developer? Zero? Wow. A Python conference probably wouldn’t happen unless it was for Django.
00:20:43.480 Ruby has Rails and Sinatra as its main web development frameworks, and the Ruby community is predominantly centered around web applications.
00:21:02.600 Python, on the other hand, offers several frameworks too. Django is the most popular full-stack web development framework, followed by others like Zope, Pylons, and Flask, which is quite similar to Sinatra.
00:21:18.760 Now, regarding mobile applications, neither language is highly popular, but Ruby has projects like R o Mobile and RubyMotion for building cross-platform mobile apps.
00:21:48.160 Python's presence in mobile development is less visible, but there are avenues like Kivy and BeeWare that provide some options.
00:22:07.800 On the front of desktop applications, both languages have QT and GTK bindings, but Ruby's libraries are less mature compared to those available in Python.
00:22:23.920 Scientific programming is where Python shines with libraries like NumPy and SciPy. Ruby's offering in this area lacks maturity and utility.
00:22:49.240 Lastly, when it comes to deploying Windows applications, Ruby's Rails installer is more for development environments, while Python tends to be more compatible with Windows and offers broad support for its libraries.
00:23:29.760 In general, the Python community is larger, interacting across various problem domains, unlike Ruby, which is heavily web-focused.
00:24:33.920 Now, let’s talk about my feelings. I believe it’s harder to write code that frustrates others in Python. Its concise grammar and fewer ways to handle conditionals creates common ground between developers.
00:25:37.280 Although Ruby offers elegance and expressiveness, Python’s readability is more consistent and often clearer. However, for web development, I still prefer Rails; it remains the superior framework.
00:26:34.960 If you are looking to hire, consider the abundance of Python programmers compared to Ruby developers, making it easier to find talented engineers.
00:27:04.680 In closing, I hope I’ve demonstrated that Python and Ruby share many similarities, more so than they differ. Moreover, diving into Python could significantly enhance your Ruby skills.
00:27:35.080 Thank you for your time. You can find our website at panopticdev.com, my personal blog at leon.panopticdev.com, or connect with me on Twitter at @faceBiff.
00:28:07.360 Now, I’d like to open the floor for any questions.
00:28:38.000 Audience Question: Is method chaining different in Python? Answer: Yes, it's generally less common and not as seamlessly implemented due to the language's structure.
00:29:55.400 Thank you so much, everyone!
Explore all talks recorded at LA RubyConf 2013
+6