<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Michele Campeotto: Tag style</title>
    <link>http://blog.micampe.it/articles/tag/style?tag=style</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>I'm not Winston Wolfe.</description>
    <item>
      <title>Ruby and Python: perspectives</title>
      <description>&lt;p&gt;I just realized something interesting.&lt;/p&gt;

&lt;p&gt;In Ruby you mix in modules that add methods to objects that implement interfaces.&lt;/p&gt;

&lt;p&gt;In Python you have functions that you use on objects that implement interfaces.&lt;/p&gt;&lt;p&gt;One easy example are enumerables/iterables: in Ruby, an object is enumerable if it implements the &lt;code&gt;each&lt;/code&gt; method. The object can then mix in &lt;a href="http://www.ruby-doc.org/core/classes/Enumerable.html"&gt;Enumerable&lt;/a&gt; to gain some methods that you can use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;f = File.new(".bashrc", "r")
lines = f.find_all { |l| l =~ /^[^#]/ }
# or lines = f.grep(/^[^#]/)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Python, an object is &lt;a href="http://www.python.org/dev/peps/pep-0234/"&gt;iterable&lt;/a&gt; if it implements the &lt;code&gt;__iter__&lt;/code&gt; method, and the language &lt;em&gt;&amp;#8220;knows&amp;#8221;&lt;/em&gt; how to use the iterators:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;f = file(".bashrc", "r")
lines = [ l for l in f if not l.startswith("#") ]
# or lines = filter(lambda l: not l.startswith("#"), f)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;other examples are &lt;code&gt;any&lt;/code&gt; and &lt;code&gt;all&lt;/code&gt;, which incidentally are what started the discussion with &lt;a href="http://fmosca.acx.it"&gt;Francesco&lt;/a&gt; about this:&lt;/p&gt;

&lt;p&gt;In Ruby, the &lt;code&gt;any?&lt;/code&gt; method is added to every enumerable object that mixes in Enumerable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;lines.any? { |l| l =~ /^#/ }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Python, &lt;code&gt;any()&lt;/code&gt; is a built-in function (introduced in &lt;a href="http://docs.python.org/whatsnew/whatsnew25.html"&gt;2.5&lt;/a&gt;) which knows how to use any sequence:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;any(l.startswith("#") for l in lines)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So Ruby works from the &lt;em&gt;inside&lt;/em&gt;, and Python from the &lt;em&gt;outside&lt;/em&gt; of objects. I think therefore that the next comparison between the two languages should not be side-by-side, but inside-and-out.&lt;/p&gt;</description>
      <pubDate>Fri, 10 Nov 2006 07:03:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:db3d233b-525c-4b4f-96af-4079ed77cfcc</guid>
      <author>micampe</author>
      <link>http://blog.micampe.it/articles/2006/11/10/ruby-and-python-perspectives</link>
      <category>English</category>
      <category>ruby</category>
      <category>python</category>
      <category>perspective</category>
      <category>style</category>
      <category>philosophy</category>
      <trackback:ping>http://blog.micampe.it/articles/trackback/1064</trackback:ping>
    </item>
    <item>
      <title>Ops, I did it again</title>
      <description>&lt;p&gt;Yes, I changed the style again. The old one got me tired pretty soon &amp;#8211; I was missing the green! &amp;#8211; so I changed it. I&amp;#8217;ve gone for even more simplicty and minimalism this time.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m not entirely satisfied with the orange rollover on the menu and I&amp;#8217;d like some cool header image, but for now I&amp;#8217;m keeping it as it is.&lt;/p&gt;

&lt;p&gt;Someone liked the old design very much so it will probably come to a new life soon in some other form.&lt;/p&gt;</description>
      <pubDate>Tue, 05 Apr 2005 10:21:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:8cc7ec5c-77d9-45c9-aa05-e02f15f455af</guid>
      <author>micampe</author>
      <link>http://blog.micampe.it/articles/2005/04/05/ops-i-did-it-again</link>
      <category>English</category>
      <category>meta</category>
      <category>blog</category>
      <category>micampe</category>
      <category>style</category>
      <trackback:ping>http://blog.micampe.it/articles/trackback/166</trackback:ping>
    </item>
  </channel>
</rss>
