100% found this document useful (1 vote)
144 views

Python

The document compares Python and Ruby programming languages. It notes that both have an interactive prompt, support multiline strings, use brackets for lists and braces for dictionaries, and are dynamically typed with objects. Key differences include that Ruby strings are mutable, has enforced naming conventions, only one kind of mutable list, different truth testing, and shortcuts that make Ruby more productive but with more to remember. Ruby also allows modifying built-in classes while Python does not.

Uploaded by

Gudiya Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
144 views

Python

The document compares Python and Ruby programming languages. It notes that both have an interactive prompt, support multiline strings, use brackets for lists and braces for dictionaries, and are dynamically typed with objects. Key differences include that Ruby strings are mutable, has enforced naming conventions, only one kind of mutable list, different truth testing, and shortcuts that make Ruby more productive but with more to remember. Ruby also allows modifying built-in classes while Python does not.

Uploaded by

Gudiya Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Python is another very nice general purpose programming language.

Going from Python to Ruby, youll find that theres a little bit more syntax to learn than with Python.

Similarities
As with Python, in Ruby, Theres an interactive prompt (called irb). You can read docs on the command line (with the ri command instead of pydoc). There are no special line terminators (except the usual newline). String literals can span multiple lines like Pythons triple-quoted strings. Brackets are for lists, and braces are for dicts (which, in Ruby, are called hashes). Arrays work the same (adding them makes one long array, but composing them like this a3 = [ a1, a2 ] gives you an array of arrays). Objects are strongly and dynamically typed. Everything is an object, and variables are just references to objects. Although the keywords are a bit different, exceptions work about the same. Youve got embedded doc tools (Rubys is called rdoc).

Differences
Unlike Python, in Ruby, Strings are mutable. You can make constants (variables whose value you dont intend to change). There are some enforced case-conventions (ex. class names start with a capital letter, variables start with a lowercase letter). Theres only one kind of list container (an Array), and its mutable. Double-quoted strings allow escape sequences (like \t) and a special expression substitution syntax (which allows you to insert the results of Ruby expressions directly into other strings without having to "add " + "strings " + "together"). Single-quoted strings are like Pythons r"raw strings". There are no new style and old style classes. Just one kind. (Python 3+ doesnt have this issue, but it isnt fully backward compatible with Python 2.) You never directly access attributes. With Ruby, its all method calls. Parentheses for method calls are usually optional. Theres public, private, and protected to enforce access, instead of Pythons _voluntary_ underscore __convention__. mixins are used instead of multiple inheritance. You can add or modify the methods of built-in classes. Both languages let you open up and modify classes at any point, but Python prevents modification of built-ins Ruby does not. Youve got true and false instead of True and False (and nilinstead of None).

When tested for truth, only false and nil evaluate to a false value. Everything else is true (including 0, 0.0, "", and []). Its elsif instead of elif. Its require instead of import. Otherwise though, usage is the same. The usual-style comments on the line(s) above things (instead of docstrings below them) are used for generating docs. There are a number of shortcuts that, although give you more to remember, you quickly learn. They tend to make Ruby fun and very productive. Theres no way to unset a variable once set (like Pythons delstatement). You can reset a variable to nil, allowing the old contents to be garbage collected, but the variable will remain in the symbol table as long as it is in scope.

You might also like