{"id":13,"date":"2010-11-29T10:02:24","date_gmt":"2010-11-29T17:02:24","guid":{"rendered":"http:\/\/sloanseaman.com\/wordpress\/?p=13"},"modified":"2010-12-05T20:29:35","modified_gmt":"2010-12-06T03:29:35","slug":"python-vs-ruby","status":"publish","type":"post","link":"http:\/\/sloanseaman.com\/wordpress\/2010\/11\/29\/python-vs-ruby\/","title":{"rendered":"Python vs Ruby"},"content":{"rendered":"<p>I just spent the last 8 hours taking notes on Python and Ruby.   Most of my notes are from the excellent tutorials at <a href=\"http:\/\/www.tutorialspoint.com\">Tutorials Point<\/a>.<\/p>\n<p>After 25 pages of notes where I took just what was needed to be good enough to write in the two languages, I&#8217;m going to have to say that neither is really a clear winner for me.  <\/p>\n<h3>Overview:<\/h3>\n<h4>OOP<\/h4>\n<p>Ruby is all OOP which is great.  Python looks likes OOP was an afterthought.   Case in point:<br \/>\nA Ruby object:<\/p>\n<pre class=\"brush: ruby; light: false; title: ; notranslate\" title=\"\">\r\nclass Point\r\n    def initialize(x,y)\r\n        @x, @y = x, y\r\n    end\r\n    def doSomething(x)\r\n        stmts\r\n    end\r\nend\r\n<\/pre>\n<p>Python:<\/p>\n<pre class=\"brush: python; light: false; title: ; notranslate\" title=\"\">\r\nclass Point:\r\n    &quot;description&quot;\r\n    def __init__(self, x, y):\r\n        self.x = x\r\n        self.y = y\r\n    def doSomething(self, x):\r\n        suite\r\n<\/pre>\n<p><\/p>\n<p>The passing of  <code>self<\/code> just looks like it was such an afterthought that there wasn&#8217;t a lot of effort put into making it OO.  That isn&#8217;t a bad thing, if it works, it works, but it does raise some concerns for enterprise applications.<\/p>\n<p\/>\n<p>Ruby has method scoping (public, private, protected).  I didn&#8217;t see that in Python (again, I&#8217;m a newbie, I may have missed it).  If you want to write good black boxed api&#8217;s I consider this a must.   That being said, Ruby&#8217;s implementation is hit or miss as well.<\/p>\n<p\/>\nRuby<\/p>\n<pre class=\"brush: ruby; highlight: [4]; light: false; title: ; notranslate\" title=\"\">\r\nclass Class1\r\n    def method1\r\n    end\r\n    private  # all methods from here down are private\r\n    def privMethod1\r\n    end\r\nend\r\n<\/pre>\n<p>or<\/p>\n<pre class=\"brush: ruby; highlight: [6]; light: false; title: ; notranslate\" title=\"\">\r\nclass Class1\r\n    def method1\r\n    end\r\n    def privMethod1\r\n    end\r\n    private :privMethod1\r\nend\r\n<\/pre>\n<p>Not the prettiest way (maybe <code>defPrivate<\/code> would have been better) but it does work and the &#8216;all private from here down&#8217; is a nice shortcut.<\/p>\n<h4>Variable Manipulation<\/h4>\n<p>Both are pretty equal to be honest.  Lets look at some basic String manipulation:<\/p>\n<p>Ruby<\/p>\n<pre class=\"brush: ruby; light: false; title: ; notranslate\" title=\"\">\r\nstr = 'HelloWorld'\r\nputs str[1..3]    # 'ell'\r\n<\/pre>\n<p\/>Python<\/p>\n<pre class=\"brush: python; light: false; title: ; notranslate\" title=\"\">\r\nstr = 'HelloWorld'\r\nprint str[1:3]    # 'ell'\r\n<\/pre>\n<p>Now, that&#8217;s a basic example but it shows that the designers of the language has the same things in mind, just a slightly different way of doing it.<\/p>\n<h4>Statements<\/h4>\n<p>Both have <code>for, while<\/code>, etc.  Ruby goes a bit further and give you <code>unless<\/code> and <code>until<\/code>.  <code>unless<\/code> and <code>until<\/code> have their place but the existence of them does not mean a win for me.  You can do the same things with more standard loop statements.<\/p>\n<p\/>\nThey differ slightly in for loop execution but nothing too bad:<br \/>\n<br \/>\nRuby<\/p>\n<pre class=\"brush: ruby; light: false; title: ; notranslate\" title=\"\">\r\nfor i in arr\r\n    puts &quot;Value: #{i}&quot;\r\nend\r\n<\/pre>\n<p\/>Python<\/p>\n<pre class=\"brush: python; light: false; title: ; notranslate\" title=\"\">\r\nfor x in arr\r\n    print &quot;Value: &quot;, x\r\n<\/pre>\n<p>The above example does show one nice thing about Ruby.  Having the variables available for inserting into strings via #{} is very nice.  I&#8217;m not sure about performance though as Python is probably more of a concatenation while Ruby is search\/replace.  I don&#8217;t think any developer would really notice the difference though unless you are processing very large amounts of data.<\/p>\n<h3>Conclusion<\/h3>\n<p>Let me first state again that I am a newbie to both of these languages.  Also let me state that I really just touched the tip of the iceberg.  There are many subtle differences between the two languages and as most developers would tell you, it&#8217;s not always the language, but the libraries that make the difference.<\/p>\n<p\/>\nI think each has it&#8217;s place in the world.  Ruby (especially with Rails) seems to have the libraries for web development.  Python appears to have a good bit of libraries for more system oriented functions.  From a language point of view though, its a tough call.<\/p>\n<p\/>\nMe personally?  I would use Python as a replacement for shell scripts or PERL because it just seems more suited for quick hacks and file manipulation. I would use Ruby for large applications and web development because of its more strict OO and many web libraries.<\/p>\n<p>&#8220;But that&#8217;s just my opinion, I could be wrong&#8221; &#8211; Dennis Miller<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just spent the last 8 hours taking notes on Python and Ruby. Most of my notes are from the excellent tutorials at Tutorials Point. After 25 pages of notes where I took just what was needed to be good &hellip; <a href=\"http:\/\/sloanseaman.com\/wordpress\/2010\/11\/29\/python-vs-ruby\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[3,4],"_links":{"self":[{"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/posts\/13"}],"collection":[{"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":25,"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":66,"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/posts\/13\/revisions\/66"}],"wp:attachment":[{"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sloanseaman.com\/wordpress\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}