Nick Sieger: Obscure and Ugly Perlisms in Rubytag:blog.nicksieger.com,2005:TypoTypo2007-10-10T04:51:32+00:00naurn:uuid:b5b394e6-fdce-46d1-8a1b-9da77dfa0cf62007-10-10T04:51:32+00:002007-10-10T04:51:32+00:00Comment on Obscure and Ugly Perlisms in Ruby by na<p>I’m a long-time Perl developer and have just recently switched to Ruby full-time.</p>
<p>I love it. Great language. I’m surprised at how many Ruby developers are ignorant of much Perl has been incorporated. It’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’t. I think most of that criticism comes from people versed in other more traditional languages who never really learned Perl well.</p>bjcurn:uuid:5f8fe2e5-ffce-4b11-82b1-830075d682ab2007-10-10T03:12:35+00:002007-10-10T03:12:35+00:00Comment on Obscure and Ugly Perlisms in Ruby by bjc<p>Your accusation of the “flip-flor” is way off.</p>
<p>“This program, when run with itself as an argument, prints out everything between BEGIN and END.”</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 “flip-flop” is useful.</p>andreasurn:uuid:86012eb6-a6c0-4a56-af33-0188a7726df42007-10-09T22:08:39+00:002007-10-09T22:08:39+00:00Comment on 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>andreasurn:uuid:214ed132-33f6-4962-84ee-258cb79663662007-10-09T21:26:47+00:002007-10-09T21:26:47+00:00Comment on 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>Michael Bar-Sinaiurn:uuid:b918f9ef-6d9d-42e2-acf0-eb483e1aa8f52007-10-09T11:23:23+00:002007-10-09T11:23:23+00:00Comment on Obscure and Ugly Perlisms in Ruby by Michael Bar-Sinai<p>Good post. Here’s my “favorite”.
The ||= operator. Generally it means “populate a variable with a value unless the variable is already populated”. </p>
<p>irb(main):029:0> a_var = 3
=> 3
irb(main):030:0> a_var
=> 3
irb(main):031:0> a_var ||=”four”
=> 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’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>13azurn:uuid:c1fcf63f-587a-4a9a-bf8f-66b305dae23b2007-10-08T16:28:18+00:002007-10-08T16:28:18+00:00Comment on Obscure and Ugly Perlisms in Ruby by 13az<p>Re: first comment….</p>
<p> I think you misspelled your title:</p>
<p> <i>Obscure and <b>Beautiful</b> Perlisms in Ruby</i></p>
<p>Has most peeps have already mentioned these aren’t all Perlisms (historically) and Perl does more things with these “Perlisms” than the context provided in your code. </p>
<p>If Ruby is the bastard son of Perl then please treat it’s lineage with more respect ;-)</p>Rogerurn:uuid:482d1099-62dc-4bfe-9016-e27ca97d2da22007-10-08T07:15:58+00:002007-10-08T07:15:58+00:00Comment on Obscure and Ugly Perlisms in Ruby by Roger<p>Wow, a blogger ignorant of both basic Ruby and Perl! While it’s fine posing questions about a language’s syntax while you are learning it, claiming that what you don’t understand is ‘obscure’, ‘ugly’ and flawed is a red flag of a true poser.</p>Mark Thomasurn:uuid:cb0af73b-2817-4796-9856-35cf62cc596e2007-10-07T18:46:30+00:002007-10-07T18:46:30+00:00Comment on Obscure and Ugly Perlisms in Ruby by Mark Thomas<p>I’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>Bruno Goncalvesurn:uuid:cb45a2ac-9912-4039-a7fe-8f771c40a7a72007-10-07T17:57:07+00:002007-10-07T17:57:07+00:00Comment on 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>Clausurn:uuid:e00e710e-f0ed-47ae-ab76-b800db5014172007-10-07T16:52:15+00:002007-10-07T16:52:15+00:00Comment on Obscure and Ugly Perlisms in Ruby by Claus<p><>, BEGIN/END and defined are natural and non-ugly in perl. It’s hard to convince a perl hacker that anything valuable is gained by throwing out the brevity of file I/O perl has. You’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>Marsvinurn:uuid:031bc62d-52a6-4328-a6f4-0af4c9ee0a5b2007-10-07T16:34:19+00:002007-10-07T16:34:19+00:00Comment on Obscure and Ugly Perlisms in Ruby by Marsvin<p>I’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>Matthieu Riouurn:uuid:6acd6a8c-0aff-4f81-880d-f0b2bbb9c6bf2007-10-07T16:19:23+00:002007-10-07T16:19:23+00:00Comment on Obscure and Ugly Perlisms in Ruby by Matthieu Riou<p>Mmmh the formatting didn’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’t a hash, it’s the result produced by irb when you ask for the value of foo.</p>riffraffurn:uuid:ae386eeb-c1fb-42a4-8d38-e6086892e3c72007-10-07T16:04:38+00:002007-10-07T16:04:38+00:00Comment on Obscure and Ugly Perlisms in Ruby by riffraff<p>my 2c: I agree that the flip flop operator is rather obscure anod doesn’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’re at perlisms the example could also use $_ and regexps in conditionals :)</p>Matthieu Riouurn:uuid:5a352ded-7248-43d8-967a-6e2cc1c98ae62007-10-07T15:40:34+00:002007-10-07T15:40:34+00:00Comment on Obscure and Ugly Perlisms in Ruby by Matthieu Riou<p>What about this?</p>
<p>$irb</p>
<blockquote>
<p>foo = “foo” unless defined?(foo)
foo
=> nil</p>
</blockquote>
<p>But:</p>
<p>$irb</p>
<blockquote>
<p>unless defined?(foo)
foo = “foo”
end
foo
=> “foo”</p>
</blockquote>Ola Biniurn:uuid:b76d837a-7785-4bfa-b564-d11312c32be12007-10-07T13:20:58+00:002007-10-07T13:20:58+00:00Comment on 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>Kozurn:uuid:c9ff849b-698b-4d65-81b3-0f1e500487fb2007-10-07T08:24:09+00:002007-10-07T08:24:09+00:00Comment on Obscure and Ugly Perlisms in Ruby by Koz<p>Yeah, defined? is really useful when writing ‘really reflective’ code that gets used in places you don’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’t seem particularly useful. I thought it was a boolean until I used it in irb one day :).</p>Nathanurn:uuid:1476b158-a389-493d-8985-b920d9b00d4e2007-10-07T06:01:36+00:002007-10-07T06:01:36+00:00Comment on 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’d expect.</p>
<p>One of the places I’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> Robert Fischerurn:uuid:0a912094-ee61-4748-9d41-a1e9d88769f82007-10-07T05:19:58+00:002007-10-07T05:19:58+00:00Comment on 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’ve never found a lot of use for END blocks, although I’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’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’s the kind of thing that seems worthless and silly until it is critical and necessary.</p>Steve Stoneurn:uuid:b015f7aa-b06c-434c-9b33-bfe8f670b84c2007-10-07T04:53:20+00:002007-10-07T04:53:20+00:00Comment on 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… though they’re really more useful for manipulating the interpreter at compile time. It’s important to understand the difference between “gets executed as the first thing in this code” versus “gets executed before any of the rest of this code is even compiled”.</p>Sixpack dudeurn:uuid:aea5a0b9-1f54-4f5a-bed9-2c4d609b25072007-10-07T03:46:02+00:002007-10-07T03:46:02+00:00Comment on Obscure and Ugly Perlisms in Ruby by Sixpack dude<p>Ex. A is an ‘awk-ism’ rather than a perl-ism. Read a book on Awk first, dude.</p>Nickurn:uuid:d6febfab-76b0-47db-ad7e-8da5692221af2007-10-07T03:32:08+00:002007-10-07T03:32:08+00:00Comment on Obscure and Ugly Perlisms in Ruby by Nick<p>Nathan: Careful:</p>
<pre><code>defined?(false) => "false"
defined?(nil) => "nil"
</code></pre>
<p>Maybe that’s not what you meant. As for its utility, I’ll wait until I see an example (although I’m not denying your claim that there is one). I couldn’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’t know why I initially said Perl is a “distant relative” – I think I was getting overly dramatic with my prose. I’ve corrected that, as well as a side comment about quines – someone seemed to think that was evidence I don’t know what they are. Whatever, it wasn’t adding anything, so it’s gone.</p>xan/hot@hot.comurn:uuid:08a738f5-1b63-407f-9462-399a988b08fd2007-10-06T22:50:56+00:002007-10-06T22:50:56+00:00Comment on 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 ‘;’ 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>Nathanurn:uuid:5c0488eb-8b9f-4413-9c1e-85f2c40f43932007-10-06T22:11:52+00:002007-10-06T22:11:52+00:00Comment on Obscure and Ugly Perlisms in Ruby by Nathan<p><code>defined?</code> comes in handy sometimes. I’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’s defined as what.</p>Jeremy McAnallyurn:uuid:8e7e3e5d-4979-402f-97ee-76e7ed6697082007-10-06T13:45:16+00:002007-10-06T13:45:18+00:00Comment on 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>Nick Siegerurn:uuid:db343d2c-b937-4493-b803-2f1d28a2124e2007-10-06T12:39:00+00:002007-10-08T14:49:41+00:00Obscure and Ugly Perlisms in Ruby<p>So, it’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 “obscure” in the title because, in my experience, they are obscure. “Ugly” 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’t deny its usefulness for pure scripting tasks. I just don’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">"</span><span class="string">Bye!</span><span class="punct">"</span>
<span class="punct">}</span>
<span class="ident">puts</span> <span class="punct">"</span><span class="string">Processing...</span><span class="punct">"</span>
<span class="constant">BEGIN</span> <span class="punct">{</span>
<span class="ident">puts</span> <span class="punct">"</span><span class="string">One moment while I start your program</span><span class="punct">"</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’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: <> (ARGF)</h3>
<p>Thank goodness we didn’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’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’m not convinced it adds any value – 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">"</span><span class="string">Constant</span><span class="punct">"</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">"</span><span class="string">const : <span class="expr">#{defined?(Constant)}</span></span><span class="punct">"</span>
<span class="ident">puts</span> <span class="punct">"</span><span class="string">ivar : <span class="expr">#{defined?(@ivar)}</span></span><span class="punct">"</span>
<span class="ident">puts</span> <span class="punct">"</span><span class="string">global: <span class="expr">#{defined?($0)}</span></span><span class="punct">"</span>
<span class="ident">puts</span> <span class="punct">"</span><span class="string">local : <span class="expr">#{defined?(integer)}</span></span><span class="punct">"</span>
<span class="ident">puts</span> <span class="punct">"</span><span class="string">expr : <span class="expr">#{defined?(Constant + integer)}</span></span><span class="punct">"</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’t already plainly obvious what is <code>defined?</code>.</p>
<p>Any other obscure features in Ruby that you love to hate?</p>