<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.4" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>We Heart Code</title>
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<pubDate>Tue, 08 Jul 2008 12:35:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>
	<language>en</language>
			<item>
		<title>Easily Add PropertyChangeSupport to Beans in Eclipse</title>
		<link>http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/</link>
		<comments>http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 18:58:53 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>eclipse</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/</guid>
		<description><![CDATA[If you plan on using JFace Databinding in SWT you will have to implement Property Change Support on all your Java Beans, (barf). I wasn't able to find a plugin that would do this in one click to  a class so I came up with two ways to do it.
Here's a code template you [...]]]></description>
			<content:encoded><![CDATA[<p>If you plan on using JFace Databinding in SWT you will have to implement Property Change Support on all your Java Beans, (barf). I wasn't able to find a plugin that would do this in one click to  a class so I came up with two ways to do it.</p>
<p>Here's a code template you can use based on the cool trick I learned at <a href="http://stuffthathappens.com/blog/2007/12/15/javabeans-propertychangesupport-trick/">Stuff That Happens</a> </p>
<p><code>firePropertyChange("${enclosing_method_arguments}", this.${enclosing_method_arguments}, ${line_selection});</code></p>
<p>Just select the assignment part of your setter and use this template.</p>
<p>If you're really lazy you can use a regex find and replace (With the File Search Dialog) </p>
<p>Your Find string will be:<br />
<code>(this.+) = (\w+);</code></p>
<p>And your replace string will be:<br />
<code>firePropertyChange("$2", $1, $1 = $2);</code></p>
<p>Both of these solutions assume you use an Abstract BaseClass to set up your firePropertyChange stuff, then extend for each bean.</p>
<p>Mine looks like this:</p>
<div class="syntax_hilite">
<div id="java-2">
<div class="java">package com.<span style="color: #006600;">weheartcode</span>;</p>
<p><span style="color: #a1a100;">import java.beans.PropertyChangeListener;</span><br />
<span style="color: #a1a100;">import java.beans.PropertyChangeSupport;</span></p>
<p><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> BaseEntity <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">transient</span> <a href="http://www.google.com/search?q=allinurl%3APropertyChangeSupport+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeSupport</span></a> propertyChangeSupport = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=allinurl%3APropertyChangeSupport+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeSupport</span></a><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Adds the property change listener.<br />
&nbsp; &nbsp;&nbsp; *<br />
&nbsp; &nbsp;&nbsp; * @param listener the listener<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> addPropertyChangeListener<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3APropertyChangeListener+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeListener</span></a> listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.<span style="color: #006600;">addPropertyChangeListener</span><span style="color: #66cc66;">&#40;</span>listener<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Adds the property change listener.<br />
&nbsp; &nbsp;&nbsp; *<br />
&nbsp; &nbsp;&nbsp; * @param propertyName the property name<br />
&nbsp; &nbsp;&nbsp; * @param listener the listener<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> addPropertyChangeListener<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> propertyName,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3APropertyChangeListener+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeListener</span></a> listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.<span style="color: #006600;">addPropertyChangeListener</span><span style="color: #66cc66;">&#40;</span>propertyName, listener<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Removes the property change listener.<br />
&nbsp; &nbsp;&nbsp; *<br />
&nbsp; &nbsp;&nbsp; * @param listener the listener<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> removePropertyChangeListener<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3APropertyChangeListener+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeListener</span></a> listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.<span style="color: #006600;">removePropertyChangeListener</span><span style="color: #66cc66;">&#40;</span>listener<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Removes the property change listener.<br />
&nbsp; &nbsp;&nbsp; *<br />
&nbsp; &nbsp;&nbsp; * @param propertyName the property name<br />
&nbsp; &nbsp;&nbsp; * @param listener the listener<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> removePropertyChangeListener<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> propertyName,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3APropertyChangeListener+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeListener</span></a> listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.<span style="color: #006600;">removePropertyChangeListener</span><span style="color: #66cc66;">&#40;</span>propertyName,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp;&nbsp; * Fire property change.<br />
&nbsp; &nbsp;&nbsp; *<br />
&nbsp; &nbsp;&nbsp; * @param propertyName the property name<br />
&nbsp; &nbsp;&nbsp; * @param oldValue the old value<br />
&nbsp; &nbsp;&nbsp; * @param newValue the new value<br />
&nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> firePropertyChange<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">String</span></a> propertyName, <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> oldValue,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> newValue<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; propertyChangeSupport.<span style="color: #006600;">firePropertyChange</span><span style="color: #66cc66;">&#40;</span>propertyName, oldValue,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newValue<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p>
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/&amp;title=Easily+Add+PropertyChangeSupport+to+Beans+in+Eclipse" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Del.icio.us" alt="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/&amp;title=Easily+Add+PropertyChangeSupport+to+Beans+in+Eclipse" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to digg" alt="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Easily+Add+PropertyChangeSupport+to+Beans+in+Eclipse&amp;u=http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to FURL" alt="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/&amp;title=Easily+Add+PropertyChangeSupport+to+Beans+in+Eclipse" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to reddit" alt="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Technorati" alt="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/&amp;title=Easily+Add+PropertyChangeSupport+to+Beans+in+Eclipse" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Google Bookmarks" alt="Add 'Easily Add PropertyChangeSupport to Beans in Eclipse' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2008/04/29/easily-add-propertychangesupport-to-beans-in-eclipse/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Automatically Add JavaDoc Comments in Eclipse</title>
		<link>http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/</link>
		<comments>http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 13:05:44 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>eclipse</category>
	<category>java</category>
	<category>tools</category>
	<category>code generation</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/</guid>
		<description><![CDATA[Recently while preparing to contribute some code to Eclipse I had to add a copyright comment to the top of every class. Enter JAutodoc it will go through and add Javadoc comments and other automated comments, the update site is:
http://jautodoc.sourceforge.net/update/
Screenshot:


Bookmark to:
]]></description>
			<content:encoded><![CDATA[<p>Recently while preparing to contribute some code to <a href="http://www.eclipse.org">Eclipse</a> I had to add a copyright comment to the top of every class. Enter <a href="http://jautodoc.sourceforge.net/">JAutodoc</a> it will go through and add Javadoc comments and other automated comments, the update site is:<br />
http://jautodoc.sourceforge.net/update/</p>
<p>Screenshot:<br />
<img src="http://jautodoc.sourceforge.net/images/preferences.gif" alt="JAutodoc Preferences" />
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Del.icio.us" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to digg" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Automatically+Add+JavaDoc+Comments+in+Eclipse&amp;u=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to FURL" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to reddit" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Technorati" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/&amp;title=Automatically+Add+JavaDoc+Comments+in+Eclipse" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Automatically Add JavaDoc Comments in Eclipse' to Google Bookmarks" alt="Add 'Automatically Add JavaDoc Comments in Eclipse' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2008/04/11/automatically-add-javadoc-comments-in-eclipse/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Limiting log4j SMTPAppender</title>
		<link>http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/</link>
		<comments>http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 14:43:21 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>tip</category>
	<category>java</category>
	<category>logging</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/</guid>
		<description><![CDATA[I wanted to set a maximum amount of e-mails that SMTPAppender would send after filling up a mailbox with 30,000 damn e-mails.
Turns out it was pretty damn easy, I set up my appender in a java class, and the SMTP appender has a method called setEvaluator that takes an instance of TriggeringEventEvaluator, so I just [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to set a maximum amount of e-mails that SMTPAppender would send after filling up a mailbox with 30,000 damn e-mails.</p>
<p>Turns out it was pretty damn easy, I set up my appender in a java class, and the SMTP appender has a method called setEvaluator that takes an instance of TriggeringEventEvaluator, so I just created a quick inner class like so.</p>
<div class="syntax_hilite">
<div id="java-5">
<div class="java"><span style="color: #000000; font-weight: bold;">class</span> LimitingEvaluator <span style="color: #000000; font-weight: bold;">implements</span> TriggeringEventEvaluator<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> sentEvents = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> MAX_EVENTS = <span style="color: #cc66cc;">20</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> isTriggeringEvent<span style="color: #66cc66;">&#40;</span>LoggingEvent arg0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sentEvents++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>sentEvents&gt;= MAX_EVENTS<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</div>
</div>
<p></p>
<p>and when i set up my appender I do this</p>
<div class="syntax_hilite">
<div id="java-6">
<div class="java">appender.<span style="color: #006600;">setEvaluator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LimitingEvaluator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</div>
</div>
<p>
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/&amp;title=Limiting+log4j+SMTPAppender" title="Add 'Limiting log4j SMTPAppender' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Limiting log4j SMTPAppender' to Del.icio.us" alt="Add 'Limiting log4j SMTPAppender' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/&amp;title=Limiting+log4j+SMTPAppender" title="Add 'Limiting log4j SMTPAppender' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Limiting log4j SMTPAppender' to digg" alt="Add 'Limiting log4j SMTPAppender' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Limiting+log4j+SMTPAppender&amp;u=http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/" title="Add 'Limiting log4j SMTPAppender' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Limiting log4j SMTPAppender' to FURL" alt="Add 'Limiting log4j SMTPAppender' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/&amp;title=Limiting+log4j+SMTPAppender" title="Add 'Limiting log4j SMTPAppender' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Limiting log4j SMTPAppender' to reddit" alt="Add 'Limiting log4j SMTPAppender' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/" title="Add 'Limiting log4j SMTPAppender' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Limiting log4j SMTPAppender' to Technorati" alt="Add 'Limiting log4j SMTPAppender' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/&amp;title=Limiting+log4j+SMTPAppender" title="Add 'Limiting log4j SMTPAppender' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Limiting log4j SMTPAppender' to Google Bookmarks" alt="Add 'Limiting log4j SMTPAppender' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2008/02/15/limiting-log4j-smtpappender/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Mass E-mailing, worth paying for a service</title>
		<link>http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/</link>
		<comments>http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 14:20:48 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>email</category>
	<category>review</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/</guid>
		<description><![CDATA[Most of us have written little mass e-mailing scripts in the past. No big deal, connect to SNMP, loop through our subscriber list, do our thang. But then the requirements come in: "
"Hey, I need to know what person is clicking on that link, and what time they opened it."
"How does it look in a [...]]]></description>
			<content:encoded><![CDATA[<p>Most of us have written little mass e-mailing scripts in the past. No big deal, connect to SNMP, loop through our subscriber list, do our thang. But then the requirements come in: "</p>
<p>"Hey, I need to know what person is clicking on that link, and what time they opened it."</p>
<p>"How does it look in a hotmail account?"</p>
<p>"Does it pass every spam filter?"</p>
<p>Sure, we could code a little link pass through tool, encoded with the person's e-mail address we sent it to, we could create test accounts at all the major e-mail services, we can make all sorts of intricate unsubscribe reports. But why would you want to do that? That's Amateur Hour! You've got cool shit to build. The solution? <a href="http://www.campaignmonitor.com/">CampaignMonitor.com</a>. They charge $5 base fee for every "Campaign" you send, plus 1 cent per recipient. So to send to 45,000 people that costs you $50. Big woop.</p>
<p>Plus you get all sorts of <a href="http://www.campaignmonitor.com/features/powerful-elegant-reporting.aspx">intricate reports </a></p>
<p><img src="http://www.campaignmonitor.com/images/screenshots/reports-opens.gif" alt="Campaign Monitor Reports" /></p>
<p>They allow you to send full HTML with no size limit, you're not limited to their templates. You also get a nice list manager with unsubscribe and subscribe features built right in.</p>
<p>For a $10 fee you can preview your design in like 10 different e-mail clients &#038; services as well as test that your e-mail passes spam filter (hint: don't put the word viagra in your e-mail).</p>
<p>CampaignMonitor also has relationships with major ISP's so you know your e-mail will not be denied and your servers won't be blacklisted. Plus the office/marketing drones love the reports and the flexibilitiy to send an e-mail campaign themselves.</p>
<p>So don't waste time re-inventing the wheel dummy, pony up your whopper money and do it the easy way.</p>
<p>And no -- they didn't pay me anything for this post, though they can if they want!</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/&amp;title=Mass+E-mailing%2C+worth+paying+for+a+service" title="Add 'Mass E-mailing, worth paying for a service' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Mass E-mailing, worth paying for a service' to Del.icio.us" alt="Add 'Mass E-mailing, worth paying for a service' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/&amp;title=Mass+E-mailing%2C+worth+paying+for+a+service" title="Add 'Mass E-mailing, worth paying for a service' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Mass E-mailing, worth paying for a service' to digg" alt="Add 'Mass E-mailing, worth paying for a service' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Mass+E-mailing%2C+worth+paying+for+a+service&amp;u=http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/" title="Add 'Mass E-mailing, worth paying for a service' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Mass E-mailing, worth paying for a service' to FURL" alt="Add 'Mass E-mailing, worth paying for a service' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/&amp;title=Mass+E-mailing%2C+worth+paying+for+a+service" title="Add 'Mass E-mailing, worth paying for a service' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Mass E-mailing, worth paying for a service' to reddit" alt="Add 'Mass E-mailing, worth paying for a service' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/" title="Add 'Mass E-mailing, worth paying for a service' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Mass E-mailing, worth paying for a service' to Technorati" alt="Add 'Mass E-mailing, worth paying for a service' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/&amp;title=Mass+E-mailing%2C+worth+paying+for+a+service" title="Add 'Mass E-mailing, worth paying for a service' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Mass E-mailing, worth paying for a service' to Google Bookmarks" alt="Add 'Mass E-mailing, worth paying for a service' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2008/01/17/mass-e-mailing-worth-paying-for-a-service/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Postgres select replace with regular expression</title>
		<link>http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/</link>
		<comments>http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 21:22:50 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>Uncategorized</category>
	<category>sql</category>
	<category>postgres</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/</guid>
		<description><![CDATA[Postgres seriously kicks ass, here's a quick way to do a substring based on a regular expression. A Co-worker wanted to get the first word out of a column that contained a sentence. If we were using something lame like oracle we'd have to do gymnastics to do that.
It's a one-liner in postgres.


SELECT substring&#40;'this is [...]]]></description>
			<content:encoded><![CDATA[<p>Postgres seriously kicks ass, here's a quick way to do a substring based on a regular expression. A Co-worker wanted to get the first word out of a column that contained a sentence. If we were using something lame like oracle we'd have to do gymnastics to do that.</p>
<p>It's a one-liner in postgres.</p>
<div class="syntax_hilite">
<div id="sql-8">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> substring<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'this is a sentence'</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'^(.+?)<span style="color: #000099; font-weight: bold;">\\</span>s.+$'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p></p>
<p>So we're selecting out the group that is a series of characters at the start of a line (column) before a space. Epic win.</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Postgres select replace with regular expression' to Del.icio.us" alt="Add 'Postgres select replace with regular expression' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Postgres select replace with regular expression' to digg" alt="Add 'Postgres select replace with regular expression' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Postgres+select+replace+with+regular+expression&amp;u=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/" title="Add 'Postgres select replace with regular expression' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Postgres select replace with regular expression' to FURL" alt="Add 'Postgres select replace with regular expression' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Postgres select replace with regular expression' to reddit" alt="Add 'Postgres select replace with regular expression' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/" title="Add 'Postgres select replace with regular expression' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Postgres select replace with regular expression' to Technorati" alt="Add 'Postgres select replace with regular expression' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Postgres select replace with regular expression' to Google Bookmarks" alt="Add 'Postgres select replace with regular expression' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Upgrade to Gutsy Gibbon Fast</title>
		<link>http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/</link>
		<comments>http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 03:29:34 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
		
	<category>Uncategorized</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/</guid>
		<description><![CDATA[Everyone is excited that Ubuntu 7.10 is finally out.  (Have a look at the new feature tour here.)
But the servers are getting hammered, so most people are waiting on the upgrade (voluntarily or otherwise).
No need to wait!  The more people who download from Bittorent, the faster your speeds!  Grab it while it's [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone is excited that Ubuntu 7.10 is finally out.  (<span style="font-size: 100%">Have a look at the new feature tour <a href="http://www.ubuntu.com/getubuntu/releasenotes/710tour">here</a>.)</span></p>
<p>But the servers are getting hammered, so most people are waiting on the upgrade (voluntarily or otherwise).</p>
<p><span style="font-size: 100%">No need to wait!  The more people who download from </span><span style="font-size: 100%">Bittorent, the faster your speeds!  Grab it while it's hot!<br />
</span><span style="font-size: 100%">Download a copy of the alternative i386 CD <a href="http://releases.ubuntu.com/7.10/ubuntu-7.10-alternate-i386.iso.torrent">here</a> via Bittorent.<br />
Mac and 64 bit torrents can be obtained <a href="http://releases.ubuntu.com/7.10/">here.</a></span></p>
<p>Once you have the alternative CD, burn it, boot from it, select the upgrade option, and away you go.</p>
<p>Want to be environmentally friendly and save a CD?  Try this:<span style="font-size: 100%"><br />
</span></p>
<ol type="a">
<li><span style="font-size: 100%">Mount the ISO to /cdrom:<br />
sudo mount -t iso9660 ubuntu-7.10-alternate-i386.iso /cdrom -o loop<br />
</span><span style="font-size: 100%"><br />
Using the full path and proper name for the ISO image.</span></li>
<li>Open a terminal and type:<br />
sudo /cdrom/cdromupgrade</p>
<p>Then the upgrade utility should launch and run from the CD.</li>
</ol>
<p><span style="font-size: 100%">Make sure you have the alternative CD and NOT the standard desktop CD.</span></p>
<p>Good luck and happy upgrading!
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/&amp;title=Upgrade+to+Gutsy+Gibbon+Fast" title="Add 'Upgrade to Gutsy Gibbon Fast' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Upgrade to Gutsy Gibbon Fast' to Del.icio.us" alt="Add 'Upgrade to Gutsy Gibbon Fast' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/&amp;title=Upgrade+to+Gutsy+Gibbon+Fast" title="Add 'Upgrade to Gutsy Gibbon Fast' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Upgrade to Gutsy Gibbon Fast' to digg" alt="Add 'Upgrade to Gutsy Gibbon Fast' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Upgrade+to+Gutsy+Gibbon+Fast&amp;u=http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/" title="Add 'Upgrade to Gutsy Gibbon Fast' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Upgrade to Gutsy Gibbon Fast' to FURL" alt="Add 'Upgrade to Gutsy Gibbon Fast' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/&amp;title=Upgrade+to+Gutsy+Gibbon+Fast" title="Add 'Upgrade to Gutsy Gibbon Fast' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Upgrade to Gutsy Gibbon Fast' to reddit" alt="Add 'Upgrade to Gutsy Gibbon Fast' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/" title="Add 'Upgrade to Gutsy Gibbon Fast' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Upgrade to Gutsy Gibbon Fast' to Technorati" alt="Add 'Upgrade to Gutsy Gibbon Fast' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/&amp;title=Upgrade+to+Gutsy+Gibbon+Fast" title="Add 'Upgrade to Gutsy Gibbon Fast' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Upgrade to Gutsy Gibbon Fast' to Google Bookmarks" alt="Add 'Upgrade to Gutsy Gibbon Fast' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2007/10/18/upgrade-to-gutsy-gibbon-fast/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Reading An Excel File With Ruby</title>
		<link>http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/</link>
		<comments>http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 13:31:35 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>ruby</category>
	<category>tutorial</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/</guid>
		<description><![CDATA[This tutorial will cover how to read (or parse) an excel file with ruby. I had to write a script to unpivot some data for a co-worker that saved him hours of time, and I got to write a ruby script, so it was a win-win. Here's how you can do the same thing.

Installing Parseexcel
Parseexcel [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will cover how to read (or parse) an excel file with ruby. I had to write a script to unpivot some data for a co-worker that saved him hours of time, and I got to write a ruby script, so it was a win-win. Here's how you can do the same thing.</p>
<p><a id="more-35"></a></p>
<h2>Installing Parseexcel</h2>
<p><a href="http://raa.ruby-lang.org/project/parseexcel/">Parseexcel</a> is a ruby port of the perl parseexcel module.</p>
<p>It's installable via a nice gem like so:</p>
<div class="syntax_hilite">
<div id="code-16">
<div class="code">gem install parseexcel</div>
</div>
</div>
<p></p>
<p>That's that, now remember since it's a gem library we have to tell our script to use the gem libs when we run the script from the console using the -rubygems switch.</p>
<h2>Using Parseexcel</h2>
<p>Parseexcel is a very straight forward library, you can't do too much with it, but it gets the job done.</p>
<h3>Getting a Workbook</h3>
<div class="syntax_hilite">
<div id="code-17">
<div class="code">Spreadsheet::<span style="">ParseExcel</span>.<span style="">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>filenameandpath<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p>
This returns the actual excel file's workbook, from there we need to determine what worksheet we're on.</p>
<h3>Getting a Worksheet</h3>
<div class="syntax_hilite">
<div id="code-18">
<div class="code">worksheet = workbook.<span style="">worksheet</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p>
Will return the first worksheet, you could also use the each method on the workbook to iterate over all the worksheets.</p>
<h3>Iterating over rows and columns</h3>
<p>The worksheet object has a very nice each method that will allow us to iterate over the rows like so</p>
<div class="syntax_hilite">
<div id="ruby-19">
<div class="ruby">worksheet.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |row|<br />
&nbsp; j=<span style="color:#006666;">0</span><br />
&nbsp; i=<span style="color:#006666;">0</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> row != <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; row.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |cell|<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> cell != <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; &nbsp; contents = cell.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#40;</span>'latin1'<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">"Row: #{j} Cell: #{i} #{contents}"</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; i = i+<span style="color:#006666;">1</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; j = j +<span style="color:#006666;">1</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p></p>
<h3>Getting Cell Data</h3>
<ul>
<li><b>Getting a String</b>: cell.to_s('latin1')</li>
<li><b>Getting a Float</b>: cell.to_s('latin1')</li>
<li><b>Getting a Int</b>: cell.to_i</li>
<li><b>Getting a Date</b>: cell.date</li>
</ul>
<h3>Getting A Specific Cell</h3>
<div class="syntax_hilite">
<div id="ruby-20">
<div class="ruby">cell = row.<span style="color:#9900CC;">at</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#returns cell at column 3 </span></div>
</div>
</div>
<p></p>
<h3>A basic script for dumping an excel file</h3>
<div class="syntax_hilite">
<div id="ruby-21">
<div class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> 'parseexcel'</p>
<p><span style="color:#008000; font-style:italic;">#Open the excel file passed in from the commandline</span><br />
workbook = Spreadsheet::ParseExcel.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p><span style="color:#008000; font-style:italic;">#Get the first worksheet</span><br />
worksheet = workbook.<span style="color:#9900CC;">worksheet</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p><span style="color:#008000; font-style:italic;">#cycle over every row</span><br />
worksheet.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |row|<br />
&nbsp; j=<span style="color:#006666;">0</span><br />
&nbsp; i=<span style="color:#006666;">0</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> row != <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; <span style="color:#008000; font-style:italic;">#cycle over each cell in this row if it's not an empty row</span><br />
&nbsp; row.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |cell|<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">if</span> cell != <span style="color:#0000FF; font-weight:bold;">nil</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;">#Get the contents of the cell as a string</span><br />
&nbsp; &nbsp; &nbsp; contents = cell.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#40;</span>'latin1'<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">"Row: #{j} Cell: #{i}&gt; #{contents}"</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; i = i+<span style="color:#006666;">1</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p></p>
<p>To run the script, remember to use the -rubygems switch so that you can find the parsexcel library.</p>
<div class="syntax_hilite">
<div id="code-22">
<div class="code">ruby -rubygems excelparse.<span style="">rb</span> myfile.<span style="">xls</span></div>
</div>
</div>
<p>
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/&amp;title=Reading+An+Excel+File+With+Ruby" title="Add 'Reading An Excel File With Ruby' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Reading An Excel File With Ruby' to Del.icio.us" alt="Add 'Reading An Excel File With Ruby' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/&amp;title=Reading+An+Excel+File+With+Ruby" title="Add 'Reading An Excel File With Ruby' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Reading An Excel File With Ruby' to digg" alt="Add 'Reading An Excel File With Ruby' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Reading+An+Excel+File+With+Ruby&amp;u=http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/" title="Add 'Reading An Excel File With Ruby' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Reading An Excel File With Ruby' to FURL" alt="Add 'Reading An Excel File With Ruby' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/&amp;title=Reading+An+Excel+File+With+Ruby" title="Add 'Reading An Excel File With Ruby' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Reading An Excel File With Ruby' to reddit" alt="Add 'Reading An Excel File With Ruby' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/" title="Add 'Reading An Excel File With Ruby' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Reading An Excel File With Ruby' to Technorati" alt="Add 'Reading An Excel File With Ruby' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/&amp;title=Reading+An+Excel+File+With+Ruby" title="Add 'Reading An Excel File With Ruby' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Reading An Excel File With Ruby' to Google Bookmarks" alt="Add 'Reading An Excel File With Ruby' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2007/10/05/reading-an-excel-file-with-ruby/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Easy XML Generation with Ruby and ERB</title>
		<link>http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/</link>
		<comments>http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 15:42:25 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>ruby</category>
	<category>tutorial</category>
	<category>code generation</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/</guid>
		<description><![CDATA[Ah, the bracket tax. XML-Situps, whatever you want to call them, if you write any code (especially if you use java) it should be your sworn enemy. The other day I had to create about 40 XML files for some Java Webstart configs. Instead of doing them by hand, I decided to check out the [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, the bracket tax. XML-Situps, whatever you want to call them, if you write any code (especially if you use java) it should be your sworn enemy. The other day I had to create about 40 XML files for some Java Webstart configs. Instead of doing them by hand, I decided to check out the ERB class for easy "templating" that's built into Ruby. It makes doing code generation very easy!</p>
<h3>Our Task</h3>
<p>Create a custom XML file for each .jar file in a directory.</p>
<p>...more after the jump!<br />
<a id="more-31"></a></p>
<h3>Strategy</h3>
<p>With any code generation project we need to identify a few things.</p>
<ol>
<li>Static template: The code/text that will stay the same.</li>
<li>Dynamic variables: These are the pieces that will change in our template.</li>
<li>Input source: The input for our dynamic variables.</li>
</ol>
<p>For me, the static template is a JNLP XML file that looks like this:</p>
<div class="syntax_hilite">
<div id="xml-30">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jnlp</span><br />
&nbsp; <span style="color: #000066;">spec</span>=<span style="color: #ff0000;">"1.0+"</span><br />
&nbsp; <span style="color: #000066;">codebase</span>=<span style="color: #ff0000;">"http://redacted.com/libs/com.redacted.dao_1.3"</span><br />
&nbsp; <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"RcpWebStart.jnlp"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;information<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;title<span style="font-weight: bold; color: black;">&gt;</span></span></span>RedactedLibs<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/title<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;vendor<span style="font-weight: bold; color: black;">&gt;</span></span></span>Redacted<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/vendor<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;strong<span style="font-weight: bold; color: black;">&gt;</span></span></span>com.redacted.dao_1.3.1.jar<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/strong<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/information<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;security<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;all</span>-permissions<span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/security<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;resources<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;j2se</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.4+"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jar</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">"com.redacted.dao_1.3.1.jar"</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/resources<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;component</span>-desc<span style="font-weight: bold; color: black;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/jnlp<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>The dynamic bits will be anywhere a jar is referenced as well as the directory for the jar.</p>
<h3>The Template</h3>
<p>The template defines our static text, and the variables and/or ruby code that will be substituted into the text by ERB.</p>
<p>We use<br />
<div class="syntax_hilite">
<div id="code-31">
<div class="code">&lt;%= %&gt;</div>
</div>
</div>
<p> blocks to substitute in variables from our ruby script.</p>
<p>Here's what my template looks like, I have four variables that I want to substitute in.</p>
<ul>
<li><strong>basedir</strong>: the base directory of where I'm storing my jar.</li>
<li><strong>shortname</strong>: the name of the jar sans the version.</li>
<li><strong>jarname</strong>: the full name of the jar file.</li>
</ul>
<div class="syntax_hilite">
<div id="xml-32">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">"1.0"</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">"UTF-8"</span>?<span style="font-weight: bold; color: black;">&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;jnlp</span><br />
&nbsp; <span style="color: #000066;">spec</span>=<span style="color: #ff0000;">"1.0+"</span><br />
&nbsp; <span style="color: #000066;">codebase</span>=<span style="color: #ff0000;">"http://redacted.net/libs/&lt;%=basedir%&gt;</span>/<span style="color: #009900;">&lt;%=shortname%&gt;</span>_<span style="color: #009900;">&lt;%=version%&gt;</span>&quot;<br />
&nbsp; href=&quot;RcpWebStart.jnlp&quot;&gt;<br />
&nbsp; <span style="color: #009900;">&lt;information&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;title&gt;</span><span style="color: #009900;">&lt;%=basedir%&gt;</span><span style="color: #009900;">&lt;/title&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;vendor&gt;</span>Redacted<span style="color: #009900;">&lt;/vendor&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;description&gt;</span><span style="color: #009900;">&lt;%=jarname%&gt;</span><span style="color: #009900;">&lt;/description&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;/information&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;security&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;all-permissions/&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;/security&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;resources&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;j2se version="</span><span style="color: #cc66cc;">1</span>.<span style="color: #cc66cc;">4</span>+<span style="color: #ff0000;">"/&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;jar href="</span><span style="font-weight: bold; color: black;">&lt;</span>%=jarname%<span style="font-weight: bold; color: black;">&gt;</span></span>&quot;/&gt;<br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/resources<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;component</span>-desc<span style="font-weight: bold; color: black;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/jnlp<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<h3>The Code</h3>
<p>Let's examine some code. The first thing I do is create the template string, i Use the %q{ shortcut to avoid having to worry about quotes.</p>
<p>Next, I need to define my template, that's what this line does.</p>
<div class="syntax_hilite">
<div id="ruby-33">
<div class="ruby">jnlp = ERB.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>template, <span style="color:#006666;">0</span>, <span style="color:#996600;">"%&lt;&gt;"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</div>
</div>
<p></p>
<p>It says, create a new ERB object using the string 'template' as the template and the "%&lt;&gt;" as my template escape operators.</p>
<p>Our next step is to loop through all the jar's in a directory and create a new file for each one, here's the juicy bits to accomplish that.</p>
<div class="syntax_hilite">
<div id="ruby-34">
<div class="ruby">b = <span style="color:#CC0066; font-weight:bold;">binding</span><br />
&nbsp; &nbsp; &nbsp;jarname = File.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;basedir = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;shortname = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">1</span>'<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;version = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d\.\d<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">2</span>'<span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp;dirname = shortname + <span style="color:#996600;">"_"</span> + version<br />
&nbsp; &nbsp; &nbsp;ourfile = jnlp.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p></p>
<p>Notice that we create a binding variable called b and then pass that to our ERB object (jnlp), that binding variable tells ERB to use template variables in the same scope as it. It's some ruby black magic that makes our lives easier.<br />
The rest of the script actually creates some directory and writes the created file to a directory based on the name of the jar.</p>
<div class="syntax_hilite">
<div id="ruby-35">
<div class="ruby"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">"erb"</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">"find"</span></p>
<p>template = %q<span style="color:#006600; font-weight:bold;">&#123;</span><br />
&lt;?xml version=<span style="color:#996600;">"1.0"</span> encoding=<span style="color:#996600;">"UTF-8"</span>?&gt;<br />
&lt;jnlp<br />
&nbsp; spec=<span style="color:#996600;">"1.0+"</span><br />
&nbsp; codebase=<span style="color:#996600;">"http://redacted.net/libs/&lt;%=basedir%&gt;/&lt;%=shortname%&gt;_&lt;%=version%&gt;"</span><br />
&nbsp; href=<span style="color:#996600;">"RcpWebStart.jnlp"</span>&gt;<br />
&nbsp; &lt;information&gt;<br />
&nbsp; &nbsp; &lt;title&gt;&lt;%=basedir%&gt;&lt;/title&gt;<br />
&nbsp; &nbsp; &lt;vendor&gt;Redactedl&lt;/vendor&gt;<br />
&nbsp; &nbsp; &lt;description&gt;&lt;%=jarname%&gt;&lt;/description&gt;<br />
&nbsp; &lt;/information&gt;<br />
&nbsp; &lt;security&gt;<br />
&nbsp; &nbsp; &lt;all-permissions/&gt;<br />
&nbsp; &lt;/security&gt;<br />
&nbsp; &lt;resources&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;j2se version=<span style="color:#996600;">"1.4+"</span>/&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;jar href=<span style="color:#996600;">"&lt;%=jarname%&gt;"</span>/&gt;<br />
&nbsp; &lt;/resources&gt;<br />
&nbsp; &lt;component-desc/&gt;<br />
&lt;/jnlp&gt;<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; jnlp = ERB.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>template, <span style="color:#006666;">0</span>, <span style="color:#996600;">"%&lt;&gt;"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</p>
<p>&nbsp; <span style="color:#008000; font-style:italic;">#Search for jar files here</span><br />
&nbsp; Find.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |path|</p>
<p>&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">if</span> !FileTest.<span style="color:#9900CC;">directory</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;b = <span style="color:#CC0066; font-weight:bold;">binding</span><br />
&nbsp; &nbsp; &nbsp;jarname = File.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;basedir = ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;shortname = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">1</span>'<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;version = jarname.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/<span style="color:#006600; font-weight:bold;">&#40;</span>.+?<span style="color:#006600; font-weight:bold;">&#41;</span>_<span style="color:#006600; font-weight:bold;">&#40;</span>\d\.\d<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#40;</span>.+<span style="color:#006600; font-weight:bold;">&#41;</span>$/,'\<span style="color:#006666;">2</span>'<span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp;dirname = shortname + <span style="color:#996600;">"_"</span> + version</p>
<p>&nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#pass in the binding variable and create a string from the template</span><br />
&nbsp; &nbsp; &nbsp;ourfile = jnlp.<span style="color:#9900CC;">result</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp;<span style="color:#008000; font-style:italic;">#create a new directory and file, and output the template string to it.</span><br />
&nbsp; &nbsp; &nbsp;Dir.<span style="color:#9900CC;">mkdir</span><span style="color:#006600; font-weight:bold;">&#40;</span>basedir + <span style="color:#996600;">"/"</span> + dirname<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;outfile = File.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>basedir + <span style="color:#996600;">"/"</span> + dirname +&nbsp; <span style="color:#996600;">"/RcpWebStart.jnlp"</span>,<span style="color:#996600;">"w+"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;outfile &lt;&lt;ourfile<br />
&nbsp; &nbsp;<span style="color:#9966CC; font-weight:bold;">end</span></div>
</div>
</div>
<p></p>
<h3>The fun doesn't stop there</h3>
<p>This is an elementary example, but ERB also allows you to embed real ruby code in the template, this allows you to accomplish looping. </p>
<p>So we could pass in an array of person objects and loop through them with a template like this:</p>
<div class="syntax_hilite">
<div id="xml-36">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;people<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%@people.each do |person| %<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%=person.name%<span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;</span>%end%<span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/people<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Easy XML Generation with Ruby and ERB' to Del.icio.us" alt="Add 'Easy XML Generation with Ruby and ERB' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Easy XML Generation with Ruby and ERB' to digg" alt="Add 'Easy XML Generation with Ruby and ERB' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Easy+XML+Generation+with+Ruby+and+ERB&amp;u=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/" title="Add 'Easy XML Generation with Ruby and ERB' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Easy XML Generation with Ruby and ERB' to FURL" alt="Add 'Easy XML Generation with Ruby and ERB' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Easy XML Generation with Ruby and ERB' to reddit" alt="Add 'Easy XML Generation with Ruby and ERB' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/" title="Add 'Easy XML Generation with Ruby and ERB' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Easy XML Generation with Ruby and ERB' to Technorati" alt="Add 'Easy XML Generation with Ruby and ERB' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/&amp;title=Easy+XML+Generation+with+Ruby+and+ERB" title="Add 'Easy XML Generation with Ruby and ERB' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Easy XML Generation with Ruby and ERB' to Google Bookmarks" alt="Add 'Easy XML Generation with Ruby and ERB' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2007/09/18/easy-xml-generation-with-ruby-and-erb/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Killing a process by name in ubuntu</title>
		<link>http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/</link>
		<comments>http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 20:52:23 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>tip</category>
	<category>linux</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/</guid>
		<description><![CDATA[I have to kill mongrel alot because it's a flea-bitten mutt.
My usual method was to do a ps -aux &#124; grep mongrel , find the pid and kill it!
Annoying and stupid!
Now I just use the nice pkill tool in ubuntu like so.
pkill mongrel
boom! All bad doggies killed! This probably works on other *nix flavors, but [...]]]></description>
			<content:encoded><![CDATA[<p>I have to kill <a href="http://mongrel.rubyforge.org/">mongrel</a> alot because it's a flea-bitten mutt.</p>
<p>My usual method was to do a <code>ps -aux | grep mongrel </code>, find the pid and kill it!</p>
<p>Annoying and stupid!</p>
<p>Now I just use the nice pkill tool in ubuntu like so.</p>
<p><code>pkill mongrel</code></p>
<p>boom! All bad doggies killed! This probably works on other *nix flavors, but who knows if your stupid admin installed it.
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/&amp;title=Killing+a+process+by+name+in+ubuntu" title="Add 'Killing a process by name in ubuntu' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Killing a process by name in ubuntu' to Del.icio.us" alt="Add 'Killing a process by name in ubuntu' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/&amp;title=Killing+a+process+by+name+in+ubuntu" title="Add 'Killing a process by name in ubuntu' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Killing a process by name in ubuntu' to digg" alt="Add 'Killing a process by name in ubuntu' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Killing+a+process+by+name+in+ubuntu&amp;u=http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/" title="Add 'Killing a process by name in ubuntu' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Killing a process by name in ubuntu' to FURL" alt="Add 'Killing a process by name in ubuntu' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/&amp;title=Killing+a+process+by+name+in+ubuntu" title="Add 'Killing a process by name in ubuntu' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Killing a process by name in ubuntu' to reddit" alt="Add 'Killing a process by name in ubuntu' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/" title="Add 'Killing a process by name in ubuntu' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Killing a process by name in ubuntu' to Technorati" alt="Add 'Killing a process by name in ubuntu' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/&amp;title=Killing+a+process+by+name+in+ubuntu" title="Add 'Killing a process by name in ubuntu' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Killing a process by name in ubuntu' to Google Bookmarks" alt="Add 'Killing a process by name in ubuntu' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2007/09/17/killing-a-process-by-name-in-ubuntu/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Capturing STDERR with ruby backticks</title>
		<link>http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/</link>
		<comments>http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/#comments</comments>
		<pubDate>Fri, 14 Sep 2007 14:13:25 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
		
	<category>tip</category>
	<category>ruby</category>
		<guid isPermaLink="false">http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/</guid>
		<description><![CDATA[I was writing a ruby script today that ran a linux command for every line in a file. The problem was the backtick operator in ruby only captures stdout, here's a little trick to get stderr as well. (*nix only)


out = `ourlinuxcmd 2&#62;&#38;1`



the 2>&#038;1 bit tells the shell to redirect stderr to stdout

Bookmark to:
]]></description>
			<content:encoded><![CDATA[<p>I was writing a ruby script today that ran a linux command for every line in a file. The problem was the backtick operator in ruby only captures stdout, here's a little trick to get <strong>stderr</strong> as well. (<em>*nix only</em>)</p>
<div class="syntax_hilite">
<div id="ruby-38">
<div class="ruby">out = <span style="color:#996600;">`ourlinuxcmd 2&gt;&amp;1`</span></div>
</div>
</div>
<p></p>
<p>the 2>&#038;1 bit tells the shell to redirect <strong>stderr</strong> to <strong>stdout</strong>
</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/&amp;title=Capturing+STDERR+with+ruby+backticks" title="Add 'Capturing STDERR with ruby backticks' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Capturing STDERR with ruby backticks' to Del.icio.us" alt="Add 'Capturing STDERR with ruby backticks' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/&amp;title=Capturing+STDERR+with+ruby+backticks" title="Add 'Capturing STDERR with ruby backticks' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Capturing STDERR with ruby backticks' to digg" alt="Add 'Capturing STDERR with ruby backticks' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Capturing+STDERR+with+ruby+backticks&amp;u=http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/" title="Add 'Capturing STDERR with ruby backticks' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Capturing STDERR with ruby backticks' to FURL" alt="Add 'Capturing STDERR with ruby backticks' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/&amp;title=Capturing+STDERR+with+ruby+backticks" title="Add 'Capturing STDERR with ruby backticks' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Capturing STDERR with ruby backticks' to reddit" alt="Add 'Capturing STDERR with ruby backticks' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/" title="Add 'Capturing STDERR with ruby backticks' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Capturing STDERR with ruby backticks' to Technorati" alt="Add 'Capturing STDERR with ruby backticks' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/&amp;title=Capturing+STDERR+with+ruby+backticks" title="Add 'Capturing STDERR with ruby backticks' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Capturing STDERR with ruby backticks' to Google Bookmarks" alt="Add 'Capturing STDERR with ruby backticks' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRSS>http://www.weheartcode.com/2007/09/14/capturing-stderr-with-ruby-backticks/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
