Nick Sieger: Obscure and Ugly Perlisms in Ruby http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby en-us 40 "Obscure and Ugly Perlisms in Ruby" by na <p>I&#8217;m a long-time Perl developer and have just recently switched to Ruby full-time.</p> <p>I love it. Great language. I&#8217;m surprised at how many Ruby developers are ignorant of much Perl has been incorporated. It&#8217;s a very simple switch from Perl to Ruby.</p> <p>I think there are a lot of great features of Ruby and hopefully it will get a bit faster (ahem).</p> <p>I never found Perl obscure and still don&#8217;t. I think most of that criticism comes from people versed in other more traditional languages who never really learned Perl well.</p> Wed, 10 Oct 2007 04:51:32 +0000 urn:uuid:b5b394e6-fdce-46d1-8a1b-9da77dfa0cf6 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-330 "Obscure and Ugly Perlisms in Ruby" by bjc <p>Your accusation of the &#8220;flip-flor&#8221; is way off.</p> <p>&#8220;This program, when run with itself as an argument, prints out everything between BEGIN and END.&#8221;</p> <p>Which is exactly what the toggle code does.</p> <p>Your ruby code, on the other hand, prints out all the lines between 5 and 10, which in <em>this example</em> happens to be those lines between BEGIN and END.</p> <p>To call the programs equivalent would be a huge stretch of the word. And it also happens to show exactly why the &#8220;flip-flop&#8221; is useful.</p> Wed, 10 Oct 2007 03:12:35 +0000 urn:uuid:5f8fe2e5-ffce-4b11-82b1-830075d682ab http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-329 "Obscure and Ugly Perlisms in Ruby" by andreas <p>@Michael Bar-Sinai</p> <p>The <code>||=</code> Operator in <br> <code>a ||= b</code> <br> stands for:<br> <code> a = a || b </code> <br> Meaning: Set <code>a = a</code> if <code>a</code> is <i>not</i> <code>nil</code> or <code>false</code>, otherwise set <code>a = b</code> (to the second argument of the exclusive logical or operator, short-circuit evaluation). <br>The second argument <code>b</code> of the logical OR <code>||</code> operator will only be evaluated if the first argument <code>a</code> is <code>false</code> (or <code>nil</code>).</p> Tue, 09 Oct 2007 22:08:39 +0000 urn:uuid:86012eb6-a6c0-4a56-af33-0188a7726df4 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-328 "Obscure and Ugly Perlisms in Ruby" by andreas <p>@Michael Bar-Sinai</p> <p><code> irb(main):038:0> f_var = false </code> <br> <code> => false </code></p> <p><code> irb(main):039:0> t_var = :somethingOtherValueExceptNilAndFalse </code> <br> <code> => :somethingOtherValueExceptNilAndFalse </code></p> <p><code> irb(main):040:0> t_var </code> <br> <code> => :somethingOtherValueExceptNilAndFalse </code></p> <p><code> irb(main):041:0> f_var ||= true </code> <br> <code> => true </code></p> <p><code> irb(main):042:0> t_var ||= true </code> <br> <code> => :somethingOtherValueExceptNilAndFalse </code> <br><br> <b>So be careful with the BooleanGang!</b></p> Tue, 09 Oct 2007 21:26:47 +0000 urn:uuid:214ed132-33f6-4962-84ee-258cb7966366 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-327 "Obscure and Ugly Perlisms in Ruby" by Michael Bar-Sinai <p>Good post. Here&#8217;s my &#8220;favorite&#8221;. The ||= operator. Generally it means &#8220;populate a variable with a value unless the variable is already populated&#8221;. </p> <p>irb(main):029:0> a_var = 3 => 3 irb(main):030:0> a_var => 3 irb(main):031:0> a_var ||=&#8221;four&#8221; => 3 irb(main):032:0> a_var => 3</p> <p>So far so good, right?</p> <p>irb(main):033:0> a_var = false => false irb(main):034:0> a_var => false irb(main):035:0> a_var ||=true => true irb(main):036:0> a_var => true</p> <p>Now imagine you have a view that uses a (hopefully) boolean instance variable set in the controller (we&#8217;re talking rails here), and you want to set it to true if it is not already set. So you use ||=. So you spend the next 3 hours banging your head at the keyboard.</p> Tue, 09 Oct 2007 11:23:23 +0000 urn:uuid:b918f9ef-6d9d-42e2-acf0-eb483e1aa8f5 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-326 "Obscure and Ugly Perlisms in Ruby" by 13az <p>Re: first comment&#8230;.</p> <p>&nbsp; I think you misspelled your title:</p> <p>&nbsp; <i>Obscure and <b>Beautiful</b> Perlisms in Ruby</i></p> <p>Has most peeps have already mentioned these aren&#8217;t all Perlisms (historically) and Perl does more things with these &#8220;Perlisms&#8221; than the context provided in your code. </p> <p>If Ruby is the bastard son of Perl then please treat it&#8217;s lineage with more respect ;-)</p> Mon, 08 Oct 2007 16:28:18 +0000 urn:uuid:c1fcf63f-587a-4a9a-bf8f-66b305dae23b http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-325 "Obscure and Ugly Perlisms in Ruby" by Roger <p>Wow, a blogger ignorant of both basic Ruby and Perl! While it&#8217;s fine posing questions about a language&#8217;s syntax while you are learning it, claiming that what you don&#8217;t understand is &#8216;obscure&#8217;, &#8216;ugly&#8217; and flawed is a red flag of a true poser.</p> Mon, 08 Oct 2007 07:15:58 +0000 urn:uuid:482d1099-62dc-4bfe-9016-e27ca97d2da2 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-324 "Obscure and Ugly Perlisms in Ruby" by Mark Thomas <p>I&#8217;ve used BEGIN and END in perl and found them very useful. In a persistent environment (PPerl, PerPerl, etc), the BEGIN clause is executed once only, whereas the body of the code is executed N times, with variables persisting between invocations. This lets you use BEGIN to establish connection pools, parse config files, and other one-time-only things. Likewise tear-down can go into END.</p> Sun, 07 Oct 2007 18:46:30 +0000 urn:uuid:cb0af73b-2817-4796-9856-35cf62cc596e http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-322 "Obscure and Ugly Perlisms in Ruby" by Bruno Goncalves <p>The BEGIN and END clauses seem to have come straight out of awk where they actually make sense. </p> <p><a href='http://www.bgoncalves.com/notes/2007/04/16/gawk-for-dummies-part-i/' rel="nofollow">http://www.bgoncalves.com/notes/2007/04/16/gawk-for-dummies-part-i/</a></p> <p>There they are executed before the first file is read and after the last file is read, respectively. Of course, in a more general purpose language they are mostly useless.</p> Sun, 07 Oct 2007 17:57:07 +0000 urn:uuid:cb45a2ac-9912-4039-a7fe-8f771c40a7a7 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-321 "Obscure and Ugly Perlisms in Ruby" by Claus <p>&lt;>, BEGIN/END and defined are natural and non-ugly in perl. It&#8217;s hard to convince a perl hacker that anything valuable is gained by throwing out the brevity of file I/O perl has. You&#8217;re missing the point of BEGIN/END - they are guaranteed to run, so they capture failure/exception conditions also, which makes them essential. Obviously defined is of less value in a pure OO language.</p> Sun, 07 Oct 2007 16:52:15 +0000 urn:uuid:e00e710e-f0ed-47ae-ab76-b800db501417 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-320 "Obscure and Ugly Perlisms in Ruby" by Marsvin <p>I&#8217;m no expert, but I think BEGIN is used in Perl modules to do fancy stuff with the syntax tree and END is used to clean up things that the user of the module may not know about.</p> Sun, 07 Oct 2007 16:34:19 +0000 urn:uuid:031bc62d-52a6-4328-a6f4-0af4c9ee0a5b http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-319 "Obscure and Ugly Perlisms in Ruby" by Matthieu Riou <p>Mmmh the formatting didn&#8217;t quite work (guess I should have used pre blocks). The first snippet is with an unless as a single, the second inside a block. The => isn&#8217;t a hash, it&#8217;s the result produced by irb when you ask for the value of foo.</p> Sun, 07 Oct 2007 16:19:23 +0000 urn:uuid:6acd6a8c-0aff-4f81-880d-f0b2bbb9c6bf http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-318 "Obscure and Ugly Perlisms in Ruby" by riffraff <p>my 2c: I agree that the flip flop operator is rather obscure anod doesn&#8217;t deserve special syntax (it can be factored out into a class) but his behaviour is quite useful, and the equivalent ruby code without the flip flop hardly looks shorter and cleaner:</p> <p>tmp = false ARGF.each_line do |line| if tmp puts line tmp = !(line =~ /^# END/) else tmp = (line =~ /^# BEGIN/) end end</p> <p>Anyway while we&#8217;re at perlisms the example could also use $_ and regexps in conditionals :)</p> Sun, 07 Oct 2007 16:04:38 +0000 urn:uuid:ae386eeb-c1fb-42a4-8d38-e6086892e3c7 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-317 "Obscure and Ugly Perlisms in Ruby" by Matthieu Riou <p>What about this?</p> <p>$irb</p> <blockquote> <p>foo = &#8220;foo&#8221; unless defined?(foo) foo => nil</p> </blockquote> <p>But:</p> <p>$irb</p> <blockquote> <p>unless defined?(foo) foo = &#8220;foo&#8221; end foo => &#8220;foo&#8221;</p> </blockquote> Sun, 07 Oct 2007 15:40:34 +0000 urn:uuid:5a352ded-7248-43d8-967a-6e2cc1c98ae6 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-316 "Obscure and Ugly Perlisms in Ruby" by Ola Bini <p>Hi Nick,</p> <p>I kinda agree with some of what you write - defined? is totally more gross than it needs to be, but highly useful in some cases. For example, doing defined? on constants can be very nice. The string result is totally unnecessary, though.</p> <p>BEGIN can be useful in libraries sometimes, while END seems to not give anything more than at_exit.</p> <p>Flip-flops should DIEDIEDIE.</p> Sun, 07 Oct 2007 13:20:58 +0000 urn:uuid:b76d837a-7785-4bfa-b564-d11312c32be1 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-315 "Obscure and Ugly Perlisms in Ruby" by Koz <p>Yeah, defined? is really useful when writing &#8216;really reflective&#8217; code that gets used in places you don&#8217;t intend. For example the new routing optimisation code uses if defined?(request) to know if it can rely on using the request object to provide the host, protocol and port. </p> <p>However the particular string values returned don&#8217;t seem particularly useful. I thought it was a boolean until I used it in irb one day :).</p> Sun, 07 Oct 2007 08:24:09 +0000 urn:uuid:c9ff849b-698b-4d65-81b3-0f1e500487fb http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-314 "Obscure and Ugly Perlisms in Ruby" by Nathan <p>Nick:</p> <p><code>defined?(false)</code> and <code>defined?(nil)</code> return strings, so something like</p> <pre>if defined?(nil) puts "It's defined!" else puts "It's not." end</pre> <p>works as you&#8217;d expect.</p> <p>One of the places I&#8217;ve used it is when writing plugins for Rails that can also operate outside of Rails. In this case I might do</p> <pre>if defined?(ActiveRecord) class ActiveRecord::Base # Add relevant functionality end end</pre> Sun, 07 Oct 2007 06:01:36 +0000 urn:uuid:1476b158-a389-493d-8985-b920d9b00d4e http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-313 "Obscure and Ugly Perlisms in Ruby" by Robert Fischer <p>BEGIN blocks are extremely useful for generating methods at run-time or other cute stunts. I&#8217;ve never found a lot of use for END blocks, although I&#8217;ve heard some people use them to make sure all the filehandles are closed before a program exits.</p> <p>In either case, you use them because you either 1) you don&#8217;t have access to main thread of code being executed, or 2) you want to execute something before any other code even has a chance to be executed.</p> <p>It&#8217;s the kind of thing that seems worthless and silly until it is critical and necessary.</p> Sun, 07 Oct 2007 05:19:58 +0000 urn:uuid:0a912094-ee61-4748-9d41-a1e9d88769f8 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-312 "Obscure and Ugly Perlisms in Ruby" by Steve Stone <p>I actually find END blocks very useful in one-liners, particularly with the -n and -p switches.</p> <p>BEGIN blocks can have similar uses&#8230; though they&#8217;re really more useful for manipulating the interpreter at compile time. It&#8217;s important to understand the difference between &#8220;gets executed as the first thing in this code&#8221; versus &#8220;gets executed before any of the rest of this code is even compiled&#8221;.</p> Sun, 07 Oct 2007 04:53:20 +0000 urn:uuid:b015f7aa-b06c-434c-9b33-bfe8f670b84c http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-311 "Obscure and Ugly Perlisms in Ruby" by Sixpack dude <p>Ex. A is an &#8216;awk-ism&#8217; rather than a perl-ism. Read a book on Awk first, dude.</p> Sun, 07 Oct 2007 03:46:02 +0000 urn:uuid:aea5a0b9-1f54-4f5a-bed9-2c4d609b2507 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-309 "Obscure and Ugly Perlisms in Ruby" by Nick <p>Nathan: Careful:</p> <pre><code>defined?(false) =&gt; "false" defined?(nil) =&gt; "nil" </code></pre> <p>Maybe that&#8217;s not what you meant. As for its utility, I&#8217;ll wait until I see an example (although I&#8217;m not denying your claim that there is one). I couldn&#8217;t find any usage of <code>defined?</code> in the standard library that used the result for anything other than a boolean test.</p> <p>xan: Maybe the Perl code should be here, but the point is not to compare Ruby to Perl at the syntax level. Also, the examples are pretty simplistic (maybe too much so) and the equivalent Perl is not hard to imagine. But thanks for pointing out the semi! At least you can tell I was futzing with Perl a little bit during the preparation of the article.</p> <p>Everyone else: I don&#8217;t know why I initially said Perl is a &#8220;distant relative&#8221; &#8211; I think I was getting overly dramatic with my prose. I&#8217;ve corrected that, as well as a side comment about quines &#8211; someone seemed to think that was evidence I don&#8217;t know what they are. Whatever, it wasn&#8217;t adding anything, so it&#8217;s gone.</p> Sun, 07 Oct 2007 03:32:08 +0000 urn:uuid:d6febfab-76b0-47db-ad7e-8da5692221af http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-308 "Obscure and Ugly Perlisms in Ruby" by xan/hot@hot.com <p>You should provide the perl version of the ruby code too if you claim it is a Perlism. </p> <p>Also, the &#8216;;&#8217; is not needed: integer = 10;</p> <p>Dont put more perl into ruby than in fact exists, I want to see the perl version of the code above too! ;-)</p> <p>Also $0 is $PROGRAM_NAME, lets use the longer names instead of the cryptic and hard-to-google perl legacy vars (they seem only useful for code golfing and one liners to me anyway)</p> Sat, 06 Oct 2007 22:50:56 +0000 urn:uuid:08a738f5-1b63-407f-9462-399a988b08fd http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-307 "Obscure and Ugly Perlisms in Ruby" by Nathan <p><code>defined?</code> comes in handy sometimes. I&#8217;ve even seen the output values used here and there. Since everything but <code>false</code> and <code>nil</code> evaluates to true, it might as well give extra information - and it can be very useful to be able to dynamically determine what&#8217;s defined as what.</p> Sat, 06 Oct 2007 22:11:52 +0000 urn:uuid:5c0488eb-8b9f-4413-9c1e-85f2c40f4393 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-306 "Obscure and Ugly Perlisms in Ruby" by Jeremy McAnally <p>I think you misspelled your title:</p> <p><i>Obscene and Ugly Perlisms in Ruby</i></p> <p>There we go.</p> Sat, 06 Oct 2007 13:45:16 +0000 urn:uuid:8e7e3e5d-4979-402f-97ee-76e7ed669708 http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby#comment-303 Obscure and Ugly Perlisms in Ruby <p>So, it&#8217;s well known that Ruby owes a debt to its predecessor Perl, although some (maybe many) question whether we should repay that debt or even go so far as to put Perl on trial and excise those elements which somehow haphazardly survived the generation gap. It turns out the evidence is mixed.</p> <p><em>Update: I use the word &#8220;obscure&#8221; in the title because, in my experience, they are obscure. &#8220;Ugly&#8221; is pure opinion, but this is my blog, after all.</em></p> <h3>Exhibit A: BEGIN/END</h3> <p><em>Update: Yes, yes, this is an awk-ism, not a perlism, strictly speaking. And I don&#8217;t deny its usefulness for pure scripting tasks. I just don&#8217;t see its utility in a larger application.</em></p> <div class="typocode"><pre><code class="typocode_ruby "><span class="constant">END</span> <span class="punct">{</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">Bye!</span><span class="punct">&quot;</span> <span class="punct">}</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">Processing...</span><span class="punct">&quot;</span> <span class="constant">BEGIN</span> <span class="punct">{</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">One moment while I start your program</span><span class="punct">&quot;</span> <span class="punct">}</span></code></pre></div> <p>Output:</p> <pre><code>One moment while I start your program Processing... Bye! </code></pre> <p>Why would any sane Ruby programmer do this? Have you ever seen a use for BEGIN that isn&#8217;t met by simply executing code at the top level of the main program? Geez, BEGIN even has its own node in the AST!</p> <p>And how about END? If you really need to hook into interpreter shutdown, just use <code>Kernel#at_exit</code>. (In fact, <a href="http://rubini.us/" title="Rubinius">Rubinius</a> currently uses END simply as an alias for <code>at_exit</code>.)</p> <h3>Exhibit B: &lt;> (ARGF)</h3> <p>Thank goodness we didn&#8217;t get the diamond operator in Ruby, but we did get ARGF as a replacement. Though obscure, it actually turns out to be useful. Consider this program, which prepends copyright headers in-place (thanks to another perlism, <code>-i</code>) to every file mentioned on the command-line. Any other creative uses of ARGF out there?</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="comment">#!/usr/bin/env ruby -i</span> <span class="constant">Header</span> <span class="punct">=</span> <span class="constant">DATA</span><span class="punct">.</span><span class="ident">read</span> <span class="constant">ARGF</span><span class="punct">.</span><span class="ident">each_line</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">e</span><span class="punct">|</span> <span class="ident">puts</span> <span class="constant">Header</span> <span class="keyword">if</span> <span class="constant">ARGF</span><span class="punct">.</span><span class="ident">pos</span> <span class="punct">-</span> <span class="ident">e</span><span class="punct">.</span><span class="ident">length</span> <span class="punct">==</span> <span class="number">0</span> <span class="ident">puts</span> <span class="ident">e</span> <span class="keyword">end</span> <span class="comment">__END__ #-- # Copyright (C) 2007 Fancypants, Inc. #++</span></code></pre></div> <h3>Exhibit C: The Flip-flop</h3> <p>This is a weird beast. I didn&#8217;t even know of its existence until <a href="http://headius.blogspot.com/2007/09/compiler-is-complete.html">Charlie</a> was complaining about having to compile it properly. Apparently we have Perl to thank for this nonsense as well (and, indirectly, <code>sed</code>). With the exception of the sed-ism, I&#8217;m not convinced it adds any value &#8211; in fact the code usually ends up looking more verbose.</p> <p>This program, when run with itself as an argument, prints out everything between BEGIN and END.</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="comment">#</span> <span class="comment"># BEGIN</span> <span class="constant">ARGF</span><span class="punct">.</span><span class="ident">each_line</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">line</span><span class="punct">|</span> <span class="keyword">if</span> <span class="punct">(</span><span class="ident">line</span> <span class="punct">=~</span> <span class="punct">/</span><span class="regex">^# BEGIN</span><span class="punct">/)..(</span><span class="ident">line</span> <span class="punct">=~</span> <span class="punct">/</span><span class="regex">^# END</span><span class="punct">/)</span> <span class="ident">puts</span> <span class="ident">line</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="comment"># END</span> <span class="comment"># </span></code></pre></div> <p>This snippet is a long-hand way to do <code>5.upto(10) {|i| puts i}</code>.</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="ident">i</span> <span class="punct">=</span> <span class="number">5</span> <span class="keyword">while</span> <span class="punct">(</span><span class="ident">i</span> <span class="punct">==</span> <span class="number">5</span><span class="punct">)...(</span><span class="ident">i</span> <span class="punct">==</span> <span class="number">10</span><span class="punct">)</span> <span class="keyword">do</span> <span class="ident">puts</span> <span class="ident">i</span> <span class="ident">i</span> <span class="punct">+=</span> <span class="number">1</span> <span class="keyword">end</span></code></pre></div> <h3>Exhibit D: Output from defined?</h3> <p>Not sure if this came from Perl.</p> <p><em>The basic need for <code>defined?</code> in a dynamic language is unquestionable. Instead, I meant to highlight the fact that <code>defined?</code> returns a string value here, which is strange.</em></p> <div class="typocode"><pre><code class="typocode_ruby "><span class="constant">Constant</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string">Constant</span><span class="punct">&quot;</span> <span class="attribute">@ivar</span> <span class="punct">=</span> <span class="punct">[</span><span class="number">1</span><span class="punct">,</span> <span class="number">2</span><span class="punct">,</span> <span class="number">3</span><span class="punct">]</span> <span class="ident">integer</span> <span class="punct">=</span> <span class="number">10</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">const : <span class="expr">#{defined?(Constant)}</span></span><span class="punct">&quot;</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">ivar : <span class="expr">#{defined?(@ivar)}</span></span><span class="punct">&quot;</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">global: <span class="expr">#{defined?($0)}</span></span><span class="punct">&quot;</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">local : <span class="expr">#{defined?(integer)}</span></span><span class="punct">&quot;</span> <span class="ident">puts</span> <span class="punct">&quot;</span><span class="string">expr : <span class="expr">#{defined?(Constant + integer)}</span></span><span class="punct">&quot;</span></code></pre></div> <p>Running this code produces:</p> <pre><code>const : constant ivar : instance-variable global: global-variable local : local-variable expr : method </code></pre> <p>Perl at least is sane enough to return true or false for its own <code>defined</code> operator. But <code>method</code>? Looking at the source, I see also <code>expression</code>, <code>local-variable(in-block)</code>, <code>assignment</code>, <code>class variable</code>, <code>true</code>, <code>false</code>, and <code>self</code>. But why would this output be useful? As if it isn&#8217;t already plainly obvious what is <code>defined?</code>.</p> <p>Any other obscure features in Ruby that you love to hate?</p> Sat, 06 Oct 2007 12:39:00 +0000 urn:uuid:db343d2c-b937-4493-b803-2f1d28a2124e Nick Sieger http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby ruby http://blog.nicksieger.com/articles/trackback/302