<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yan Pritzker &#187; code</title>
	<atom:link href="http://yanpritzker.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://yanpritzker.com</link>
	<description>photographer, entrepreneur, software engineer, musician, skier</description>
	<lastBuildDate>Sat, 21 Jan 2012 01:18:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<atom:link rel="next" href="http://yanpritzker.com/category/code/feed/?page=2" />

		<item>
		<title>The cleanest vimrc you&#8217;ll ever see</title>
		<link>http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/</link>
		<comments>http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 17:02:51 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=446</guid>
		<description><![CDATA[So many of us use vimrc files that are a frankenstein&#8217;s monster, cobbled together from spare parts, gists, and snippets from wikis. One day I decided I was no longer going to treat my vimrc as a garbage dump. As programmers, we strive toward well factored code, so why do we accept such ugly vimrc&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>So many of us use vimrc files that are a frankenstein&#8217;s monster, cobbled together from spare parts, gists, and snippets from wikis. One day I decided I was no longer going to treat my vimrc as a garbage dump. As programmers, we strive toward well factored code, so why do we accept such ugly vimrc&#8217;s that are impossible to understand, and even worse impossible to share because of their complexity. Let&#8217;s stop the insanity! It turns out it&#8217;s very easy&#8230;</p>
<p>Since vim automatically loads everything in ~/.vim/plugin/, you can just add a directory like ~/.vim/plugin/settings and put each semantic piece in its own snippet. Put your keymaps in ~/.vim/plugin/settings/keymap.vim. Put your visual setup like font and colors into ~/.vim/plugin/settings/gui.vim, etc. </p>
<p>After I did this, I ended up with a vimrc that&#8217;s only about 90 lines long including very detailed comments about every section. </p>
<p>I think this is the <a href="https://github.com/skwp/dotfiles/blob/master/vimrc">cleanest vimrc you&#8217;ll see on the web.</a> All my <a href="https://github.com/skwp/dotfiles/tree/master/vim/plugin/settings">vim plugin settings are broken down into individual files</a>, following a convention of one file per vim plugin that I use, plus some extra ones that are too small to be plugins. </p>
<p><strong>Think your vimrc is better? Show me! I want to learn :)</strong> </p>
<p>My vimrc and the rest of my vim setup is available as part of my <a href="http://github.com/skwp/dotfiles">YADR (Yet Another Dotfiles Repo) project</a>. Come check it out, I need alpha testers!</p>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Dotfiles for insane productivity in bash, git, and vim</title>
		<link>http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/</link>
		<comments>http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 22:59:57 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=356</guid>
		<description><![CDATA[Following the principles of insane productivity that I blogged about earlier, I am releasing my dotfile repository, containing customizations for bash, git, and vim. The guiding principles are two letter mnemonics for everything bash and git related, and single letter capital letter actions for all common Vim commands. The selection of vim plugins is best [...]]]></description>
			<content:encoded><![CDATA[<p>Following the principles of <a href="http://yanpritzker.com/2006/10/03/five-ways-to-get-insane-productivity-boosts/">insane productivity</a> that I blogged about earlier, I am releasing my <a href="http://github.com/skwp/dotfiles">dotfile repository</a>, containing customizations for bash, git, and vim.</a> The guiding principles are two letter mnemonics for everything bash and git related, and single letter capital letter actions for all common Vim commands. The selection of vim plugins is best of breed for navigation, color highlighting, and git integration. The vim setup is optimized for rails and git development. The vimrc contains my custom single-letter aliases and is pretty well commented, so that you can easily take or reject my modifications as you need. and I will continue to clean up the repo and move everything into tpope&#8217;s pathogen plugin format over time. Enjoy and please send feedback!</p>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2011/11/17/dotfiles-for-ultimate-productivity-in-bash-and-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>vim/rails/git: find references to current partial</title>
		<link>http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/</link>
		<comments>http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 16:34:57 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=344</guid>
		<description><![CDATA[Update 12/15/2011: Released as a plugin on github Blazing fast way of finding references to the current partial you&#8217;re viewing using this simple script. Requires git-grep.vim. Bonus: at the bottom of the script I also define &#8220;K&#8221; to git grep the current word. Very handy for fast function reference finding. Like this post? Here are [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update 12/15/2011: Released as a plugin <a href="https://github.com/skwp/vim-git-grep-rails-partial">on github</a></strong></p>
<p>Blazing fast way of finding references to the current partial you&#8217;re viewing using this simple script. Requires <a href="https://github.com/tjennings/git-grep-vim">git-grep.vim</a>. Bonus: at the bottom of the script I also define &#8220;K&#8221; to git grep the current word. Very handy for fast function reference finding.</p>
<p><script src="https://gist.github.com/1327904.js?file=git_grep_partial.vim"></script></p>
<p>Like this post? Here are some more you might enjoy:</p>
<ul>
<li><a href="http://yanpritzker.com/2006/10/03/five-ways-to-get-insane-productivity-boosts/">Five ways to get insane productivity boosts</a></li>
<li><a href="http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/">Colorful ruby tests and debugging</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2011/10/31/vimrailsgit-find-references-to-current-partial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Colorful Vim Ruby Tests and Debugging</title>
		<link>http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/</link>
		<comments>http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 17:07:53 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=286</guid>
		<description><![CDATA[Update 12/15/2011: released as a plugin on github Bet your TextMate can&#8217;t do this! Screenshot first, how-to follows :) And now the how-to&#8230; ConqueTerm for a color-enabled embedded vim terminal ConqueTerm is pretty magical. It gives you an embedded shell where you can run bash, rib, rake, really..you name it. All with full ansi color [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update 12/15/2011: released as a plugin <a href="https://github.com/skwp/vim-ruby-conque">on github</a></strong></p>
<p>Bet your TextMate can&#8217;t do this! Screenshot first, how-to follows :)</p>
<p><a href="http://i.imgur.com/dhM1z.png"><img src="http://i.imgur.com/dhM1z.png" style='width:400px'/></a></p>
<p>And now the how-to&#8230;</p>
<h3>ConqueTerm for a color-enabled embedded vim terminal</h3>
<p>ConqueTerm is pretty magical. It gives you an embedded shell where you can run bash, rib, rake, really..you name it. All with full ansi color support. This lets us run red green tests that actually look green and not as a bunch of ansi escape codes.</p>
<ul>
<li><a href="http://code.google.com/p/conque/">Homepage</a></li>
<li><a href="http://conque.googlecode.com/files/conque_2.3.vmb">Direct Download Link</a></li>
<li>Install: follow directions on homepage or just <code>:so conque_2.3.vmb</code> from vim and restart</li>
</ul>
<h3>Setting up aliases for running bash, rails console, and irb</h3>
<p>Put this snippet into your .vimrc. This will give you a few aliases to use in vim:</p>
<ul>
<li><code>\r</code> for <strong>r</strong>uby on the current file as a ruby executable (great for unit tests)</li>
<li><code>\R</code> for <strong>R</strong>ake, great for running the entire test suite</li>
<li><code>\S</code> for R<strong>S</strong>pec on the current file</li>
<li><code>\i</code> for <strong>i</strong>rb (actually pry)</li>
<li><code>\c</code> for the Rails <strong>c</strong>onsole </li>
<li><code>\b</code> for <strong>b</strong>ash</li>
<li><code>\e</code> to <strong>e</strong>valuate the current selection in the already open console</li>
</ul>
<p><script src="https://gist.github.com/1316994.js"> </script></p>
<p>To get your tests to run red and green, <code>gem install redgreen</code> and <code>require 'redgreen'</code> in your test helper. By using the above <code>\r</code> alias with your ruby files, you will now see the output of your red green tests inside the colorized ConqueTerm. You can also run rspec colorized in ConqueTerm.</p>
<h3>Variable inspection and debugging with Pry</h3>
<p><code>gem install pry</code> is one of the most brilliant and useful things I&#8217;ve seen. Simply by requiring this gem in your code and then adding a <code>binding.pry</code> in the spot where you want to break, you will be dumped into a very nice (and again colorized if you use the ConqueTerm) debugging context where you can inspect any variable by simply typing its name. </p>
<p>The other cool thing is that Pry lets you treat your code like a filesystem (try <code>ls</code> and <code>cd [varname]</code> to dive into complex objects). Try it out! You can also use pry as an irb substitute. Simply type <code>pry</code> at the bash command line, or use <code>script/console --irb=pry</code> with your Rails 2.x project. Here is an <a href="https://github.com/pry/pry/wiki/Setting-up-Rails-to-use-Pry">page on pry&#8217;s github site on how to set it up in other environments</a>. </p>
<p>Pry does <a href="https://github.com/pry/pry/wiki">a whole ton more</a> including the ability to create &#8216;macros&#8217; in your irb, but that&#8217;s a whole different post. </p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2011/10/26/colorful-vim-ruby-tests-and-debugging/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fear and loathing in code</title>
		<link>http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/</link>
		<comments>http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 16:24:32 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://yanpritzker.com/?p=236</guid>
		<description><![CDATA[When we develop code from a position of fear rather than one of confidence, we make design decisions that lead to unmaintainable code. A developer who is afraid to mess things up asks himself: what is the absolute minimal change I can make to fix the symptoms without affecting underlying behavior. The answer involves creating [...]]]></description>
			<content:encoded><![CDATA[<p>When we develop code from a position of fear rather than one of confidence, we make design decisions that lead to unmaintainable code. </p>
<p>A developer who is afraid to mess things up asks himself: <em>what is the absolute minimal change I can make to fix the symptoms without affecting underlying behavior.</em> The answer involves creating some workaround, patch, or quick fix that hides the problem at hand without addressing the underlying cause. The upside is the developer feels safe &#8211; by making a minimal change he has taken on minimal risk, and he probably won&#8217;t get yelled at for breaking things.</p>
<p>But fear of change has a big price. Every piece of code that patches a symptom instead of fixing deep seated issues is moving the codebase towards less maintainability. Even if it lets us squeeze a release out today, it&#8217;s a hidden burden on future releases, and it&#8217;s a huge burden on every developer that comes to fix it later. </p>
<p>When you don&#8217;t continuously work to improve old code, you risk the failure not only of the product, but the entire team. Developers throw their hands up, leave the company, and new developers don&#8217;t want to touch legacy code. The Rewrite is declared, but without addressing any of the underlying Fear in developer DNA, we are bound to repeat the same mistakes.</p>
<p>The correct question to ask is: <em>what is the root of the problem, what else in the system may be affected by it, and what do I need to change in order for this system to be more maintainable tomorrow than it is today.</em> The answer to this question is usually not easy, takes longer to implement, and is harder to explain to your boss, who wants the product out next week. But it&#8217;s nevertheless the right question to ask.</p>
<p>To create maintainable systems that survive the test of time and, perhaps more importantly to the company&#8217;s survival, create code that is inviting to new developers as we grow the team, we have to develop from a position of confidence rather than fear. Here&#8217;s how we get there:</p>
<ul>
<li><strong>Declare one day of the week testing/refactoring day</strong> where no new functionality is developed, and everyone is asked to pick a part of the system that is turning into unmaintainable legacy code and bring it back to life. This is a minimum, not a maximum. Refactoring should be constant and ongoing.</li>
<li><strong>Add tests to provide an insurance policy against breakage.</strong> If working on a part of system that is untested, write tests first before making your changes. Change is less risky with the safety net of tests underneath you.</li>
<li><strong>Encourage collective code ownership.</strong> It&#8217;s fine for one person to be the thought and code leader on a project but if no one else ever looks at that code, it is feared by the rest of the team, and the success of the project rests on a single developer. If he leaves the company, the project is finished.</li>
<li><strong>Use code reviews to minimize risk of large changes</strong> and encourage a team effort in cleaning up the system. Ask developers to spend half of their refactoring time reviewing and/or refactoring code from a part of the system that is not their own.</li>
<li><strong>Encourage code walkthroughs</strong> (distributed teams, use screencasts). Create a time and place for people to show off techniques they used, or just introduce others to parts of the system they may not be familiar with.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2010/11/29/fear-and-loathing-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Five Rules for Writing Good Code</title>
		<link>http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/</link>
		<comments>http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 21:22:55 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[conventions]]></category>
		<category><![CDATA[habits]]></category>

		<guid isPermaLink="false">http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/</guid>
		<description><![CDATA[1. Write for an audience Code tends to outlive jobs. When we create code, we are not writing for ourselves, but for an audience of peers and progeny that will look upon it and have to maintain it. Do we want them to do so with awe and respect, or with fear and disgust? Writing [...]]]></description>
			<content:encoded><![CDATA[<p>1. Write for an audience</p>
<p>Code tends to outlive jobs. When we create code, we are not writing for ourselves, but for an audience of peers and progeny that will look upon it and have to maintain it. Do we want them to do so with awe and respect, or with fear and disgust? Writing maintainable code not only makes our life easier, but the lives of those around us, and garners admiration, praise, and rewards &#8211; if not always financial, at the very least karmic.</p>
<p>2. Establish a clean framework for future changes</p>
<p>If we start writing new code, and the pattern we establish is that we&#8217;ve copied and pasted a line across ten functions, what will happen when someone else comes along to add an eleventh function? Let&#8217;s face it, even the neatest programmers can get lazy. If something has been copy-pasted ten times, it will get copy-pasted for the 11th time, and a year later, when we find out we have to change the logic or content of that line, we are now changing 50 lines where we could have had one. Untangling code is always a lot harder than writing it, and we could have prevented the spread of the copy-paste disease by evaluating our code initially for repetitive statements. Always establish a <i>framework</i> for others to follow by writing clean code that reduces repetition, and encourages maintainability by isolating each piece of logic and content to one spot only.</p>
<p>3. Be brief, self-descriptive, and avoid inline comments</p>
<p>Five line functions with descriptive names are easy to understand. They require no comments. Thirty line functions take quite a bit of brainpower to digest, and usually have smelly comments scattered all throughout trying to explain bits of the function. Hundred line functions are an assault on all that is holy and stink to high heaven. If we see lots of comments interspersed in a method, it is a good sign that the code cannot be easily understood. Break it down into lots of little functions with descriptive names, and all of a sudden our code reads like a very clear and concise recipe, and we find that the comments become redundant. Most comments that live inside functions are parasitic organisms, treat them with suspicion.</p>
<p>4. Follow language standards and community conventions</p>
<p>If we break conventions, the next person to read our code will wonder why we did so. When our code raises questions about its style, the reader may start wondering if there was some specific reason that we coded it that way. Worse, they may perpetuate our unusual style through imitation and copy-paste tactics. This makes code harder to understand and wastes other people&#8217;s time. I&#8217;ve heard an argument from more than one person that they will code with the style they are comfortable with rather than follow language convention. This is especially true of people migrating from one language to another without investing their time in learning new habits and techniques. Remember that most of the time, we are writing code that <i>someone else</i> will be maintaining. Make their job easier by conforming to widely acknowledged standards, so they don&#8217;t have to spend any extra mental energy reading through it. It may take we a larger effort initially, but it will pay off with dividends when it comes to maintenance.</p>
<p>5. Really learn the language and the framework</p>
<p>Lots of bad code is written because of language or framework ignorance. If we don&#8217;t know the framework we use, we might reinvent the wheel or write obtuse code because we&#8217;re not taking advantage of the conventions and helpers already created for us. No need to be a walking encyclopedia, but remember to occasionally open up that encyclopedia and read through it so that you know at least what&#8217;s out there. I am sometimes surprised by new things I find in a framework I&#8217;ve used for quite a while, that makes my life a whole lot easier. Don&#8217;t neglect the docs, and don&#8217;t neglect to keep up with blogs that discuss new techniques.</p>
<p>I&#8217;m not really back, I&#8217;m just pretending. That&#8217;s all for now :)</p>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2009/09/29/five-rules-for-writing-good-code/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Long running Threads in Rails and metaprogramming fun</title>
		<link>http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/</link>
		<comments>http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 03:07:41 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[background]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[thoughts]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://skwpspace.com/?p=166</guid>
		<description><![CDATA[Disclaimer: This post contains evil (but highly fun!) code. Proceed at your own peril&#8230; I was recently designing an application that needed to execute some long running requests against an external host. If you&#8217;ve ever tried doing something like this in Rails, you&#8217;ll find your mongrels will block up waiting for the request to complete, [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: This post contains evil (but highly fun!) code. Proceed at your own peril&#8230;</p>
<p>I was recently designing an application that needed to execute some long running requests against an external host. If you&#8217;ve ever tried doing something like this in Rails, you&#8217;ll find your mongrels will block up waiting for the request to complete, bringing the experience for all other users to a halt. </p>
<p>I wanted to dispatch my long running request, return to the user, and then poll for results using AJAX. There are many ways to do background tasks in Rails, most of which require running an out of process background server with which you will communicate over some sort of queue or memcached. There&#8217;s <a href="http://backgroundrb.rubyforge.org/">BackgroundRb</a>, <a href="http://agilewebdevelopment.com/plugins/bj">Bj</a>, <a href="http://playtype.net/past/2008/2/6/starling_and_asynchrous_tasks_in_ruby_on_rails/">workling</a>, and so on, but this seemed overkill for my problem.</p>
<p>After reading a <a href="http://bibwild.wordpress.com/2007/08/28/threading-in-rails/">post on using Ruby Threads</a>, I decided to be brave and try this approach. I implemented a simple action which would spawn a thread and proceed to return the result whether it was ready or not. This action is polled via AJAX and on the next poll the result will be correct. The pseudocode looks something like</p>
<pre class="textmate-source black_pearl"><span class="source source_ruby"><span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">long_running_action</span></span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span>spawn a thread
</span>  precache_the_results

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> This action throws DataNotAvailableException
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> if file is missing/unreadable
</span>  results <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> read_cached_results

<span class="keyword keyword_control keyword_control_ruby">rescue</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">DataNotAvailableException</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> This tells me that when I load the page
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> I should invoke an ajax a couple seconds
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> later to check for results again
</span>  flash<span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">[</span><span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>update_right_away</span><span class="punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby">]</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="constant constant_language constant_language_ruby">true</span>
<span class="keyword keyword_control keyword_control_ruby">ensure</span>
  respond_to <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do </span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span><span class="variable variable_other variable_other_block variable_other_block_ruby">wants</span><span class="punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby">|</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">    <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> render an RJS update with the results
</span>  <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">precache_the_results</span></span>
  <span class="support support_class support_class_ruby">Thread</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block">
</span>    expensive_action_outputs_to<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>file.txt<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
  <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>
<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>Because I didn&#8217;t <code>join</code> the Thread to the request thread, it lives on after the request completes, which is just what I needed. Since the code inside my Thread is a call to an external provider and doesn&#8217;t write to the database, I am not concerned with ActiveRecord threading issues.</p>
<p><b>The only problem</b> with this approach is that in development mode, Rails likes to reload your classes on every request. But if your thread runs past the request lifetime, the class that&#8217;s running it may be unloaded while it&#8217;s running, wreaking all sorts of havoc. But Ruby allows us the power to be <em>truly evil:</em>. What if I just prevent Threads from doing what they want to in development mode? Turns out I can!</p>
<pre class="textmate-source black_pearl"><span class="source source_ruby source_ruby_rails"><span class="keyword keyword_control keyword_control_ruby">if</span> <span class="meta meta_environment-variable meta_environment-variable_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">ENV</span>[<span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>RAILS_ENV<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>]</span> <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">==</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>development<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>
<span class="meta meta_class meta_class_ruby">  <span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">Thread</span></span>
    <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">initialize</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby"><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">&amp;</span>block</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
      block<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>call
    <span class="keyword keyword_control keyword_control_ruby">end</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="keyword keyword_control keyword_control_ruby">end</span>
</span></pre>
<p>This code is defined in the class where I&#8217;m doing the magic. Do NOT just slap this into your environment.rb as you&#8217;ll horribly break the Rails startup logic. There&#8217;s probably a slightly smarter and safer way to do this by using a Factory pattern to create the threads and explicitly specifying the implementation you want. But this is my party and I&#8217;ll monkeypatch if I want to.</p>
<p>So..comments, suggestions, complaints? Is this going to die horribly in production? I guess we&#8217;ll have to see!</p>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2008/06/11/long-running-threads-in-rails-and-metaprogramming-fun/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Ruby/html trick: wrap long strings with invisible wordwrap characters</title>
		<link>http://yanpritzker.com/2008/05/13/rubyhtml-trick-wrap-long-strings-with-invisible-wordwrap-characters/</link>
		<comments>http://yanpritzker.com/2008/05/13/rubyhtml-trick-wrap-long-strings-with-invisible-wordwrap-characters/#comments</comments>
		<pubDate>Tue, 13 May 2008 22:23:44 +0000</pubDate>
		<dc:creator>yan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[thoughts]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://skwpspace.com/2008/05/13/rubyhtml-trick-wrap-long-strings-with-invisible-wordwrap-characters/</guid>
		<description><![CDATA[def wrap_long_string(text,max_width = 20) (text.length &#60; max_width) ? text : text.scan(/.{1,#{max_width}}/).join("&#60;wbr&#62;") end]]></description>
			<content:encoded><![CDATA[<pre class="textmate-source black_pearl"><span class="source source_ruby source_ruby_rails"><span class="meta meta_rails meta_rails_helper"><span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">wrap_long_string</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">text<span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>max_width <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="constant constant_numeric constant_numeric_ruby">20</span></span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
  <span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span>text<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>length <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&lt;</span> max_width<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">?</span>
    text <span class="punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby">:</span>
    text<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>scan<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_regexp string_regexp_classic string_regexp_classic_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_ruby">/</span><span class="constant constant_character constant_character_escape constant_character_escape_ruby">.</span>{1,<span class="source source_ruby source_ruby_embedded source_ruby_embedded_source"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">#{</span>max_width<span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">}</span></span>}<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_ruby">/</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>join<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>&lt;wbr&gt;<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
</span><span class="keyword keyword_control keyword_control_ruby">end</span>
</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://yanpritzker.com/2008/05/13/rubyhtml-trick-wrap-long-strings-with-invisible-wordwrap-characters/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

