<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Rock, Paper, Software</title>
	
	<link>http://software.tulentsev.com</link>
	<description>Random thoughts about programming</description>
	<lastBuildDate>Thu, 05 Apr 2012 03:25:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ThoughtsOnSoftwareAndComputers" /><feedburner:info uri="thoughtsonsoftwareandcomputers" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Can I haz a gem?</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/3Wkc9AvF58U/</link>
		<comments>http://software.tulentsev.com/2012/04/can-i-haz-a-gem/#comments</comments>
		<pubDate>Thu, 05 Apr 2012 03:23:48 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=364</guid>
		<description><![CDATA[Back when I was just starting to program, we used to joke about Delphi coders. A Delphi coder wants to build a tool for cheating in games (read/write memory of another process). So, before writing a single line of code he goes to a forum and asks: &#8220;Are there ready-made components for building game cheating [...]]]></description>
			<content:encoded><![CDATA[<p>Back when I was just starting to program, we used to joke about Delphi coders.</p>

<blockquote>
  <p>A Delphi coder wants to build a tool for cheating in games (read/write memory of another process). So, before writing a single line of code he goes to a forum and asks: &#8220;Are there ready-made components for building game cheating tools?&#8221;</p>
</blockquote>

<p>I guess, this happens to every widely adopted technology that has plugins/libraries. <a href="http://stackoverflow.com/questions/10022215/building-an-auction-with-ruby-on-rails">An example</a>.</p>

<p><img src="http://software.tulentsev.com/wp-content/uploads/2012/04/screenshot_2012_04_05_071714.png" alt="Screenshot 2012 04 05 07.17.14" /></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=3Wkc9AvF58U:4asw_eH9kos:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=3Wkc9AvF58U:4asw_eH9kos:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=3Wkc9AvF58U:4asw_eH9kos:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=3Wkc9AvF58U:4asw_eH9kos:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/3Wkc9AvF58U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/04/can-i-haz-a-gem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/04/can-i-haz-a-gem/</feedburner:origLink></item>
		<item>
		<title>Connect to multiple MySQL servers</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/hn_-szJE-44/</link>
		<comments>http://software.tulentsev.com/2012/04/connect-to-multiple-mysql-servers/#comments</comments>
		<pubDate>Thu, 05 Apr 2012 03:02:36 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[active-record]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=359</guid>
		<description><![CDATA[You know how everyone is obsessed these days with scalability? Well, I am too. There&#8217;s a project of mine where I need to connect to multiple MySQL servers, depending on the current client id. The simplest implementation looks like this: def self.use_table_for_app aid config = get_connection_config aid ActiveRecord::Base.establish_connection config end This works, but it creates [...]]]></description>
			<content:encoded><![CDATA[<p>You know how everyone is obsessed these days with scalability? Well, I am too. There&#8217;s a project of mine where I need to connect to multiple MySQL servers, depending on the current client id. The simplest implementation looks like this:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_table_for_app</span> aid
  config = get_connection_config aid
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span> config
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>This works, but it creates a new connection on every call. Let&#8217;s suppose, we&#8217;re processing a queue and we&#8217;re handling events from different apps (customers). The cost of setting up a connection can easily outweigh the actual work. We need to cache that somehow.</p>

<p>Now, <code>ActiveRecord::Base</code> has a method called <code>connection_config</code> that returns, well, configuration of current connection. We can compare that to what we have on hands and, if they match, do not reconnect. Here&#8217;s how our code looks like now:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_table_for_app</span> aid
  config = get_connection_config aid
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">unless</span> can_use_connection?<span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">active</span>?
    <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span> config
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> can_use_connection? config
  current_config = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection_config</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># current config can have more keys than our config (or vice versa), so simple hash comparison doesn't work.</span>
  config.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#008000; font-style:italic;"># if even a single parameter is different - can't reuse this connection.</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> config<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span> != current_config<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#008000; font-style:italic;"># TODO: log warning</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>Looks almost good. Now let&#8217;s extract this to a module for ease of re-using. Here&#8217;s the final code.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> SqlModelHelpers
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span> klass
    klass.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:extend</span>, ClassMethods
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> setup_connection aid
      <span style="color:#008000; font-style:italic;"># TODO: do not reestablish good connections.</span>
      <span style="color:#008000; font-style:italic;"># 1. get config to connect</span>
      <span style="color:#008000; font-style:italic;"># 2. compare with current config (ActiveRecord::Base.connection_config)</span>
      <span style="color:#008000; font-style:italic;"># 3. if configs match and connection is open - do not establish_connection</span>
      config = get_connection_config aid
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">unless</span> can_use_connection?<span style="color:#006600; font-weight:bold;">&#40;</span>config<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">active</span>?
        <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span> config
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> can_use_connection? config
      current_config = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection_config</span>
&nbsp;
      config.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">if</span> config<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span> != current_config<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span>
          <span style="color:#008000; font-style:italic;"># TODO: log warning</span>
          <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyModel <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> SqlModelHelpers
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">use_table_for_app</span> aid
    setup_connection aid
&nbsp;
    table_name
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>Now if we want to use this functionality in some other models, we just include that module. This code can certainly be improved further, post your suggestions :)</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=hn_-szJE-44:Q4ENM43PtVM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=hn_-szJE-44:Q4ENM43PtVM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=hn_-szJE-44:Q4ENM43PtVM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=hn_-szJE-44:Q4ENM43PtVM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/hn_-szJE-44" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/04/connect-to-multiple-mysql-servers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/04/connect-to-multiple-mysql-servers/</feedburner:origLink></item>
		<item>
		<title>Define module programmatically</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/hHFV5D9EYI4/</link>
		<comments>http://software.tulentsev.com/2012/04/define-module-programmatically/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 19:14:50 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=354</guid>
		<description><![CDATA[module Foo end Given a module Foo, how do you define a nested module Bar? module Foo module Bar end end There&#8217;s a number of ways. First, most obvious one is to eval a string. module Foo end &#160; name = 'Bar' &#160; Foo.class_eval &#60;&#60;RUBY module #{name} end RUBY &#160; Foo::Bar # =&#62; Foo::Bar While [...]]]></description>
			<content:encoded><![CDATA[


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Foo
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>Given a module <code>Foo</code>, how do you define a nested module <code>Bar</code>?</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Foo
    <span style="color:#9966CC; font-weight:bold;">module</span> Bar
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>There&#8217;s a number of ways. First, most obvious one is to eval a string.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Foo
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
name = <span style="color:#996600;">'Bar'</span>
&nbsp;
Foo.<span style="color:#9900CC;">class_eval</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;RUBY
  module #{name}
  end
RUBY</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Foo::Bar</span> <span style="color:#008000; font-style:italic;"># =&gt; Foo::Bar</span></pre></div></div>




<p>While this certainly works and gets job done, it has some flaws. First, it&#8217;s a string, so some editors and IDEs will get confused and lose coloring/completion. Second, there&#8217;s no validation on module name. In best case, you&#8217;ll get compiler error. In worst case, you&#8217;ll get hard to track bugs.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Foo
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
bar = <span style="color:#9966CC; font-weight:bold;">Module</span>.<span style="color:#9900CC;">new</span>
Foo.<span style="color:#9900CC;">const_set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:Bar</span>, bar<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Foo::Bar</span> <span style="color:#008000; font-style:italic;"># =&gt; Foo::Bar</span></pre></div></div>




<p>This one is better. You clearly state that you&#8217;re going to set a constant, and module name is pretty restricted.</p>

<p>Are there other ways to do this?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=hHFV5D9EYI4:yNGCrTyzpto:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=hHFV5D9EYI4:yNGCrTyzpto:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=hHFV5D9EYI4:yNGCrTyzpto:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=hHFV5D9EYI4:yNGCrTyzpto:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/hHFV5D9EYI4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/04/define-module-programmatically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/04/define-module-programmatically/</feedburner:origLink></item>
		<item>
		<title>Ruby: for…in loop</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/3tcKAnb8-iU/</link>
		<comments>http://software.tulentsev.com/2012/03/ruby-forin-loop/#comments</comments>
		<pubDate>Sat, 31 Mar 2012 03:19:36 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=349</guid>
		<description><![CDATA[You live to learn every day. Today I discovered for..in loop in ruby. When I saw it in a question on stackoverflow, I was like &#8220;Hey, dude, this is ruby, not javascript!&#8221; in my head. But, apparently, it&#8217;s legal ruby :) arr = &#91;1, 2, 3&#93; &#160; for a in arr puts &#34;element #{a}&#34; end [...]]]></description>
			<content:encoded><![CDATA[<p>You live to learn every day. Today I discovered <code>for..in</code> loop in ruby. When I saw it in a question on <a href="http://stackoverflow.com">stackoverflow</a>, I was like &#8220;Hey, dude, this is ruby, not javascript!&#8221; in my head. But, apparently, it&#8217;s legal ruby :)</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">arr = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">for</span> a <span style="color:#9966CC; font-weight:bold;">in</span> arr
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;element #{a}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#=&gt; element 1</span>
<span style="color:#008000; font-style:italic;">#=&gt; element 2</span>
<span style="color:#008000; font-style:italic;">#=&gt; element 3</span></pre></div></div>




<p>You can put ranges in there also.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">for</span> a <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> a
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>I usually write such loops with <code>.each</code>, but good to know there&#8217;s another way.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=3tcKAnb8-iU:wHN74_kxz58:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=3tcKAnb8-iU:wHN74_kxz58:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=3tcKAnb8-iU:wHN74_kxz58:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=3tcKAnb8-iU:wHN74_kxz58:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/3tcKAnb8-iU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/03/ruby-forin-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/03/ruby-forin-loop/</feedburner:origLink></item>
		<item>
		<title>Fun with classes: find which class is the biggest :)</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/NOmt7CLHOU8/</link>
		<comments>http://software.tulentsev.com/2012/03/fun-with-classes-find-which-class-is-the-biggest/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 21:13:27 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=343</guid>
		<description><![CDATA[Did you know that in Ruby you can compare classes with &#8216;less-than&#8217; and &#8216;greater-than&#8217; operators? I did not (until today). Observe: class A end &#160; class B &#60; A end &#160; class C &#60; A end &#160; class D &#60; B end &#160; A &#60; B # false A &#62; B # true D &#60; [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know that in Ruby you can compare classes with &#8216;less-than&#8217; and &#8216;greater-than&#8217; operators? I did not (until today). Observe:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> B <span style="color:#006600; font-weight:bold;">&lt;</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> C <span style="color:#006600; font-weight:bold;">&lt;</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> D <span style="color:#006600; font-weight:bold;">&lt;</span> B
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
A <span style="color:#006600; font-weight:bold;">&lt;</span> B <span style="color:#008000; font-style:italic;"># false</span>
A <span style="color:#006600; font-weight:bold;">&gt;</span> B <span style="color:#008000; font-style:italic;"># true</span>
D <span style="color:#006600; font-weight:bold;">&lt;</span> A <span style="color:#008000; font-style:italic;"># true</span>
D <span style="color:#006600; font-weight:bold;">&lt;</span> B <span style="color:#008000; font-style:italic;"># true</span>
D <span style="color:#006600; font-weight:bold;">&lt;</span> C <span style="color:#008000; font-style:italic;"># nil</span>
C <span style="color:#006600; font-weight:bold;">&gt;</span> D <span style="color:#008000; font-style:italic;"># nil</span></pre></div></div>




<p>This operator returns a boolean value (as one would expect), so you can write code like this:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;B inherits from A&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> B <span style="color:#006600; font-weight:bold;">&lt;</span> A</pre></div></div>




<p>Note how it resembles the class definition syntax. I think that this is simply brilliant (not very intuitive, though. I wouldn&#8217;t think of writing this code).</p>

<p>Now that we know this fact, let&#8217;s try to sort the classes :)</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> B <span style="color:#006600; font-weight:bold;">&lt;</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> C <span style="color:#006600; font-weight:bold;">&lt;</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> D <span style="color:#006600; font-weight:bold;">&lt;</span> B
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> E <span style="color:#006600; font-weight:bold;">&lt;</span> A
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#9966CC; font-weight:bold;">Class</span>
  <span style="color:#008000; font-style:italic;"># we need to define the spaceship operator for classes, since it's not defined yet.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&lt;=&gt;</span> other
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> == other
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">&lt;</span> other
    <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
klasses = <span style="color:#006600; font-weight:bold;">&#91;</span>A, B, C, D, E<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
klasses.<span style="color:#9900CC;">sort</span> <span style="color:#008000; font-style:italic;"># [E, C, D, B, A]</span></pre></div></div>




<p>Now this class hierarchy is sorted, with children first and parents last. Homework: come up with a practical application for this :)</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=NOmt7CLHOU8:7nKWqgw84cY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=NOmt7CLHOU8:7nKWqgw84cY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=NOmt7CLHOU8:7nKWqgw84cY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=NOmt7CLHOU8:7nKWqgw84cY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/NOmt7CLHOU8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/03/fun-with-classes-find-which-class-is-the-biggest/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/03/fun-with-classes-find-which-class-is-the-biggest/</feedburner:origLink></item>
		<item>
		<title>How to dump your MongoDB database partially (only selected tables)</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/TH11wBNbZ4o/</link>
		<comments>http://software.tulentsev.com/2012/03/how-to-dump-your-mongodb-database-partially-only-selected-tables/#comments</comments>
		<pubDate>Wed, 21 Mar 2012 11:15:22 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[MongoDb]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=335</guid>
		<description><![CDATA[Let&#8217;s say you want to dump your MongoDB database. There&#8217;s a handy tool that does just that, mongodump. mongodump If executes without any arguments it will try to connect to localhost:27017 and dump all databases. You can specify a single database that you&#8217;re interested in and it will dump just this database. mongodump -d mydb [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you want to dump your MongoDB database. There&#8217;s a handy tool that does just that, <strong>mongodump</strong>.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mongodump</pre></div></div>




<p>If executes without any arguments it will try to connect to localhost:27017 and dump all databases. You can specify a single database that you&#8217;re interested in and it will dump just this database.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mongodump <span style="color: #660033;">-d</span> mydb</pre></div></div>




<p>But in some cases you don&#8217;t want to dump the whole database. In my case, it&#8217;s an analytics application and 99% of data is in raw event collections (<strong>events20120320</strong>, <strong>events20120321</strong>, &#8230;). I am interested only in a small number of &#8220;important&#8221; collections. But <strong>mongodump</strong> doesn&#8217;t provide us with an option to specify several collections. You can only dump one collection at a time. If you don&#8217;t mind some typing, that&#8217;s easy.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mongodump <span style="color: #660033;">-d</span> mydb <span style="color: #660033;">-c</span> mycoll1
mongodump <span style="color: #660033;">-d</span> mydb <span style="color: #660033;">-c</span> mycoll2
mongodump <span style="color: #660033;">-d</span> mydb <span style="color: #660033;">-c</span> mycoll5</pre></div></div>




<p>But we&#8217;re all programmers, so let&#8217;s automate this stuff. I, personally, never used bash loops before, and it seems like a good use case for them. Let&#8217;s do it.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">colls</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span> mycoll1 mycoll2 mycoll5 <span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> c <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${colls[@]}</span>
<span style="color: #000000; font-weight: bold;">do</span>
  mongodump <span style="color: #660033;">-d</span> mydb <span style="color: #660033;">-c</span> <span style="color: #007800;">$c</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>




<p>First line defines a bash array literal. <strong>Don&#8217;t use commas to delimit array elements</strong>, they&#8217;ll become part of the element. The <strong>${colls[@]}</strong> string means &#8220;all array elements&#8221; and it can be used anywhere where a variable is expected. The rest is pretty straightforward, I think.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=TH11wBNbZ4o:4LG4hSZAl5k:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=TH11wBNbZ4o:4LG4hSZAl5k:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=TH11wBNbZ4o:4LG4hSZAl5k:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=TH11wBNbZ4o:4LG4hSZAl5k:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/TH11wBNbZ4o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/03/how-to-dump-your-mongodb-database-partially-only-selected-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/03/how-to-dump-your-mongodb-database-partially-only-selected-tables/</feedburner:origLink></item>
		<item>
		<title>Deploying with Sinatra + Capistrano + Unicorn</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/R-VPrLWtths/</link>
		<comments>http://software.tulentsev.com/2012/03/deploying-with-sinatra-capistrano-unicorn/#comments</comments>
		<pubDate>Fri, 09 Mar 2012 12:02:46 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sinatra]]></category>
		<category><![CDATA[unicorn]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=323</guid>
		<description><![CDATA[Today we&#8217;ll be deploying a simple Sinatra app with Capistrano, using Unicorn as our web server. First things first: let&#8217;s think of a stupid name for this project. What about &#8220;sincapun&#8221;? Any objections? Good, let&#8217;s proceed. mkdir sincapun cd sincapun Minimal runnable version: # Gemfile gem 'sinatra' &#160; &#160; # sincapun.rb require 'sinatra' &#160; get [...]]]></description>
			<content:encoded><![CDATA[<p>Today we&#8217;ll be deploying a simple Sinatra app with Capistrano, using Unicorn as our web server. First things first: let&#8217;s think of a stupid name for this project. What about &#8220;sincapun&#8221;? Any objections? Good, let&#8217;s proceed.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> sincapun
<span style="color: #7a0874; font-weight: bold;">cd</span> sincapun</pre></div></div>




<p>Minimal runnable version:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Gemfile</span>
gem <span style="color:#996600;">'sinatra'</span>
&nbsp;
&nbsp;
<span style="color:#008000; font-style:italic;"># sincapun.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
&nbsp;
get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#996600;">&quot;Hello world&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>Let&#8217;s see if we can run it&#8230;<span id="more-323"></span></p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ bundle <span style="color: #7a0874; font-weight: bold;">exec</span> ruby sincapun.rb 
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2012</span>-03-09 <span style="color: #000000;">16</span>:04:<span style="color: #000000;">17</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> INFO  WEBrick 1.3.1
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2012</span>-03-09 <span style="color: #000000;">16</span>:04:<span style="color: #000000;">17</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> INFO  ruby 1.9.3 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2012</span>-02-<span style="color: #000000;">16</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>x86_64-darwin11.3.0<span style="color: #7a0874; font-weight: bold;">&#93;</span>
== Sinatra<span style="color: #000000; font-weight: bold;">/</span>1.3.2 has taken the stage on <span style="color: #000000;">4567</span> <span style="color: #000000; font-weight: bold;">for</span> development with backup from WEBrick
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2012</span>-03-09 <span style="color: #000000;">16</span>:04:<span style="color: #000000;">17</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> INFO  WEBrick::HTTPServer<span style="color: #666666; font-style: italic;">#start: pid=14871 port=4567</span></pre></div></div>




<p>So far, so good. Now let&#8217;s convert it to modular app and create a proper rackup file.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># sincapun.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Sincapun <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Sinatra::Base</span>
  get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#996600;">&quot;Hello world&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># config.ru</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'./sincapun'</span>
run Sincapun</pre></div></div>




<p>At this point our app is runnable by every Rack-compatible server (thin, unicorn, &#8230;). Let&#8217;s add some Capistrano to it.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Gemfile</span>
source <span style="color:#ff3333; font-weight:bold;">:rubygems</span>
&nbsp;
gem <span style="color:#996600;">'sinatra'</span>
gem <span style="color:#996600;">'unicorn'</span>
gem <span style="color:#996600;">'capistrano'</span></pre></div></div>





<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ bundle <span style="color: #c20cb9; font-weight: bold;">install</span>
Using highline <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.6.11<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using net-ssh <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.3.0<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using net-scp <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.0.4<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using net-sftp <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.0.5<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using net-ssh-gateway <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.1.0<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using capistrano <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.11.2<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using kgio <span style="color: #7a0874; font-weight: bold;">&#40;</span>2.7.2<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using rack <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.4.1<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using rack-protection <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.2.0<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using raindrops <span style="color: #7a0874; font-weight: bold;">&#40;</span>0.8.0<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using tilt <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.3.3<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using sinatra <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.3.2<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using unicorn <span style="color: #7a0874; font-weight: bold;">&#40;</span>4.2.0<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Using bundler <span style="color: #7a0874; font-weight: bold;">&#40;</span>1.0.22<span style="color: #7a0874; font-weight: bold;">&#41;</span> 
Your bundle is <span style="color: #7a0874; font-weight: bold;">complete</span><span style="color: #000000; font-weight: bold;">!</span> Use <span style="color: #000000; font-weight: bold;">`</span>bundle show <span style="color: #7a0874; font-weight: bold;">&#91;</span>gemname<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">`</span> to see where a bundled gem is installed.
&nbsp;
$ capify .
<span style="color: #7a0874; font-weight: bold;">&#91;</span>add<span style="color: #7a0874; font-weight: bold;">&#93;</span> writing <span style="color: #ff0000;">'./Capfile'</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>add<span style="color: #7a0874; font-weight: bold;">&#93;</span> making directory <span style="color: #ff0000;">'./config'</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>add<span style="color: #7a0874; font-weight: bold;">&#93;</span> writing <span style="color: #ff0000;">'./config/deploy.rb'</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">done</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> capified<span style="color: #000000; font-weight: bold;">!</span></pre></div></div>




<p>Let&#8217;s improve our capistrano config a little bit.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config/deploy.rb</span>
<span style="color:#008000; font-style:italic;"># We're using RVM on a server, need this.</span>
$:.<span style="color:#9900CC;">unshift</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'./lib'</span>, ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'rvm_path'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rvm/capistrano'</span>
set <span style="color:#ff3333; font-weight:bold;">:rvm_ruby_string</span>, <span style="color:#996600;">'1.9.3'</span>
set <span style="color:#ff3333; font-weight:bold;">:rvm_type</span>, <span style="color:#ff3333; font-weight:bold;">:user</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Bundler tasks</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'bundler/capistrano'</span>
&nbsp;
set <span style="color:#ff3333; font-weight:bold;">:application</span>, <span style="color:#996600;">&quot;sincapun&quot;</span>
set <span style="color:#ff3333; font-weight:bold;">:repository</span>,  <span style="color:#996600;">&quot;git@github.com:stulentsev/sincapun.git&quot;</span>
&nbsp;
set <span style="color:#ff3333; font-weight:bold;">:scm</span>, <span style="color:#ff3333; font-weight:bold;">:git</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># do not use sudo</span>
set <span style="color:#ff3333; font-weight:bold;">:use_sudo</span>, <span style="color:#0000FF; font-weight:bold;">false</span>
set<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:run_method</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> use_sudo ? <span style="color:#ff3333; font-weight:bold;">:sudo</span> : <span style="color:#ff3333; font-weight:bold;">:run</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># This is needed to correctly handle sudo password prompt</span>
default_run_options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:pty</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
set <span style="color:#ff3333; font-weight:bold;">:user</span>, <span style="color:#996600;">&quot;myname&quot;</span>
set <span style="color:#ff3333; font-weight:bold;">:group</span>, user
set <span style="color:#ff3333; font-weight:bold;">:runner</span>, user
&nbsp;
set <span style="color:#ff3333; font-weight:bold;">:host</span>, <span style="color:#996600;">&quot;#{user}@myhost&quot;</span> <span style="color:#008000; font-style:italic;"># We need to be able to SSH to that box as this user.</span>
role <span style="color:#ff3333; font-weight:bold;">:web</span>, host
role <span style="color:#ff3333; font-weight:bold;">:app</span>, host
&nbsp;
set <span style="color:#ff3333; font-weight:bold;">:rails_env</span>, <span style="color:#ff3333; font-weight:bold;">:production</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Where will it be located on a server?</span>
set <span style="color:#ff3333; font-weight:bold;">:deploy_to</span>, <span style="color:#996600;">&quot;/srv/#{application}&quot;</span>
set <span style="color:#ff3333; font-weight:bold;">:unicorn_conf</span>, <span style="color:#996600;">&quot;#{deploy_to}/current/config/unicorn.rb&quot;</span>
set <span style="color:#ff3333; font-weight:bold;">:unicorn_pid</span>, <span style="color:#996600;">&quot;#{deploy_to}/shared/pids/unicorn.pid&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Unicorn control tasks</span>
namespace <span style="color:#ff3333; font-weight:bold;">:deploy</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  task <span style="color:#ff3333; font-weight:bold;">:restart</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run <span style="color:#996600;">&quot;if [ -f #{unicorn_pid} ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current &amp;&amp; bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  task <span style="color:#ff3333; font-weight:bold;">:start</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run <span style="color:#996600;">&quot;cd #{deploy_to}/current &amp;&amp; bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  task <span style="color:#ff3333; font-weight:bold;">:stop</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    run <span style="color:#996600;">&quot;if [ -f #{unicorn_pid} ]; then kill -QUIT `cat #{unicorn_pid}`; fi&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>We need a config file for Unicorn. Here is what it may look like:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># define paths and filenames</span>
deploy_to = <span style="color:#996600;">&quot;/srv/sincapun&quot;</span>
rails_root = <span style="color:#996600;">&quot;#{deploy_to}/current&quot;</span>
pid_file = <span style="color:#996600;">&quot;#{deploy_to}/shared/pids/unicorn.pid&quot;</span>
socket_file= <span style="color:#996600;">&quot;#{deploy_to}/shared/unicorn.sock&quot;</span>
log_file = <span style="color:#996600;">&quot;#{rails_root}/log/unicorn.log&quot;</span>
err_log = <span style="color:#996600;">&quot;#{rails_root}/log/unicorn_error.log&quot;</span>
old_pid = pid_file <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'.oldbin'</span>
&nbsp;
timeout <span style="color:#006666;">30</span>
worker_processes <span style="color:#006666;">2</span> <span style="color:#008000; font-style:italic;"># increase or decrease</span>
listen socket_file, <span style="color:#ff3333; font-weight:bold;">:backlog</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1024</span>
&nbsp;
pid pid_file
stderr_path err_log
stdout_path log_file
&nbsp;
<span style="color:#008000; font-style:italic;"># make forks faster</span>
preload_app <span style="color:#0000FF; font-weight:bold;">true</span> 
&nbsp;
<span style="color:#008000; font-style:italic;"># make sure that Bundler finds the Gemfile</span>
before_exec <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>server<span style="color:#006600; font-weight:bold;">|</span>
  ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'BUNDLE_GEMFILE'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">expand_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'../Gemfile'</span>, <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
before_fork <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>server, worker<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">and</span>
      <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">disconnect</span>!
&nbsp;
  <span style="color:#008000; font-style:italic;"># zero downtime deploy magic:</span>
  <span style="color:#008000; font-style:italic;"># if unicorn is already running, ask it to start a new process and quit.</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>old_pid<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> server.<span style="color:#9900CC;">pid</span> != old_pid
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      <span style="color:#CC00FF; font-weight:bold;">Process</span>.<span style="color:#9900CC;">kill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;QUIT&quot;</span>, <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>old_pid<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Errno</span>::ENOENT, <span style="color:#CC00FF; font-weight:bold;">Errno</span>::ESRCH
      <span style="color:#008000; font-style:italic;"># someone else did our job for us</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
after_fork <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>server, worker<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># re-establish activerecord connections.</span>
  <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">and</span>
      <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>




<p>That should do it. Now you can deploy your app, assuming that you have <a href="http://beginrescueend.com/">RVM</a> on the server, you can SSH into it and write to /srv directory.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">cap deploy:setup
cap deploy</pre></div></div>




<p>Deploy should spit a lot of text into the console, and there should be no errors. Verify that our unicorns are launched correctly by logging into the server and running this:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> sincapun
myuser   <span style="color: #000000;">24851</span>  <span style="color: #000000;">2.0</span>  <span style="color: #000000;">0.1</span>  <span style="color: #000000;">88480</span> <span style="color: #000000;">21024</span> ?        Sl   <span style="color: #000000;">11</span>:<span style="color: #000000;">42</span>   <span style="color: #000000;">0</span>:00 unicorn master <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>sincapun<span style="color: #000000; font-weight: bold;">/</span>current<span style="color: #000000; font-weight: bold;">/</span>config<span style="color: #000000; font-weight: bold;">/</span>unicorn.rb <span style="color: #660033;">-E</span> production <span style="color: #660033;">-D</span>                                         
myuser   <span style="color: #000000;">24854</span>  <span style="color: #000000;">0.1</span>  <span style="color: #000000;">0.1</span>  <span style="color: #000000;">88480</span> <span style="color: #000000;">19732</span> ?        Sl   <span style="color: #000000;">11</span>:<span style="color: #000000;">42</span>   <span style="color: #000000;">0</span>:00 unicorn worker<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>sincapun<span style="color: #000000; font-weight: bold;">/</span>current<span style="color: #000000; font-weight: bold;">/</span>config<span style="color: #000000; font-weight: bold;">/</span>unicorn.rb <span style="color: #660033;">-E</span> production <span style="color: #660033;">-D</span>                                      
myuser   <span style="color: #000000;">24857</span>  <span style="color: #000000;">0.1</span>  <span style="color: #000000;">0.1</span>  <span style="color: #000000;">88480</span> <span style="color: #000000;">19732</span> ?        Sl   <span style="color: #000000;">11</span>:<span style="color: #000000;">42</span>   <span style="color: #000000;">0</span>:00 unicorn worker<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>sincapun<span style="color: #000000; font-weight: bold;">/</span>current<span style="color: #000000; font-weight: bold;">/</span>config<span style="color: #000000; font-weight: bold;">/</span>unicorn.rb <span style="color: #660033;">-E</span> production <span style="color: #660033;">-D</span></pre></div></div>




<p>To access these unicorns from the internet, you need to put a reverse proxy in front of them. But that is another story.</p>

<p>You can get a full copy of this code <a href="https://github.com/stulentsev/sincapun">from Github repo</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=R-VPrLWtths:qhX7XFxXzoU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=R-VPrLWtths:qhX7XFxXzoU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=R-VPrLWtths:qhX7XFxXzoU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=R-VPrLWtths:qhX7XFxXzoU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/R-VPrLWtths" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/03/deploying-with-sinatra-capistrano-unicorn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/03/deploying-with-sinatra-capistrano-unicorn/</feedburner:origLink></item>
		<item>
		<title>How to keep your system clock synchronized on Ubuntu?</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/rDBPxGuDoyY/</link>
		<comments>http://software.tulentsev.com/2012/03/how-to-keep-your-system-clock-synchronized-on-ubuntu/#comments</comments>
		<pubDate>Fri, 09 Mar 2012 04:10:18 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=309</guid>
		<description><![CDATA[Your server&#8217;s hardware clock isn&#8217;t perfectly accurate. It may run faster or slower (in my experience it was always slower). So it is important to synchronize it every so often, or else you might encounter some unexpected bugs. There&#8217;s a command in Ubuntu that synchronizes hardware clock against atomic clock servers. It&#8217;s called ntpdate $ [...]]]></description>
			<content:encoded><![CDATA[<p>Your server&#8217;s hardware clock isn&#8217;t perfectly accurate. It may run faster or slower (in my experience it was always slower). So it is important to synchronize it every so often, or else you might encounter some unexpected bugs. There&#8217;s a command in Ubuntu that synchronizes hardware clock against atomic clock servers. It&#8217;s called ntpdate</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> ntpdate ntp.ubuntu.com
 <span style="color: #000000;">9</span> Mar 03:<span style="color: #000000;">59</span>:<span style="color: #000000;">12</span> ntpdate<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">7225</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: step <span style="color: #000000; font-weight: bold;">time</span> server 91.189.94.4 offset <span style="color: #000000;">179.440440</span> sec</pre></div></div>




<p>This one was three minutes behind. Could be worse, though. So, now clock is more or less accurate. To keep it this way, let&#8217;s add an hourly cron job. Create a file called &#8216;ntpdate&#8217; (for example) at &#8216;/etc/cron.hourly&#8217; with this content:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/sh</span>
&nbsp;
ntpdate ntp.ubuntu.com</pre></div></div>




<p>We don&#8217;t need sudo here, because these jobs are run with root privileges. Now make that file executable.</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>cron.hourly<span style="color: #000000; font-weight: bold;">/</span>ntpdate</pre></div></div>




<p>We&#8217;re all set now. Come back a few days later and verify that clock doesn&#8217;t deviate as much anymore.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=rDBPxGuDoyY:nXfm-6dHebw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=rDBPxGuDoyY:nXfm-6dHebw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=rDBPxGuDoyY:nXfm-6dHebw:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=rDBPxGuDoyY:nXfm-6dHebw:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/rDBPxGuDoyY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/03/how-to-keep-your-system-clock-synchronized-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/03/how-to-keep-your-system-clock-synchronized-on-ubuntu/</feedburner:origLink></item>
		<item>
		<title>Ruby: how to override class method with a module</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/YUPrwi5lqyw/</link>
		<comments>http://software.tulentsev.com/2012/02/ruby-how-to-override-class-method-with-a-module/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 13:09:00 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=298</guid>
		<description><![CDATA[This seems to be a popular interview question. It indeed requires advanced knowledge of ruby. You have a class with a class method. Write a module that, when included, will override that class method. Explanation of the problem Now classic way of mixing in class methods is this (and it doesn&#8217;t solve the problem, of [...]]]></description>
			<content:encoded><![CDATA[<p>This seems to be a popular interview question. It indeed requires advanced knowledge of ruby.</p>

<blockquote>
  <p>You have a class with a class method. Write a module that, when included, will override that class method.</p>
</blockquote>

<h3>Explanation of the problem</h3>

<p>Now classic way of mixing in class methods is this (and it doesn&#8217;t solve the problem, of course).</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> FooModule
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span> base
    base.<span style="color:#9900CC;">extend</span> ClassMethods
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#9966CC; font-weight:bold;">def</span> bar
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;module&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Klass
  <span style="color:#9966CC; font-weight:bold;">include</span> FooModule
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bar</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'class'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
Klass.<span style="color:#9900CC;">bar</span> <span style="color:#008000; font-style:italic;">#=&gt; class</span></pre></div></div>




<p>When modules are included or extended into a class, its methods are placed right above this class&#8217; methods in inheritance chain. This means that if we were to call <code>super</code> in that class method, it would print &#8220;module&#8221;. But we don&#8217;t want to touch original class definition, we want to alter it from outside.</p>

<h3>So, can we do something?</h3>

<p>Good for us, ruby has a concept of <a href="http://www.google.ru/search?ix=hca&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=ruby+open+classes">&#8220;open classes&#8221;</a>. This means that we can change virtually everything in the app, even some 3rd-party libraries. Every class can &#8220;opened&#8221; and new methods can be added to it, or old methods can be redefined. Let&#8217;s look how it works.<span id="more-298"></span></p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Klass
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bar</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'class'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Klass
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bar</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'class 2'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Klass.<span style="color:#9900CC;">bar</span> <span style="color:#008000; font-style:italic;">#=&gt; class 2</span></pre></div></div>




<p>The second class definition does not overwrite previous one, it opens and alters it. In this case, it happened to define a method with the same name. This resulted in old method being overwritten by the new one. This works with any classes, even base library classes.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#008000; font-style:italic;">#=&gt; [1, 2, 3]</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#996600;">&quot;an array: #{join ', '}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#008000; font-style:italic;">#=&gt; an array: 1, 2, 3</span></pre></div></div>




<p>Or the same code can be rewritten as</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#008000; font-style:italic;">#=&gt; [1, 2, 3]</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_s
    <span style="color:#996600;">&quot;an array: #{join ', '}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#008000; font-style:italic;">#=&gt; an array: 1, 2, 3</span></pre></div></div>




<h3>Applying the knowledge</h3>

<p>Let&#8217;s start with simpler things, like overriding an instance method.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Klass
  <span style="color:#9966CC; font-weight:bold;">def</span> say
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'class'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> FooModule
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span> base
    base.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> say
        <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;module&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
Klass.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#9966CC; font-weight:bold;">include</span>, FooModule<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
Klass.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">say</span> <span style="color:#008000; font-style:italic;">#=&gt; module</span></pre></div></div>




<p>Modules have a special callback that gets called every time a module is included in a class. We can use that to call class_eval on that class and redefine a method.</p>

<p>Replacing a class method is done in a similar way.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Klass
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">say</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'class'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> FooModule
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span> base
    base.<span style="color:#9900CC;">instance_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> say
        <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;module&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
Klass.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#9966CC; font-weight:bold;">include</span>, FooModule<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
Klass.<span style="color:#9900CC;">say</span> <span style="color:#008000; font-style:italic;">#=&gt; module</span></pre></div></div>




<p>The only difference here is that we call instance_eval instead of class_eval. This can be a very confusing part. In short, class_eval creates instance methods and instance_eval creates class methods.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=YUPrwi5lqyw:LaMjuUz13_A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=YUPrwi5lqyw:LaMjuUz13_A:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=YUPrwi5lqyw:LaMjuUz13_A:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=YUPrwi5lqyw:LaMjuUz13_A:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/YUPrwi5lqyw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/02/ruby-how-to-override-class-method-with-a-module/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/02/ruby-how-to-override-class-method-with-a-module/</feedburner:origLink></item>
		<item>
		<title>Mongoid, db.system.namespaces queries</title>
		<link>http://feedproxy.google.com/~r/ThoughtsOnSoftwareAndComputers/~3/bw0gCXeicDE/</link>
		<comments>http://software.tulentsev.com/2012/02/mongoid-db-system-namespaces-queries/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 12:12:54 +0000</pubDate>
		<dc:creator>Sergio Tulentsev</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MongoDb]]></category>
		<category><![CDATA[MongoId]]></category>

		<guid isPermaLink="false">http://software.tulentsev.com/?p=295</guid>
		<description><![CDATA[Recently I faced some issues with Mongoid when upgrading my Rails app from REE+Passenger to MRI 1.9.3+Unicorn. There are some Resque workers in the background. After some deploy they started to consume a ton of traffic from MongoDB. After some investigation, I found that they heavily read system.namespaces collection. I tried upgrading to latest versions [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I faced some issues with <a href="http://mongoid.org">Mongoid</a> when upgrading my Rails app from REE+Passenger to MRI 1.9.3+Unicorn.</p>

<p>There are some Resque workers in the background. After some deploy they started to consume a ton of traffic from MongoDB. After some investigation, I found that they heavily read <code>system.namespaces</code> collection. I tried upgrading to latest versions of <code>mongoid</code>(2.4.3) and <code>mongo</code>(1.5.2) to no avail. This does not happen with normal unicorn workers. This also does not happen if I downgrade <code>mongoid</code> to 2.0.1.</p>

<p>I am still not sure what&#8217;s happening here. I&#8217;ll update this post when I discover something.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=bw0gCXeicDE:hXTjn1F5VbQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=bw0gCXeicDE:hXTjn1F5VbQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?i=bw0gCXeicDE:hXTjn1F5VbQ:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?a=bw0gCXeicDE:hXTjn1F5VbQ:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/ThoughtsOnSoftwareAndComputers?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/ThoughtsOnSoftwareAndComputers/~4/bw0gCXeicDE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://software.tulentsev.com/2012/02/mongoid-db-system-namespaces-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://software.tulentsev.com/2012/02/mongoid-db-system-namespaces-queries/</feedburner:origLink></item>
	</channel>
</rss>

