<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Eli Bendersky's website - Ruby</title><link href="https://eli.thegreenplace.net/" rel="alternate"></link><link href="https://eli.thegreenplace.net/feeds/ruby.atom.xml" rel="self"></link><id>https://eli.thegreenplace.net/</id><updated>2025-05-31T13:46:04-07:00</updated><entry><title>A subjective comparison between Perl and Ruby</title><link href="https://eli.thegreenplace.net/2008/02/01/a-subjective-comparison-between-perl-and-ruby" rel="alternate"></link><published>2008-02-01T17:36:24-08:00</published><updated>2023-06-30T23:16:27-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2008-02-01:/2008/02/01/a-subjective-comparison-between-perl-and-ruby</id><summary type="html">
        	&lt;p&gt;The goal of this article is to examine whether it's worth for me to switch to Ruby as my main programming language, a position currently taken by Perl. As the title states, it is &lt;em&gt;subjective&lt;/em&gt; &amp;#8211; in that it presents my personal opinions and targets my specific needs.&lt;/p&gt;


	&lt;h4&gt;Introduction&lt;/h4&gt;


	&lt;p&gt;First, some …&lt;/p&gt;</summary><content type="html">
        	&lt;p&gt;The goal of this article is to examine whether it's worth for me to switch to Ruby as my main programming language, a position currently taken by Perl. As the title states, it is &lt;em&gt;subjective&lt;/em&gt; &amp;#8211; in that it presents my personal opinions and targets my specific needs.&lt;/p&gt;


	&lt;h4&gt;Introduction&lt;/h4&gt;


	&lt;p&gt;First, some background. For many years now, Perl is my main go-to guy for programming tasks. As a hardware engineer, my "programming tasks" are not enterprise-level apps distributed for the use of thousands of customers, but rather support programs and scripts whose main customers are myself and my colleagues. Sometimes these programs have GUIs, sometimes they're even web-based, a lot of times they interact with other (mainly C++ for performance) programs, but most of them are scripts. Whenever some script has to be written for text manipulation, or a simple program for controlling some piece of embedded hardware, I turn to Perl. In size, these programs range from one-liners to several thousand lines of code (KLOCs).&lt;/p&gt;


	&lt;p&gt;Also, I must add that my platform at the time being is 100% MS Windows. In my previous job, I worked on UNIXes (Linux, &lt;span class="caps"&gt;AIX&lt;/span&gt;, Solaris), where Perl blends much better in the environment. But now I have to work with what I've got, and Windows is the platform. This means that I use ActiveState as the Perl distribution and have a keen interest in GUIs.&lt;/p&gt;


	&lt;p&gt;I'm always on the lookout for new tools and technologies to learn. Around two years ago, I decided to give Ruby a try, since I began hearing more and more about how cool it is. As I'm constantly lurking in the Perl community, so what gave me the push was seeing how several Perl hackers whom I respect recommend Ruby as a good future direction.&lt;/p&gt;


	&lt;p&gt;Anyway, in my last attempt, after a brief flirtation Ruby I decided to go back to Perl, as I found Ruby still not mature enough to be my go-to guy, mainly because of the lack of documentation, suitable libraries and the much smaller community.&lt;/p&gt;


	&lt;p&gt;Today, I decided to examine Ruby again, by first publishing a thorough comparison between it and Perl, in order to help myself make a learned choice.&lt;/p&gt;


	&lt;h4&gt;First things first &amp;#8211; the language core&lt;/h4&gt;


	&lt;p&gt;In my opinion, Ruby's core is superior to Perl. Not much to think of here. And I'm definitely not one to be called a "Perl basher". I like Perl, I like its syntax, I like programming in Perl. But Ruby is better.&lt;/p&gt;


	&lt;p&gt;Ruby is everything Perl is, and more. You can write any Perl code in Ruby, in a very similar way, but Ruby offers several definite improvements. Here are a few I deem most important:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Object-orientation from the ground-up. Personally, I feel that while &lt;span class="caps"&gt;OOP&lt;/span&gt; is not the silver bullet, it helps in keeping large bodies of code maintainable. Writing OO code in Perl is painful, in Ruby a pleasure.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Convenient, built-in exception handling.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;The support for functional features is better than in Perl. Blocks, procs, lambdas, and all that. I feel very grateful to Ruby for providing convenient means of replacing several of the things I previously thought require the power of Lisp macros.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;ul&gt;
	&lt;li&gt;Metaprogramming, better support for code generation and embedded DSLs.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;So there's no question here, at least for me. Had everything else been equal, I would certainly pick Ruby. Alas, things are never so simple.&lt;/p&gt;


	&lt;h4&gt;Performance&lt;/h4&gt;


	&lt;p&gt;According to the &lt;a href="http://shootout.alioth.debian.org/gp4/ruby.php"&gt;shootout&lt;/a&gt;, Ruby is 4-5x slower than Perl on average. This is not a major difference &amp;#8211; I wouldn't use Perl for heavy number crunching anyway. Besides, Ruby has an alternative virtual machine named &lt;span class="caps"&gt;YARV&lt;/span&gt; that should enter service any time now&amp;#8482;, and is expected to &lt;a href="http://shootout.alioth.debian.org/gp4sandbox/benchmark.php?test=all&amp;amp;lang=yarv&amp;amp;lang2=perl"&gt;improve the performance&lt;/a&gt; by at least a few 10s of %s.&lt;/p&gt;


	&lt;p&gt;When you need high performance, you'll have to resort to C/C++, and both Perl and Ruby have good bindings for these languages.&lt;/p&gt;


	&lt;h4&gt;Documentation and libraries&lt;/h4&gt;


	&lt;p&gt;I place these two together, because I think they're very much related. The sample people who spare the time to write useful modules, also write good documentation for their and others' modules.&lt;/p&gt;


	&lt;p&gt;In this section, Perl wins big. Perl's library of code, the mighty &lt;span class="caps"&gt;CPAN&lt;/span&gt;, is incomparable.  Maybe Ruby is not far from Perl in popularity today, but this is a recent event. For many years, Perl was more popular by orders of magnitude, and accumulated a huge library of shared knowledge.&lt;/p&gt;


	&lt;p&gt;Documentation in the Perl world is also better. Funny, because I find the RDoc to be much better than &lt;span class="caps"&gt;POD&lt;/span&gt;. Documenting Ruby code in a standard way is far simpler and more pleasant than with Perl &amp;#8211; where embedding &lt;span class="caps"&gt;POD&lt;/span&gt; feels like programming in the 80s. But yet again, Perl's huge lead over a long time ensured that most of the libraries are quite well documented and there are tons of examples and snippets to be found online. Ruby is catching up, but this will take time.&lt;/p&gt;


	&lt;p&gt;Just one example: &lt;span class="caps"&gt;XML&lt;/span&gt; handling. Ruby's canonical solution is &lt;span class="caps"&gt;REXML&lt;/span&gt; &amp;#8211; a slow, unconvenient &lt;span class="caps"&gt;API&lt;/span&gt; with appalling error handling. I was so disgusted by &lt;span class="caps"&gt;REXML&lt;/span&gt; when writing a Ruby app a year ago that I switched my data format to &lt;span class="caps"&gt;YAML&lt;/span&gt;. Perl has numerous excellent solutions for &lt;span class="caps"&gt;XML&lt;/span&gt; parsing and generation, right in the standard distribution. Rubyists ported &lt;span class="caps"&gt;XML&lt;/span&gt;::Simple, but it's based on &lt;span class="caps"&gt;REXML&lt;/span&gt;, which leaves it with all the deficiencies.&lt;/p&gt;


	&lt;p&gt;A disclaimer must be made here, which also applies to the "community" section. I'm concerned mainly with the English-speaking community. For Perl, there is no other. But for Ruby, there is. Ruby is an immigrant from Japan, and has a large and vibrant Japanese community behind it. It means that there are a lot of Ruby modules without any English documentation, which is a real pity. Even if these modules are terrific, there's no way for those of us who don't understand Japanese to enjoy them.&lt;/p&gt;


	&lt;h4&gt;Community&lt;/h4&gt;


	&lt;p&gt;A community is extremely important in our connected age. A large, smart community means quicker answers to questions, more libraries and documentation, and just a place to share wisdom and learn new tricks.&lt;/p&gt;


	&lt;p&gt;The Perl community is just amazing. In football&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;, it is often said that the home crowd is the team's 12th player. The Perl community is something like that. &lt;a href="http://perlmonks.org/"&gt;Perlmonks&lt;/a&gt; is the best programming language community on the Internet, period. Any question you have will be promptly answered by super-smart Perl hackers, some of whom seem to have little other interest in life than in providing detailed, well researched answers on Perlmonks. It's also one of the best places I know of to discuss general "meaning of life" questions in relation to programming, i.e. comparing paradigms, discussing future directions, and so on.&lt;/p&gt;


	&lt;p&gt;Ruby doesn't have anything of the kind. What it does have is a mailing list / forum / Google group&lt;sup&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;. When I tried Ruby the previous time, the situation was worse &amp;#8211; the group was less active and the connection between the various channels wasn't working well, creating fragmentation. Now, however, things seem to be better. The folks in the list are friendly and questions get answered quite quickly.&lt;/p&gt;


	&lt;p&gt;And still, Perl wins here. Someone should really create a Rubymonks &amp;#8211; I think it would give a great boost to the community.&lt;/p&gt;


	&lt;h4&gt;Momentum and future directions&lt;/h4&gt;


	&lt;p&gt;This issue is difficult to define exactly, and hence I will digress to a little philosophizing. A metaphor might help. Consider two employees. One is 45, very smart, been-there done-that kind of guy. Everything he's ever done, he can do again easily, quickly and soundly. However, he's reluctant to learn new technologies, and sometime hints that he might retire early. Another is a whizzy 25 year-old. He doesn't have so much knowledge accumulated, but he's an excellent hacker and can learn very quickly. He's eager to enter any new area and signals that he'll stay in the company for a long time.&lt;/p&gt;


	&lt;p&gt;Lately, I feel that Perl is the old guy and Ruby the young guy. I will try to defend my point:&lt;/p&gt;


	&lt;p&gt;Ruby seems to be having much more momentum behind it now than Perl. New technologies are more likely to be implemented in Ruby than in Perl. New books on programming are more likely to be written with Ruby than with Perl, and most importantly, fresh hackers are much more likely to join the world of Ruby than the world of Perl.&lt;/p&gt;


	&lt;p&gt;The last point is crucial. Perl has several excellent people working with it, who are well known throughout the community. The problem is, it's pretty much the same people of years ago. In Ruby, on the other hand, names of prolific and talented hackers constantly pop up &amp;#8211; they push the language, with its libraries and community fast forward.&lt;/p&gt;


	&lt;p&gt;For example, take the Textile markup language&lt;sup&gt;&lt;a href="#fn3"&gt;3&lt;/a&gt;&lt;/sup&gt;. It's quite new, and it doesn't surprise me that Ruby has a better library for translating it than Perl. &lt;a href="http://whytheluckystiff.net/ruby/redcloth/"&gt;RedCloth&lt;/a&gt;, written by the amazingly prolific Ruby hacker &lt;a href="http://hackety.org/"&gt;_why&lt;/a&gt; supports Textile better than the best Perl library out there, and no one seems to be fixing the situation in the Perl world.&lt;/p&gt;


	&lt;p&gt;The same can be said for &lt;span class="caps"&gt;YAML&lt;/span&gt; &amp;#8211; the parser for which was also written by _why, first for Ruby, and only later ported to Perl.&lt;/p&gt;


	&lt;p&gt;Take the bindings to Wx and Qt &amp;#8211; the two most popular cross-platform native-looks &lt;span class="caps"&gt;GUI&lt;/span&gt; frameworks. Ruby has the better bindings for both. True, Perl's integration with Tk is unrivaled in the Ruby world, but this only proves my point &amp;#8211; Perl is excellent in everything &lt;em&gt;old&lt;/em&gt;, but Ruby is quickly gaining in the &lt;em&gt;newer&lt;/em&gt; technologies. While for a &lt;span class="caps"&gt;GUI I&lt;/span&gt;'d still use Perl/Tk, I'm watching wxRuby, drooling at its pretty native widgets and controls.&lt;/p&gt;


	&lt;p&gt;I can only mention Ruby's strongest card &amp;#8211; the Rails framework, in passing, because I have never used it.&lt;/p&gt;


	&lt;p&gt;Take alternative implementations as another example. Ruby has Rubinius, JRuby, IronRuby, Ruby.NET, what does Perl have ? JRuby and IronRuby are particularly interesting ventures, since they are backed by the enterprise. JRuby's developers were hired by Sun to create a natural binding to Java on the &lt;span class="caps"&gt;JVM&lt;/span&gt;, and IronRuby is being deveoped by, no less, Microsoft's Dynamic Language Runtime team, to bind it to the .NET framework. At last, the big two enterprise solutions &amp;#8211; Java and .NET realized the importance of dynamic scripting languages, and did they choose Perl ? No, they chose Ruby&lt;sup&gt;&lt;a href="#fn4"&gt;4&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;


	&lt;p&gt;So why does this happen ? Why is Perl stagnant while Ruby is stronly moving forward&lt;sup&gt;&lt;a href="#fn5"&gt;5&lt;/a&gt;&lt;/sup&gt;. Is it only because Ruby's core is better ? Is it because of Rails ? &lt;span class="caps"&gt;IMHO&lt;/span&gt; there's another reason, and that is Perl 6. With it hovering around, no one &lt;em&gt;really&lt;/em&gt; wants to improve Perl 5 any more. Perl 5.5 was released one year after 5.4, 5.6 was released 20 months after that, 5.8 took 28 months, 5.10 &amp;#8211; more than 5 years. Do you notice the tendency ?&lt;/p&gt;


	&lt;p&gt;Recall the old vs. new employee metaphor. Is Perl 6 the reason for Perl 5's stagnation &amp;#8211; I doubt it is, but it is definitely a factor. Just how much motivation would you have working hard on somethinig you know will be thrown out &lt;em&gt;soon&lt;/em&gt;. And now imagine this &lt;em&gt;soon&lt;/em&gt; lasts for years and years&lt;sup&gt;&lt;a href="#fn6"&gt;6&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;


	&lt;h4&gt;Conclusion&lt;/h4&gt;


	&lt;p&gt;It's about time to conclude this rant. Which should I choose ? Should I replace Perl as my go-to guy with Ruby ?&lt;/p&gt;


	&lt;p&gt;The answer to this question is still no, but this time with a reservation. I enjoy writing Ruby code more than writing Perl code, at least until I ran into a piece of horrible documentation or a missing library which Perl has since 1995. But at the core, Ruby is better. And while the core is practically impossible to change (see how long Perl 6 is taking) all the peripherials are easier. In the past two years, Ruby has closed many gaps on Perl in terms of documentation, libraries and community. I think that with the current trends continuing for another two or three years, Ruby will overtake Perl in popularity and will become an all around better solution.&lt;/p&gt;


&lt;img class="align-center" src="https://eli.thegreenplace.net/images/hline.jpg" style="width: 320px; height: 5px;" /&gt;

	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Real football (i.e. soccer in the US).&lt;/p&gt;


	&lt;p id="fn2"&gt;&lt;sup&gt;2&lt;/sup&gt; They're all linked. I use the Google Group interface because I find it the most convenient.&lt;/p&gt;


	&lt;p id="fn3"&gt;&lt;sup&gt;3&lt;/sup&gt; The one I'm using for writing this article.&lt;/p&gt;


	&lt;p id="fn4"&gt;&lt;sup&gt;4&lt;/sup&gt; Well, there's also Jython (not backed by Sun, &lt;span class="caps"&gt;AFAIK&lt;/span&gt;) and IronPython (backed by MS). But this article compares Perl and Ruby, which &lt;span class="caps"&gt;IMHO&lt;/span&gt; have a somewhat similar philosophy that is a bit different from Python.&lt;/p&gt;


	&lt;p id="fn5"&gt;&lt;sup&gt;5&lt;/sup&gt; According to the &lt;a href="http://www.tiobe.com/tpci.htm"&gt;&lt;span class="caps"&gt;TIOBE&lt;/span&gt; index&lt;/a&gt;, Perl is currently at 5.4%, down from 8% 5 years ago, while Ruby is 2.3%, up from 0.25% 5 years ago.&lt;/p&gt;


	&lt;p id="fn6"&gt;&lt;sup&gt;6&lt;/sup&gt; Perl 6 was announced on July 19th, 2000.&lt;/p&gt;

    </content><category term="misc"></category><category term="Perl"></category><category term="Ruby"></category></entry><entry><title>DSL = Metalinguistic Abstraction</title><link href="https://eli.thegreenplace.net/2007/07/27/dsl-metalinguistic-abstraction" rel="alternate"></link><published>2007-07-27T14:23:26-07:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2007-07-27:/2007/07/27/dsl-metalinguistic-abstraction</id><summary type="html">
    &lt;p&gt;
        The idea of Domain Specific Languages (&lt;a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language"&gt;DSL&lt;/a&gt;)
        has been lately brought into attention by the freshly popular
        programming language &lt;a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29"&gt;Ruby&lt;/a&gt;.
    In particular, the famed web framework &lt;a href="http://en.wikipedia.org/wiki/Ruby_on_Rails"&gt;Ruby On Rails&lt;/a&gt;, which
is built as a DSL on top of Ruby has been a prime example. 
    &lt;/p&gt;&lt;p&gt;

What most people are not aware …&lt;/p&gt;</summary><content type="html">
    &lt;p&gt;
        The idea of Domain Specific Languages (&lt;a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language"&gt;DSL&lt;/a&gt;)
        has been lately brought into attention by the freshly popular
        programming language &lt;a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29"&gt;Ruby&lt;/a&gt;.
    In particular, the famed web framework &lt;a href="http://en.wikipedia.org/wiki/Ruby_on_Rails"&gt;Ruby On Rails&lt;/a&gt;, which
is built as a DSL on top of Ruby has been a prime example. 
    &lt;/p&gt;&lt;p&gt;

What most people are not aware of, however, is that DSLs is just a new
incarnation of the very old concept born in the world of Lisp called &lt;a href="http://en.wikipedia.org/wiki/Metalinguistic_abstraction"&gt;&lt;i&gt;Metalinguistic
Abstraction&lt;/i&gt;&lt;/a&gt;. It is a recurrent theme in the seminal MIT textbook, &lt;a href="http://en.wikipedia.org/wiki/SICP"&gt;Structure and Interpretation of
Computer Programs&lt;/a&gt; (SICP) that was written in 1985 to present general
computer programming concepts using the Scheme language (member of the Lisp
family).
    &lt;/p&gt;&lt;p&gt;

Metalinguistic Abstraction is the process of solving programming problems by
creating a new language  that is better suited to tackle these problems. Unlike
the traditional software engineering process of dividing problems to exactly
defined subproblems and solving those subproblems, metalinguistic abstraction
instructs the programmer to imagine a new programming language in which the
solution of these problems can be expressed most naturally, and then implement
this language.
    &lt;/p&gt;&lt;p&gt;

While this sounds a bit detached, Rails actually provides a good, modern example
of how such a concept can work. The designer of Rails asked himself "what
language do I need to write web applications more naturally". He then went
forward to implement this language on top of Ruby. Naturally, he could have
created a completely new programming language. However, building it on top of an
existing system provides several powerful benefits:
    &lt;/p&gt;&lt;p&gt;

&lt;ol&gt;
	&lt;li&gt;You don't need to craft a parser for the new language.&lt;/li&gt;
	&lt;li&gt;You don't have to make up new syntax, just use the existing syntax of the language you're building on top of.&lt;/li&gt;
	&lt;li&gt;Most importantly - since you are just building a layer on top of an existing programming language, all the facilities and power of this language are available to you, for free. This greatly enhances the immediate usefulness of the new DSL.&lt;/li&gt;
&lt;/ol&gt;
    &lt;/p&gt;&lt;p&gt;

Ruby is a good language to build DSLs in, and Rails is a prime example of this.
Ruby is better in this task than the other popular languages - Perl, Python,
C++, Java, C#, etc. However, it is much inferior to Lisp. This is in no way to
diminish Ruby's success or start a language war, I'm simply stating a fact. Lisp
has some unique concepts that make metalinguistic abstraction natural, namely
uniform syntax and macros. 
    &lt;/p&gt;&lt;p&gt;

Since metalinguistic abstraction is such a powerful programming technique, it is
highly recommended for any programmer to learn Lisp (whether it is &lt;a href="http://en.wikipedia.org/wiki/Common_lisp"&gt;Common Lisp&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Scheme_%28programming_language%29"&gt;Scheme&lt;/a&gt;
doesn't really matter). Even if you'll never use it for any official project,
just the act of learning it and understanding the programming techniques
possible in it will surely expand your mind and make you a better programmer.
&lt;/p&gt;
    </content><category term="misc"></category><category term="Lisp"></category><category term="Programming"></category><category term="Ruby"></category></entry><entry><title>Logical operators in Perl and Ruby</title><link href="https://eli.thegreenplace.net/2007/06/02/logical-operators-in-perl-and-ruby" rel="alternate"></link><published>2007-06-02T20:06:28-07:00</published><updated>2023-06-30T23:16:27-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2007-06-02:/2007/06/02/logical-operators-in-perl-and-ruby</id><summary type="html">
        &lt;p&gt;Both Perl and Ruby provide two sets of logical operators: &lt;/p&gt;
	&lt;ul&gt;
	&lt;li&gt;!, &amp;#38;&amp;#38; and || (inherited from C) &lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;not&lt;/strong&gt;, &lt;strong&gt;and&lt;/strong&gt; and &lt;strong&gt;or&lt;/strong&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Unfortunately, these two sets of operators are not isomorphic &amp;#8211; due to differences in precedence, their semantics are different in subtle ways which make the whole issue of using them error prone. In …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;Both Perl and Ruby provide two sets of logical operators: &lt;/p&gt;
	&lt;ul&gt;
	&lt;li&gt;!, &amp;#38;&amp;#38; and || (inherited from C) &lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;not&lt;/strong&gt;, &lt;strong&gt;and&lt;/strong&gt; and &lt;strong&gt;or&lt;/strong&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Unfortunately, these two sets of operators are not isomorphic &amp;#8211; due to differences in precedence, their semantics are different in subtle ways which make the whole issue of using them error prone. In this article I will try to elucidate this topic.&lt;/p&gt;

&lt;h4&gt;So what&amp;#39;s the difference between them?&lt;/h4&gt;

&lt;p&gt;In order to be able to disambiguate complex expressions, parsers of programming languages assign precedence for all operators. The best known example is probably the precedence of &lt;b&gt;*&lt;/b&gt; being higher than that of &lt;b&gt;+&lt;/b&gt; to allow commonly understood mathematical expressions without forcing to use lots of parentheses. Another good example is the precedence of assignment being lower than the precedence of all mathematical operators. This allows statements like &lt;code&gt;$a = $b + $c;&lt;/code&gt; to be parsed correctly without parentheses&lt;sup class="footnote"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;In Perl and in Ruby, the precedence of !, &amp;amp;&amp;amp; and || is higher than the precedence of assignment, while the precedence of &lt;strong&gt;not&lt;/strong&gt;, &lt;strong&gt;and&lt;/strong&gt; and &lt;strong&gt;or&lt;/strong&gt; is lower. &lt;/p&gt;

&lt;p&gt;For example, the following code in Perl:&lt;/p&gt;


&lt;pre&gt;
$a = $b || 5;
&lt;/pre&gt;



&lt;p&gt;Will assign the value of &lt;code&gt;$b&lt;/code&gt; to &lt;code&gt;$a&lt;/code&gt; when &lt;code&gt;$b&lt;/code&gt; evaluates to true, and 5 when &lt;code&gt;$b&lt;/code&gt; evaluates to false (&lt;code&gt;undef&lt;/code&gt;, 0 or an empty string). In short, it works as expected. However, the following code:&lt;/p&gt;


&lt;pre&gt;
$a = $b or 5;
&lt;/pre&gt;



&lt;p&gt;Is completely broken and will just generate a warning, because it is parsed as &lt;code&gt;($a = $b) or 5;&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;Why the low precedence ?&lt;/h4&gt;

&lt;p&gt;As the example above shows, the low precedence of &lt;strong&gt;or&lt;/strong&gt; is not such a good idea. So why was it made this way ? I think that Larry Wall wanted to make the following code legal Perl that behaves as expected:&lt;/p&gt;


&lt;pre&gt;
open my $fh, &amp;quot;&amp;lt;&amp;quot;, $filename or die $!;
&lt;/pre&gt;



&lt;p&gt;Perl allows to drop parentheses where the code is not ambiguous, and proponents of the language believe it makes some code more readable. The code section above will work correctly. With ||, on the other hand, it wouldn&amp;#39;t have worked since the precedence of || is higher than that of the comma, and the expression would be parsed as:&lt;/p&gt;


&lt;pre&gt;
open my $fh, &amp;quot;&amp;lt;&amp;quot;, ($filename || die $!);
&lt;/pre&gt;



&lt;p&gt;Which isn&amp;#39;t what we wanted. In Ruby, where code like the above is not written, this motivation plays no role. &lt;/p&gt;

&lt;h4&gt;Dealing with the confusion&lt;/h4&gt;

&lt;p&gt;So it appears Perl and Ruby have two sets of almost identical operators which behave differently sometimes. Isn&amp;#39;t this confusing ? &lt;/p&gt;

&lt;p&gt;It definitely is. In fact, many "good programming style" guides deal with this confusion in a very strict manner. For instance, Damian Conway&amp;#39;s excellent "Perl Best Practices" book advises to avoid the low precedence operators at all, except for the single case of:&lt;/p&gt;


&lt;pre&gt;
open my $fh, &amp;quot;&amp;lt;&amp;quot;, $filename or die $!;
&lt;/pre&gt;



&lt;p&gt;In Ruby this is irrelevant, so to extend his advice, it probably isn&amp;#39;t recommended to use these operators at all in Ruby, is it ? I personally hold a different view.&lt;/p&gt;

&lt;h4&gt;In defence of the low precedence operators&lt;/h4&gt;

&lt;p&gt;In my opinion, the low precedence operators are better, because of two main reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Not everyone came to Perl and Ruby from C and C++. On the contrary, as the time goes on, less and less people come with this background, and many start from other languages (in lots of cases, from Perl and Ruby themselves). But even for a seasoned C hacker, &lt;span class="caps"&gt;IMHO &lt;/span&gt;&lt;code&gt;if not client.done? or client.result &gt; 0&lt;/code&gt; is more intelligible than &lt;code&gt;if !client.done? || client.result &gt; 0&lt;/code&gt;, since it is more English-like.&lt;/li&gt;
&lt;li&gt;An important factor, at least for me, is the speed and comfort of typing on the keyboard. Typing too many punctuation characters is both slower and more stressful on the wrists. Using &lt;strong&gt;not&lt;/strong&gt;, &lt;strong&gt;and&lt;/strong&gt; and &lt;strong&gt;or&lt;/strong&gt; wherever possible instead of !, &amp;amp;&amp;amp; and || definitely helps me put as little stress as possible on my tendons.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;But...&lt;/h4&gt;

However, as we have seen, some things just can&amp;#8217;t be done with the low precedence operators without resorting to parentheses. So for the singular case of the short-circuit assignment perhaps using || is better than &lt;strong&gt;or&lt;/strong&gt; (Ruby code in this case, although it of course applies to Perl as well):
&lt;pre&gt;
a = b || 5;
&lt;/pre&gt;

Implementing the same functionality with &lt;strong&gt;or&lt;/strong&gt; feels a little superfluous:
&lt;pre&gt;
a = (b or 5);
&lt;/pre&gt;

And || can also be used in the occasionally useful idiom&lt;sup&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;:
&lt;pre&gt;
var ||= 5;
&lt;/pre&gt;

	&lt;p&gt;&lt;strong&gt;or&lt;/strong&gt; has no answer in this case.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m sure, however, that there is much more logical condition code that short-circuit assignment code, so the low precedence operators can be used most of the time.&lt;/p&gt;


	&lt;h4&gt;Another little gotcha&lt;/h4&gt;


There is another little gotcha about low precedence operators which is mostly relevant for people with C background. &lt;strong&gt;or&lt;/strong&gt; and &lt;strong&gt;and&lt;/strong&gt; have the same precedence (unlike ||, which has a lower precedence than &amp;#38;&amp;#38;), so writing code like:
&lt;pre&gt;
if cond1 and cond2 or cond3 and cond4
&lt;/pre&gt;

	&lt;p&gt;Won&amp;#8217;t do what you expected. However, it is a good practice to use parentheses in all complex conditions of this kind anyway, so I don&amp;#8217;t see it as a big problem.&lt;/p&gt;


	&lt;h4&gt;Conclusion&lt;/h4&gt;


	&lt;p&gt;Perl and Ruby have two sets of logical operators. These two sets have slightly different semantics, but the confusion can be avoided with a disciplined application of good programming practices. My advice is to use the low precedence operators (&lt;strong&gt;not&lt;/strong&gt;, &lt;strong&gt;and&lt;/strong&gt; and &lt;strong&gt;or&lt;/strong&gt;) all the time. The single exception can be made in the case of short-circuit assignment, where || can be used instead of &lt;strong&gt;or&lt;/strong&gt; (although &lt;strong&gt;or&lt;/strong&gt; can also be used, with help from a couple of parentheses).&lt;/p&gt;


	&lt;h4&gt;Footnotes&lt;/h4&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Had the precedence of assignment been higher than the precedence of addition, this statement would be understood as &lt;code&gt;($a = $b) + $c;&lt;/code&gt;.&lt;/p&gt;


	&lt;p id="fn2"&gt;&lt;sup&gt;2&lt;/sup&gt; This translates to &lt;code&gt;var = var || 5&lt;/code&gt;, or &amp;#8220;if &lt;code&gt;var&lt;/code&gt; is defined, let it keep its value, otherwise assign 5 to it&amp;#8221;.&lt;/p&gt;

    </content><category term="misc"></category><category term="Perl"></category><category term="Ruby"></category></entry><entry><title>irb tab completion</title><link href="https://eli.thegreenplace.net/2007/05/07/irb-tab-completion" rel="alternate"></link><published>2007-05-07T17:49:13-07:00</published><updated>2025-05-31T13:46:04-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2007-05-07:/2007/05/07/irb-tab-completion</id><summary type="html">
      &lt;p&gt;
irb is the interactive Ruby shell - an incredibly useful tool during development. Lisp folks are proud of the REPL (Read-Evaluate-Print Loop) - well, irb is REPL for Ruby.
      &lt;/p&gt;&lt;p&gt;

What turns irb into an even more useful instrument, however, is tab completion. First, it makes coding quicker by completing known symbols (kind …&lt;/p&gt;</summary><content type="html">
      &lt;p&gt;
irb is the interactive Ruby shell - an incredibly useful tool during development. Lisp folks are proud of the REPL (Read-Evaluate-Print Loop) - well, irb is REPL for Ruby.
      &lt;/p&gt;&lt;p&gt;

What turns irb into an even more useful instrument, however, is tab completion. First, it makes coding quicker by completing known symbols (kind of an Intellisense, if you'd like). Second, and more importantly, it allows you to quickly see which methods are implemented for objects, by typing:
      &lt;/p&gt;&lt;p&gt;

&lt;pre&gt;
irb(main):001:0&gt; some_object.
&lt;/pre&gt;

      &lt;/p&gt;&lt;p&gt;
And pressing TAB twice, irb will list all the methods.
      &lt;/p&gt;&lt;p&gt;

For some reason, however, irb doesn't come with tab completion out of the box. To set it up, you can create a batch file called irbb.bat, for instance, which calls:
      &lt;/p&gt;&lt;p&gt;

&lt;pre&gt;
irb -r irb/completion
&lt;/pre&gt;

      &lt;/p&gt;&lt;p&gt;
And call that instead of irb. A simpler method, IMHO, is directly edit the irb.bat file in the Ruby installation bin/ directory to &lt;i&gt;require&lt;/i&gt; irb/completion. 
      &lt;/p&gt;&lt;p&gt;

P.S: I describe Windows solutions. It should be very similar for Linux.
      &lt;/p&gt;
    </content><category term="misc"></category><category term="Ruby"></category></entry><entry><title>Useful resources for using YAML in Ruby</title><link href="https://eli.thegreenplace.net/2007/04/13/useful-resources-for-using-yaml-in-ruby" rel="alternate"></link><published>2007-04-13T15:46:24-07:00</published><updated>2023-02-04T13:41:52-08:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2007-04-13:/2007/04/13/useful-resources-for-using-yaml-in-ruby</id><summary type="html">
        As I mentioned &lt;a href="https://eli.thegreenplace.net/2006/03/28/xml-or-yaml-for-configuration-files/"&gt;here&lt;/a&gt;, I had a choice of using either XML or YAML for the configuration files in the new version of ESMS. YAML won for two reasons:

&lt;ol&gt;
	&lt;li&gt;It is much more lightweight and looks natural. Big configuration files in XML become complex and cluttered, while YAML scales admirably …&lt;/li&gt;&lt;/ol&gt;</summary><content type="html">
        As I mentioned &lt;a href="https://eli.thegreenplace.net/2006/03/28/xml-or-yaml-for-configuration-files/"&gt;here&lt;/a&gt;, I had a choice of using either XML or YAML for the configuration files in the new version of ESMS. YAML won for two reasons:

&lt;ol&gt;
	&lt;li&gt;It is much more lightweight and looks natural. Big configuration files in XML become complex and cluttered, while YAML scales admirably in this respect.&lt;/li&gt;
	&lt;li&gt;The current de-facto XML implementation in Ruby is REXML, which I dislike. REXML is useful for employing XML as a serialization format, but is less good as a configuration file reader, because its error handling is very bad. On the other hand, &lt;a href="http://whytheluckystiff.net/syck/"&gt;Syck&lt;/a&gt; (the built-in YAML parser / emitter in Ruby) is easy to use, fast and has good error handling and reporting.&lt;/li&gt;
&lt;/ol&gt;

The following are good resources for YAML in Ruby:

&lt;ol&gt;
	&lt;li&gt;&lt;a href="http://yaml4r.sourceforge.net/cookbook/"&gt;Yaml cookbook&lt;/a&gt; - shows probably all you'll ever need to understand how Yaml entities map to Ruby code. Very useful.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://yaml4r.sourceforge.net/doc/"&gt;Yaml.rb docs&lt;/a&gt; - A reference manual with examples for the Yaml.rb library.&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://yaml.org/spec/current.html"&gt;YAML specification&lt;/a&gt; - from the official YAML website (&lt;a href="http://www.yaml.org"&gt;yaml.org&lt;/a&gt;). A crisp yet well written reference to the YAML syntax and semantics.&lt;/li&gt;



&lt;/ol&gt;


    </content><category term="misc"></category><category term="Ruby"></category></entry></feed>