tag:blogger.com,1999:blog-87268049978330476852024-02-20T12:57:38.688-08:00Shaking the TreeThe half-baked thoughts of a computer programmer / system administrator and other nincompooperySpovichhttp://www.blogger.com/profile/04598664184344571923noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8726804997833047685.post-68279662607595699922008-01-18T21:45:00.001-08:002008-01-26T23:36:30.499-08:00Code Syntax-Highlighting on BloggerAny self-respecting coder will not blog about programming without proper code syntax-highlighting. If you use Blogger, I'll save you the trouble of googling... Good results <a href="http://www.quirkey.com/blog/2006/07/13/highlight-me-baby/">here</a> and <a href="http://hype-free.blogspot.com/2006/10/how-to-publish-good-looking-code-on.html">here</a>.<br /><br />As mentioned in the above blog links, I too am using TextMate on OS X which has a nifty built-in 'export to HTML' which, when combined with TextMate's themed CSS, gives very nice looking code blocks. You simply select the code you want highlighted, export it from TextMate, and paste into the HTML editor in Blogger.<br /><br />I saved the TextMate CSS into my blogger template, so now any <pre> tag will produce a pretty code block, and any pasted HTML code from TextMate will be blocked and colorized.<br /><br /><span style="font-weight: bold;">UPDATE:</span><br />Yet another (perhaps better) approach which also works quite well is a JavaScript project cleverly named <a href="http://code.google.com/p/syntaxhighlighter/">syntaxhighlighter</a>. I stumbled across this code when viewing <a href="http://www.rubytips.org/">RubyTips.org</a> which uses this library. The feature that I really liked is that it puts some nifty links at the top of each code block that allow copy to clipboard, and options to view the code in a separate window. The only downside is that because it is implemented with RegEx's in JavaScript, for large blocks of code, it will likely slow things down too much to be useful. But for short blocks of code, it is really a nice solution! Next time I post some code to this blog, I think I will give it a try!Spovichhttp://www.blogger.com/profile/04598664184344571923noreply@blogger.com2tag:blogger.com,1999:blog-8726804997833047685.post-47679425226634869352008-01-14T21:57:00.000-08:002008-01-18T21:20:28.233-08:00Ruby method return valuesThere is a generalization in ruby that the last expression evaluated will be the return value from a method if no explicit return is specified. The Ruby Pickaxe states in Chapter 6, "The return value of a method is the value of the last expression executed or the result of an explicit return expression."<br /><br />Although this is generally true, it can bite you as this is not exactly the case.<br /><br />For example, consider the following:<br /><pre class="textmate-source"><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">test1</span></span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> a<span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span><span class="constant constant_numeric constant_numeric_ruby">5</span> <span class="keyword keyword_control keyword_control_ruby">if</span> <span class="constant constant_language constant_language_ruby">true</span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">end</span><br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span><br /><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> test1<br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_numeric constant_numeric_ruby">5</span><br /></span></pre>That looks ok, and the return value of 5 shouldn't surprise you as that was the last expression evaluated. Now how about this (below)? What is the last expression evaluated here?<br /><pre class="textmate-source"><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">test2</span></span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> a<span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span><span class="constant constant_numeric constant_numeric_ruby">5</span> <span class="keyword keyword_control keyword_control_ruby">if</span> <span class="constant constant_language constant_language_ruby">false</span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">end</span><br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span><br /><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> test2<br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span></span></pre>Did you expect that result? I didn't. I would say 'false' is the last expression evaluated, so I would expect the return value to be false. But instead of false, I get nil.<br /><br />Why? It would seem that expressions evaluated as the controlling expression of a conditional statement (if, unless, etc.) don't count as an implied return value.<br /><br />Let's simplify this even more:<br /><pre class="textmate-source"><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">test3</span></span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">if</span> <span class="constant constant_numeric constant_numeric_ruby">2</span> <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">></span> <span class="constant constant_numeric constant_numeric_ruby">3</span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">end</span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">end</span><br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span><br /><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> test3<br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span></span></pre>So it would appear it doesn't matter. And again...<br /><pre class="textmate-source"><span class="source source_ruby"><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">test4</span></span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">if</span> <span class="constant constant_numeric constant_numeric_ruby">3</span> <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">></span> <span class="constant constant_numeric constant_numeric_ruby">2</span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">end</span><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> <span class="keyword keyword_control keyword_control_ruby">end</span><br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span><br /><br /><span class="keyword keyword_operator keyword_operator_other keyword_operator_other_ruby">>></span> test4<br /><span class="punctuation punctuation_separator punctuation_separator_key-value">=></span> <span class="constant constant_language constant_language_ruby">nil</span></span></pre>Yep, ruby doesn't care about conditional statement controlling expressions with respect to return values.<br /><br />Happy ruby'ing!Spovichhttp://www.blogger.com/profile/04598664184344571923noreply@blogger.com3tag:blogger.com,1999:blog-8726804997833047685.post-16683567071984207822008-01-13T15:47:00.001-08:002008-01-18T21:42:57.077-08:00How to downgrade part of a yum repository<span style="font-weight: bold;">Backstory</span><br />(<a href="http://blog.spovich.com/2008/01/how-to-safely-downgrade-part-of-yum.html#useful">skip down to avoid the rambling...</a>)<br /><br />I've been running Zimbra 4.0.x/4.5.x on CentOS 4.x for over a year now on a couple different servers without a hiccup, although Zimbra will tell you that CentOS is NOT a supported platform. Of course, RHEL4/5 are officially supported. I dutifully ran each Zimbra upgrade (with full backup prior) and never had a problem, but for Zimbra 5.0, I decided to be cautious. Zimbra 5.0 was released on 12/31/2007 (really trying to be able to say it was released in Q4 '07). Clearly, Zimbra was in a bit of a hurry to get it out the door, and as it was a rather substantial upgrade, I decided to wait until at least 5.0.1.<br /><br />Sure enough, shortly after the 5.0 release, the Zimbra forums started popping with upgrade problems, which was finally tracked down to a bad interaction with Zimbra and the version of perl (5.8.8) that comes with Red Hat Application Server and CentOS Plus. The stock version of perl is 5.8.5. After checking my Zimbra servers, sure enough, I had the CentOS Plus version of perl installed (not sure why I had done that, but the machines were installed a couple of years ago...)<br /><br />Several people on the forums were using the following to force install, but that didn't pass the sniff test and would break the first time perl RPM was updated:<br /><pre>$ perl -MCPAN -e shell<br />force install Scalar::Util</pre><br /><a name="useful"></a><br /><span style="font-weight: bold;">Useful part of story (--nodeps is your friend)</span><br /><br />My first pass at this, I thought I'd just remove the perl rpm, fixup the CentOS Plus repository to ignore perl and install the correct version of perl for Zimbra. So, I wanted to see what kind of dependency hell I'd be in if I didn't force anything:<br /><pre>$ perl -v<br />This is perl, v5.8.8 built for i386-linux-thread-multi<br />$ sudo yum remove perl<br />...<br />Transaction Summary<br />======================================<br />Install 0 Package(s)<br />Update 0 Package(s)<br />Remove 223 Package(s)</pre>Hmm, plan B? Using 'rpm -e --force' is the quickest way to trash your RPM database, so I wasn't doing that. Fortunately, a more experienced user on the forums suggested just using rpm to uninstall perl with the --nodeps flag.<br /><pre>$ sudo rpm -e --nodeps perl</pre>Nice, that got rid of perl-5.8.8 (also uninstall perl-suid if installed).<br /><br />Then edit the CentOS Plus repository config to permanently ignore it's version of perl.<br /><pre>$ sudo vi /etc/yum.repos.d/CentOS-Base.repo<br />(insert 'exclude=perl*' under the [centosplus] section)</pre>Now reinstall perl, and you are good to go.<br /><pre>$ sudo yum install perl<br />$ perl -v<br />This is perl, v5.8.5 built for i386-linux-thread-multi<br /></pre><span style="font-weight: bold;"><br />Cleanup</span><br /><br />Check for /usr/lib/perl5/5.8.8/ directory. It shouldn't exist, but if it does, you probably installed code from CPAN and need to reinstall the appropriate 5.8.5 code.Spovichhttp://www.blogger.com/profile/04598664184344571923noreply@blogger.com4