<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://christopherirish.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://christopherirish.com/" rel="alternate" type="text/html" /><updated>2026-07-13T18:38:59+00:00</updated><id>https://christopherirish.com/feed.xml</id><title type="html">Christopher Irish</title><subtitle>Personal blog of Christopher Irish — software development, technology, and more.</subtitle><author><name>Christopher Irish</name></author><entry><title type="html">RailsConf RubyAZ Presentation Slides</title><link href="https://christopherirish.com/2012/05/16/railsconf-rubyaz-presentation-slides/" rel="alternate" type="text/html" title="RailsConf RubyAZ Presentation Slides" /><published>2012-05-16T00:00:39+00:00</published><updated>2012-05-16T00:00:39+00:00</updated><id>https://christopherirish.com/2012/05/16/railsconf-rubyaz-presentation-slides</id><content type="html" xml:base="https://christopherirish.com/2012/05/16/railsconf-rubyaz-presentation-slides/"><![CDATA[<p>This past Tuesday evening I presented to the RubyAZ user group. The talk covered useful libraries and gems that I saw at Rails Conf 2012 that I thought others might find helpful in their day to day work. I’ve made the <a href="https://www.dropbox.com/s/4tsrlwgbgb045yg/RailsConfPreso.pdf">presentation slides</a> available as a pdf. Enjoy!</p>]]></content><author><name>Christopher Irish</name></author><category term="Ruby on Rails" /><category term="Ruby" /><category term="Tools" /><summary type="html"><![CDATA[This past Tuesday evening I presented to the RubyAZ user group. The talk covered useful libraries and gems that I saw at Rails Conf 2012 that I thought others might find helpful in their day to day work. I’ve made the presentation slides available as a pdf. Enjoy!]]></summary></entry><entry><title type="html">How to set the timezone on Ubuntu Server</title><link href="https://christopherirish.com/2012/03/21/how-to-set-the-timezone-on-ubuntu-server/" rel="alternate" type="text/html" title="How to set the timezone on Ubuntu Server" /><published>2012-03-21T17:28:56+00:00</published><updated>2012-03-21T17:28:56+00:00</updated><id>https://christopherirish.com/2012/03/21/how-to-set-the-timezone-on-ubuntu-server</id><content type="html" xml:base="https://christopherirish.com/2012/03/21/how-to-set-the-timezone-on-ubuntu-server/"><![CDATA[<p>I find it’s pretty annoying when you have to go log file spelunking only to find all the timestamps are in UTC.  But we can set the timezone of the server so when Rails, cron, scripts, etc run, they output more readable dates.</p>

<p>You can check your current timezone by just running</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="go">date
Thu Mar 21 18:02:49 MST 2012
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Or checking the timezone file at</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="go">more /etc/timezone
US/Arizona
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>So to change it just run</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="go">sudo dpkg-reconfigure tzdata
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>And follow on screen instructions.  Easy.</p>

<p>Also be sure to restart cron as it won’t pick up the timezone change and will still be running on UTC.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="go">/etc/init.d/cron stop
/etc/init.d/cron start
</span></pre></td></tr></tbody></table></code></pre></div></div>]]></content><author><name>Christopher Irish</name></author><summary type="html"><![CDATA[I find it’s pretty annoying when you have to go log file spelunking only to find all the timestamps are in UTC.  But we can set the timezone of the server so when Rails, cron, scripts, etc run, they output more readable dates.]]></summary></entry><entry><title type="html">Can’t get SSH Public Keys working?</title><link href="https://christopherirish.com/2012/01/27/cant-get-ssh-public-key-working/" rel="alternate" type="text/html" title="Can’t get SSH Public Keys working?" /><published>2012-01-27T17:23:40+00:00</published><updated>2012-01-27T17:23:40+00:00</updated><id>https://christopherirish.com/2012/01/27/cant-get-ssh-public-key-working</id><content type="html" xml:base="https://christopherirish.com/2012/01/27/cant-get-ssh-public-key-working/"><![CDATA[<p>Having problems getting ssh to use your public key?</p>

<p>Log on to your server as root using a password. Remember not to log out as root until you have ssh working with your public key or you may inadvertently lock yourself off the box.. which sucks ;) Double check your sshd_config, I use the following options (note you will have to restart ssh for these changes to take effect):</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre>Port 30000    &lt;--- change to a port of your choosing
Protocol 2
PermitRootLogin no
PasswordAuthentication no
UseDNS no
AllowUsers deploy   &lt;--- change to whatever users you want
ClientAliveInterval 30
ClientAliveCountMax 4
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Make sure your public key is in the .ssh/authorized_keys file for the user you plan on logging onto the server as. You can use this super helpful <a href="http://www.christopherirish.com/2010/04/19/the-authme-ssh-key-bash-function/" title="authme bash function">authme bash function</a>.</p>

<p>So let’s say you’ve done all this and YOU STILL CAN’T get it to work. It’s probably because you have incorrect permissions set on the .ssh directory and/or the authorized_keys file. You can check this by looking at the ssh auth log. Depending on your server OS it’s usually either /var/log/auth.log or /var/log/secure . Tail this file and see if you’re getting this message</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>Authentication refused: bad ownership or modes for directory /home/deploy/.ssh
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Obviously your directory will be different depending on the name of the user you’re trying to ssh on as. But this message is the give away that your permissions are wrong. Change them with the following, assuming your username is “deploy”, otherwise just substitute that part.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="go">chmod go-w /home/deploy/
chmod 700  /home/deploy/.ssh
chmod 600  /home/deploy/.ssh/authorized_keys
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Try ssh’ing on again in another terminal window and you should be good. Now it would be safe to log off as root.</p>]]></content><author><name>Christopher Irish</name></author><summary type="html"><![CDATA[Having problems getting ssh to use your public key?]]></summary></entry><entry><title type="html">No view_context, in views, in Rails 3.1 changes</title><link href="https://christopherirish.com/2011/10/13/no-view_context-in-rails-3-1-changes/" rel="alternate" type="text/html" title="No view_context, in views, in Rails 3.1 changes" /><published>2011-10-13T22:35:11+00:00</published><updated>2011-10-13T22:35:11+00:00</updated><id>https://christopherirish.com/2011/10/13/no-view_context-in-rails-3-1-changes</id><content type="html" xml:base="https://christopherirish.com/2011/10/13/no-view_context-in-rails-3-1-changes/"><![CDATA[<p>Got burned by two things today in one of my apps by upgrading to Rails 3.1. The first thing was a few nested forms stopped working when editing an existing record that had child records. None of the children were getting rendered. For briefity, the form basically was like:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="o">&lt;</span><span class="sx">%= form_for @meal do |meal_form| %&gt;
  &lt;table&gt;
    &lt;tbody&gt;
      &lt;% meal_form.fields_for :meal_items do |item_form| %&gt;
        &lt;%=</span> <span class="n">render</span> <span class="ss">:partial</span> <span class="o">=&gt;</span> <span class="s1">'meal_item_fields'</span><span class="p">,</span>
                   <span class="ss">:locals</span> <span class="o">=&gt;</span> <span class="p">{</span> <span class="ss">:f</span> <span class="o">=&gt;</span> <span class="n">item_form</span> <span class="p">}</span> <span class="o">%&gt;</span>
      <span class="o">&lt;</span><span class="sx">% end </span><span class="o">%&gt;</span>
    <span class="o">&lt;</span><span class="sr">/tbody&gt;
  &lt;/</span><span class="n">table</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="sx">% end </span><span class="o">%&gt;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Can you spot the error? Once I spotted it I felt rather dumb myself. It’s the following erb tag</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="o">&lt;</span><span class="sx">% meal_form.fields_for </span><span class="ss">:meal_items</span> <span class="k">do</span> <span class="o">|</span><span class="n">item_form</span><span class="o">|</span> <span class="sx">%&gt;
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Ya even when they’re nested you HAVE to use the &lt;%= form… doh!</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="o">&lt;</span><span class="sx">%= meal_form.fields_for :meal_items do |item_form| %&gt;
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>The second problem I ran into, and which I still don’t have an answer for, though if you do answer my <a href="http://stackoverflow.com/questions/7761557/view-context-not-longer-available-in-rails-3-views">StackOverflow question</a>. Basically, in Rails 2.3 if you needed to get at an instance of the Template Builder you could use @template, but then they changed it in Rails 3 to a special method called “view_context”. Far enough, so I was using view_context in Rails 3.0.X to create nested forms (other than the one above) dynamically using jQuery ajax and js.erb partials. Using view_context allowed me to call fields_for off of it so that I would get nicely named form_fields when I didn’t have the parent FormBuilder handy. Like this</p>

<p>In some_view.js.erb</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="o">&lt;</span><span class="sx">% fields </span><span class="o">=</span>
    <span class="n">view_context</span><span class="p">.</span><span class="nf">fields_for</span> <span class="ss">:meal_items</span><span class="p">,</span> <span class="no">Meal</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">meal_items</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
      <span class="n">render</span> <span class="ss">:partial</span> <span class="o">=&gt;</span> <span class="s1">'meal_item_fields'</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:f</span> <span class="o">=&gt;</span> <span class="n">f</span><span class="p">}</span>
    <span class="k">end</span> <span class="sx">%&gt;

$("#meal-items").append("&lt;%= escape_javascript(fields) %&gt;</span><span class="s2">");
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>But once I upgrade to 3.1, I started getting the following error</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>ActionView::Template::Error (undefined local variable or method `view_context' for #):
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Perhaps this was removed as the Core team figured, why do you need the view_context available when you’re in a view template already… but I don’t know. I was able to workaround it though by moving my code up into the controller. Which does seem to be a violation of the MVC separation, but I couldn’t find any other way to make it work like pre Rails upgrade. So instead of creating a local variable “fields”, this became an instance variable that was set in the controller action. And render needed to become render_to_string with an html_safe call tacked on the end.</p>

<p>In controller</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">some_ajax_action</span>
  <span class="vi">@fields</span> <span class="o">=</span>
    <span class="n">view_context</span><span class="p">.</span><span class="nf">fields_for</span> <span class="ss">:meal_items</span><span class="p">,</span> <span class="no">Meal</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">meal_items</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
      <span class="n">render_to_string</span><span class="p">(</span><span class="ss">:partial</span> <span class="o">=&gt;</span> <span class="s1">'fields'</span><span class="p">,</span> <span class="ss">:locals</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:f</span> <span class="o">=&gt;</span> <span class="n">f</span><span class="p">}).</span><span class="nf">html_safe</span>
    <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>I don’t like this, but it’ll do for now I suppose. And now in some_view.js.erb</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="err">$</span><span class="p">(</span><span class="s2">"#meal-items"</span><span class="p">).</span><span class="nf">append</span><span class="p">(</span><span class="s2">"&lt;%= escape_javascript(@fields) %&gt;"</span><span class="p">);</span>
</pre></td></tr></tbody></table></code></pre></div></div>]]></content><author><name>Christopher Irish</name></author><category term="Ruby on Rails" /><category term="Development" /><summary type="html"><![CDATA[Got burned by two things today in one of my apps by upgrading to Rails 3.1. The first thing was a few nested forms stopped working when editing an existing record that had child records. None of the children were getting rendered. For briefity, the form basically was like:]]></summary></entry><entry><title type="html">Ruby 1.9.2 Segmentation Fault and OpenSSL</title><link href="https://christopherirish.com/2011/09/02/ruby-1-9-2-segmentation-fault-and-openssl/" rel="alternate" type="text/html" title="Ruby 1.9.2 Segmentation Fault and OpenSSL" /><published>2011-09-02T15:20:44+00:00</published><updated>2011-09-02T15:20:44+00:00</updated><id>https://christopherirish.com/2011/09/02/ruby-1-9-2-segmentation-fault-and-openssl</id><content type="html" xml:base="https://christopherirish.com/2011/09/02/ruby-1-9-2-segmentation-fault-and-openssl/"><![CDATA[<p>In one of my current project, whenever I ran a rake task that did a net/http request it was causing segmentation faults.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="go">bundle exec rake test:task
/Users/cirish/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:678: [BUG] Segmentation fault
ruby 1.9.2 (2011-06-30 patchlevel 290) [i686-darwin10.8.0]
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>And it seems whenever I get a segmentation fault the first place I need to look is at OpenSSL. I’ve had similar problems with seg faults and openssl in the past, namely this <a href="http://www.christopherirish.com/2010/06/21/ruby-1-8-7-openssl-bus-error/">OpenSSL Bus Error</a> that I was getting on Ruby 1.8.7.</p>

<p>If you read that post you’ll see this happened because I use MacPorts and have OpenSSL installed through it, in addition to a local OS X version. To get Ruby to be happy, you have to point your Ruby install to the correct version at installation time. So remove the bad install</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="go">rvm remove ruby-1.9.2
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>And reinstall</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="gp">rvm install ruby-1.9.2 --with-openssl-dir=/opt/local --with-iconv-dir=$</span>rvm_path/usr
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Which I forgot to do with my Ruby 1.9.2 install after upgrading OS X versions. That last bit about iconv isn’t directly related, but I have inconv package installed to RVM, you can read about that here <a href="http://beginrescueend.com/packages/iconv/">RVM Iconv</a>.</p>

<p>Also, this Phusion blog post <a href="http://blog.phusion.nl/2011/02/24/ruby-enterprise-edition-1-8-7-2011-03-released/">REE 1-8-7-2011-03-released</a> mentions problems with OpenSSL and MacPorts. Moral of the story… use HomeBrew? Dunno, but I’ll probably start using it when I upgrade my laptop.</p>]]></content><author><name>Christopher Irish</name></author><summary type="html"><![CDATA[In one of my current project, whenever I ran a rake task that did a net/http request it was causing segmentation faults.]]></summary></entry><entry><title type="html">Rails 3 Authlogic sessions not persisting in production</title><link href="https://christopherirish.com/2011/08/12/rails-3-authlogic-sessions-not-persisting-in-production/" rel="alternate" type="text/html" title="Rails 3 Authlogic sessions not persisting in production" /><published>2011-08-12T12:16:43+00:00</published><updated>2011-08-12T12:16:43+00:00</updated><id>https://christopherirish.com/2011/08/12/rails-3-authlogic-sessions-not-persisting-in-production</id><content type="html" xml:base="https://christopherirish.com/2011/08/12/rails-3-authlogic-sessions-not-persisting-in-production/"><![CDATA[<p>I just ran into a rather hard to track down issue with Authlogic and Rails 3. After upgrading an old app to Rails 3, in production, the sessions wouldn’t persist, but everything worked fine in development mode. After much searching it turns out that there’s an issue with using Rails 3, Authlogic, and HTTP auth together. I had put up HTTP basic auth in Nginx to prevent the site from getting seen by random people or web crawlers. When the app was a Rails 2 app, there was no issue, in fact the Rails app shouldn’t even have known about the HTTP auth since I was doing it in Nginx, not Rails. Not sure why this is a problem with this Rails/Authlogic/HTTP combination, but anyways, the fix was to add this one-liner to your UserSession model</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">UserSession</span> <span class="o">&lt;</span> <span class="no">Authlogic</span><span class="o">::</span><span class="no">Session</span><span class="o">::</span><span class="no">Base</span>
  <span class="n">allow_http_basic_auth</span> <span class="kp">false</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>]]></content><author><name>Christopher Irish</name></author><summary type="html"><![CDATA[I just ran into a rather hard to track down issue with Authlogic and Rails 3. After upgrading an old app to Rails 3, in production, the sessions wouldn’t persist, but everything worked fine in development mode. After much searching it turns out that there’s an issue with using Rails 3, Authlogic, and HTTP auth together. I had put up HTTP basic auth in Nginx to prevent the site from getting seen by random people or web crawlers. When the app was a Rails 2 app, there was no issue, in fact the Rails app shouldn’t even have known about the HTTP auth since I was doing it in Nginx, not Rails. Not sure why this is a problem with this Rails/Authlogic/HTTP combination, but anyways, the fix was to add this one-liner to your UserSession model]]></summary></entry><entry><title type="html">Splitting Rails DOM ids with RegEx Look Aheads</title><link href="https://christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/" rel="alternate" type="text/html" title="Splitting Rails DOM ids with RegEx Look Aheads" /><published>2010-10-15T01:27:02+00:00</published><updated>2010-10-15T01:27:02+00:00</updated><id>https://christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads</id><content type="html" xml:base="https://christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/"><![CDATA[<p>In my Rails erb views I make good use of the dom_id view helper method. It basically introspects the object you pass to it and creates a unique id to use in your markup, based on the object type and database id. So for example if I had a Post with id 1, I could do:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="o">&lt;</span><span class="n">li</span> <span class="nb">id</span><span class="o">=</span><span class="s2">"&lt;%= dom_id(@post) %&gt;"</span><span class="o">&gt;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And it would be rendered as</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="o">&lt;</span><span class="n">li</span> <span class="nb">id</span><span class="o">=</span><span class="s2">"post_1"</span><span class="o">&gt;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This is fine until you use dom_id on an object with a compound name like EmailNotification. Now dom_id will create a unique id like</p>

<p>email_notification_1</p>

<p>Which is still good until I’m doing some front-end testing and I’m parsing these dom ids to find their related db records. Now I need to split “email_notification_1” in a way that easily separates the object class from the identifier. How would you do that?</p>

<p>Well, what we can do here is use an advanced Regular Expression feature called a negative look ahead. We want to say, “split the string on the last underscore”.</p>

<p>A negative look ahead uses the ?! syntax in parenthesis, everything that follows ?! in the parenthesis will be used in the negative look ahead matching. So in our case we want to say, there should be a underscore that can be followed by any character except another underscore. The pattern would like like this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>/_(?!.*_)/
</pre></td></tr></tbody></table></code></pre></div></div>

<p>We can confirm in irb</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="gp">ree-1.8.7-head &gt;</span><span class="w"> </span>x <span class="o">=</span> <span class="s2">"email_notification_1234"</span>
<span class="gp"> =&gt;</span><span class="w"> </span><span class="s2">"email_notification_1234"</span>
<span class="gp">ree-1.8.7-head &gt;</span><span class="w"> </span>klass, <span class="nb">id</span> <span class="o">=</span> x.split<span class="o">(</span>/_<span class="o">(</span>?!.<span class="k">*</span>_<span class="o">)</span>/<span class="o">)</span>
<span class="gp"> =&gt;</span><span class="w"> </span><span class="o">[</span><span class="s2">"email_notification"</span>, <span class="s2">"1234"</span><span class="o">]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Likewise, If we wanted to split the dom_id on the first underscore we could just use a regular look ahead. It is similar but uses the ?= syntax.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="gp">ree-1.8.7-head &gt;</span><span class="w"> </span>x.split<span class="o">(</span>/_<span class="o">(</span>?<span class="o">=</span>.<span class="k">*</span>_<span class="o">)</span>/<span class="o">)</span>
<span class="gp"> =&gt;</span><span class="w"> </span><span class="o">[</span><span class="s2">"email"</span>, <span class="s2">"notification_1234"</span><span class="o">]</span>
</pre></td></tr></tbody></table></code></pre></div></div>]]></content><author><name>Christopher Irish</name></author><category term="Development" /><category term="Ruby on Rails" /><category term="Regular Expressions" /><summary type="html"><![CDATA[In my Rails erb views I make good use of the dom_id view helper method. It basically introspects the object you pass to it and creates a unique id to use in your markup, based on the object type and database id. So for example if I had a Post with id 1, I could do:]]></summary></entry><entry><title type="html">How to install RVM on Ubuntu 10.04</title><link href="https://christopherirish.com/2010/08/25/how-to-install-rvm-on-ubuntu-10-04/" rel="alternate" type="text/html" title="How to install RVM on Ubuntu 10.04" /><published>2010-08-25T01:21:09+00:00</published><updated>2010-08-25T01:21:09+00:00</updated><id>https://christopherirish.com/2010/08/25/how-to-install-rvm-on-ubuntu-10-04</id><content type="html" xml:base="https://christopherirish.com/2010/08/25/how-to-install-rvm-on-ubuntu-10-04/"><![CDATA[<p>I’ve heard people describe frustrations they had in getting Rails 3 setup on Ubuntu 10.04 with Ruby 1.9.2. I’m going to outline the steps I’ve taken to do this. Let’s get started installing RVM.</p>

<p>It should be noted that I created a fresh slice on my <a href="http://www.rackspacecloud.com/1655-0-3-13.html">Rackspace Cloud</a> account to create this tutorial. And I’m assuming you’ve already setup SSH.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="go">ssh root@173.XXX.XXX.XXX
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">root@173.XXX.XXX.XXX's password:
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Alright, so lets confirm we don’t have ruby installed</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="go">which ruby
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Cool, nada. Let’s grab the basic dependencies</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="go">sudo apt-get install curl git-core ruby
Reading package lists... Done
Building dependency tree
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Now we’ll install RVM as per their instructions<br />
http://rvm.beginrescueend.com/rvm/install/</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="rouge-code"><pre><span class="go">bash -s stable &lt; &lt;(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">
RVM:   shell scripts which allow management of multiple ruby interpreters and environments.
RTFM:  http://rvm.beginrescueend.com/
</span><span class="gp">HELP:  http://webchat.freenode.net/?channels=rvm (#</span>rvm on irc.freenode.net<span class="o">)</span>
<span class="go">
* Installing rvm to /usr/local/rvm/
    Correct permissions for base binaries in /usr/local/bin...
    Copying manpages into place.

Notes for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04 LTS" )

</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">
    *  For JRuby (if you wish to use it) you will need:
</span><span class="gp">       $</span><span class="w"> </span>aptitude <span class="nb">install </span>curl sun-java6-bin sun-java6-jre sun-java6-jdk
<span class="go">    *  For MRI &amp; ree (if you wish to use it) you will need (depending on what you are installing):
</span><span class="gp">       $</span><span class="w"> </span>aptitude <span class="nb">install </span>build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf
<span class="go">    *  For IronRuby (if you wish to use it) you will need:
</span><span class="gp">       $</span><span class="w"> </span>aptitude <span class="nb">install </span>curl mono-2.0-devel
<span class="go">
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">
WARNING:  you have a 'return' statement in your .bashrc, likely this will cause untold havoc.
</span><span class="gp">   This means that if you see '[ -z "$</span>PS1<span class="s2">" ] &amp;&amp; return' then you must change this line to:
</span><span class="gp">   if [[ -n "$</span><span class="s2">PS1"</span> <span class="o">]]</span> <span class="p">;</span> <span class="k">then</span>
<span class="go">     ... original content that was below the &amp;&amp; return line ...
</span><span class="gp">   fi #</span><span class="w"> </span>&lt;<span class="o">=</span> be sure to close the <span class="k">if</span><span class="nb">.</span>
<span class="gp">   #</span>EOF .bashrc
<span class="go">Even if you use zsh you should still adjust the .bashrc as above.
</span><span class="gp">If you have any questions about this please visit #</span>rvm on irc.freenode.net.
<span class="go">
Installation of RVM to /usr/local/rvm/ is complete.
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>The last part in there about the .bashrc file is important! Using your editor of choice, open the .bashrc file that resides in your user directory.</p>

<p>You need to replace the line that says</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>[ -z "$PS1" ] &amp;&amp; return
</pre></td></tr></tbody></table></code></pre></div></div>

<p>with</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>if [[ -n "$PS1" ]]; then
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Now add this to the last line of the file</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre>if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi

fi
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And yes there needs to be that last fi as it closes the one we added earlier. Save this file with our changes. Now we can check if RVM is setup correctly.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="go">rvm notes
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>If this doesn’t work you may have to source your .bashrc file to have it reloaded by the current terminal session</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="go">source ~/.bashrc
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>After running <code class="language-plaintext highlighter-rouge">rvm notes</code> if it gives you installation notes about RVM then you’re good to continue, otherwise double check you edited your .bashrc file correctly.</p>

<p>In the notes output RVM tells you what packages you’re gonna need to install for various flavors of Ruby. Since we’re going with 1.9.2 we want the packages it lists under the MRI &amp; ree section. Let’s install those now.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="go">sudo aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf

</span><span class="gp">&lt;-- A really long bit of Output Snipped --&gt;</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Now we can look at all the RVM known Ruby packages</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="rouge-code"><pre><span class="go">rvm list known
</span><span class="gp">#</span><span class="w"> </span>MRI Rubies
<span class="go">(ruby-)1.8.6(-p399)
(ruby-)1.8.6-head
(ruby-)1.8.7(-p302)
(ruby-)1.8.7-head
(ruby-)1.9.1-p243
(ruby-)1.9.1-p376
(ruby-)1.9.1(-p429)
(ruby-)1.9.1-head
(ruby-)1.9.2-preview1
(ruby-)1.9.2-preview3
(ruby-)1.9.2-rc1
(ruby-)1.9.2-rc2
(ruby-)1.9.2(-p0)
(ruby-)1.9.2-head
ruby-head

</span><span class="gp">#</span><span class="w"> </span>JRuby
<span class="go">jruby-1.2.0
jruby-1.3.1
jruby-1.4.0
jruby-1.5.1
jruby(-1.5.2)
jruby-head

</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Next we install the version we want and set it as the default. You’re gonna see RVM install ruby-1.8.7-p302 first, be patient as it will need to also install rubygems-1.3.7, before it finally gets to that 1.9.2.</p>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="go">rvm install 1.9.2-head
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">info: Installing ruby-1.8.7-p302
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">info: Installing rubygems dedicated to ruby-1.8.7-p302...
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">info: Installing ruby-1.9.2-head
</span><span class="gp">&lt;-- Output Snipped --&gt;</span><span class="w">
</span><span class="go">info: Updating rubygems for /usr/local/rvm/gems/ruby-1.9.2-head
</span></pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-terminal highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="go">rvm --default 1.9.2-head
ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29034) [x86_64-linux]
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Good to go.</p>]]></content><author><name>Christopher Irish</name></author><category term="Installation" /><category term="Ruby" /><category term="RVM" /><category term="Ubuntu" /><summary type="html"><![CDATA[I’ve heard people describe frustrations they had in getting Rails 3 setup on Ubuntu 10.04 with Ruby 1.9.2. I’m going to outline the steps I’ve taken to do this. Let’s get started installing RVM.]]></summary></entry><entry><title type="html">How to get non-model data in ActiveRecord Callbacks</title><link href="https://christopherirish.com/2010/08/16/how-to-get-non-model-data-in-activerecord-callbacks/" rel="alternate" type="text/html" title="How to get non-model data in ActiveRecord Callbacks" /><published>2010-08-16T01:34:54+00:00</published><updated>2010-08-16T01:34:54+00:00</updated><id>https://christopherirish.com/2010/08/16/how-to-get-non-model-data-in-activerecord-callbacks</id><content type="html" xml:base="https://christopherirish.com/2010/08/16/how-to-get-non-model-data-in-activerecord-callbacks/"><![CDATA[<p>On a project I’m currently working on, we use ActiveRecord callbacks heavily. From simplying creating other associated objects, to recording the historic activity about the object in question, and more.</p>

<p>The drawback I find from the scope of AR callbacks though is that you only have direct access to data from the database. And since you can’t pass arguments to a AR callback, you can’t easily get the request params for example or know which controller action fired off the the AR chain that triggered the callback you’re in, or know who the current user logged in is.</p>

<p>But it turns out that there is a way, however it is neither straight forward or obvious. Say for example you have the following model and controller:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Post</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="n">belongs_to</span> <span class="ss">:user</span>
  <span class="n">belongs_to</span> <span class="ss">:group</span>
  <span class="n">has_one</span>    <span class="ss">:alert</span><span class="p">,</span> <span class="ss">:as</span> <span class="o">=&gt;</span> <span class="s1">'alertable'</span>

  <span class="n">after_update</span> <span class="ss">:note_activity</span>

  <span class="kp">private</span>
  <span class="k">def</span> <span class="nf">note_activity</span>
    <span class="n">group</span><span class="p">.</span><span class="nf">admins</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">admin</span><span class="o">|</span>
      <span class="c1"># What user is doing this update?</span>
      <span class="n">admin</span><span class="p">.</span><span class="nf">notifications</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="ss">:user</span> <span class="o">=&gt;</span> <span class="s2">"Someone"</span><span class="p">,</span>
                                 <span class="ss">:action</span> <span class="o">=&gt;</span> <span class="s1">'did something'</span><span class="p">)</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Admin::PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>

  <span class="k">def</span> <span class="nf">update</span>
    <span class="vi">@post</span> <span class="o">=</span> <span class="no">Post</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="ss">:id</span><span class="p">])</span>

    <span class="k">if</span> <span class="vi">@post</span><span class="p">.</span><span class="nf">update_attributes</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="ss">:post</span><span class="p">])</span>
      <span class="n">redirect_to</span> <span class="n">admin_posts_path</span>
    <span class="k">else</span>
      <span class="n">render</span> <span class="ss">:action</span> <span class="o">=&gt;</span> <span class="s1">'edit'</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>What we want to accomplish here is that all admins of a group get a notification when another admin user edits a post. The post in question here has no context in it’s callback of which admin was logged in at the time the update happened. So what we’re gonna do is put the admin user’s id onto the current thread of the request in a before filter. Then, we’ll pull that id off in the callback to look up the admin and create the notifications.</p>

<p>So we’ll add a simple before filter:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
  <span class="n">before_filter</span> <span class="ss">:store_user</span>

  <span class="k">def</span> <span class="nf">store_user</span>
    <span class="no">User</span><span class="p">.</span><span class="nf">current</span> <span class="o">=</span> <span class="n">current_user</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And create a couple class methods on User to store the currently logged in admin for the duration of the request:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>

  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">current</span><span class="o">=</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
    <span class="no">Thread</span><span class="p">.</span><span class="nf">current</span><span class="p">[</span><span class="s1">'user'</span><span class="p">]</span> <span class="o">=</span> <span class="n">user</span><span class="p">.</span><span class="nf">try</span><span class="p">(</span><span class="ss">:id</span><span class="p">)</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">current</span>
    <span class="vc">@@current</span> <span class="o">||=</span> <span class="no">User</span><span class="p">.</span><span class="nf">find_by_id</span><span class="p">(</span><span class="no">Thread</span><span class="p">.</span><span class="nf">current</span><span class="p">[</span><span class="s1">'user'</span><span class="p">])</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Now we can alter our note_activity method in Post to get the admin who did the edit:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">def</span> <span class="nf">note_activity</span>
  <span class="n">group</span><span class="p">.</span><span class="nf">admins</span><span class="p">.</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">admin</span><span class="o">|</span>
    <span class="n">admin</span><span class="p">.</span><span class="nf">notifications</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="ss">:user</span> <span class="o">=&gt;</span> <span class="no">User</span><span class="p">.</span><span class="nf">current</span><span class="p">,</span>
                               <span class="ss">:action</span> <span class="o">=&gt;</span> <span class="s1">'did something'</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This works well in my project. I’d like to know if there is a less “hacky” way of doing this but I’m not currently aware of one.</p>]]></content><author><name>Christopher Irish</name></author><category term="Development" /><category term="ActiveRecord" /><category term="Callbacks" /><category term="Thread" /><category term="Rails" /><summary type="html"><![CDATA[On a project I’m currently working on, we use ActiveRecord callbacks heavily. From simplying creating other associated objects, to recording the historic activity about the object in question, and more.]]></summary></entry><entry><title type="html">The Ruby %r{} expression</title><link href="https://christopherirish.com/2010/08/12/the-ruby-r-expression/" rel="alternate" type="text/html" title="The Ruby %r{} expression" /><published>2010-08-12T01:13:27+00:00</published><updated>2010-08-12T01:13:27+00:00</updated><id>https://christopherirish.com/2010/08/12/the-ruby-r-expression</id><content type="html" xml:base="https://christopherirish.com/2010/08/12/the-ruby-r-expression/"><![CDATA[<p>Tonight I was reading over some Ruby code in a gem I’m currently using and came across this line:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="n">string</span> <span class="o">=</span> <span class="n">mode</span><span class="p">.</span><span class="nf">to_s</span>
<span class="n">string</span><span class="p">.</span><span class="nf">gsub!</span><span class="p">(</span><span class="sr">%r{(^.)|(_.)}</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">m</span><span class="o">|</span> <span class="n">m</span><span class="p">[</span><span class="n">m</span><span class="p">.</span><span class="nf">length</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">].</span><span class="nf">upcase</span> <span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Two things struck me about that second line. For one I’ve never used gsub and passed it a block, thats cool. And two, what is %r{} ? I’ve never seen that before. It turns out that in Ruby, the /…/ or %r… literals, and using the Regexp.new constructor are all the same thing. All ways of created a regular expression. I still think I prefer the look of the simple slashes /…/ though.</p>]]></content><author><name>Christopher Irish</name></author><category term="Development" /><category term="Ruby" /><category term="Regular Expressions" /><summary type="html"><![CDATA[Tonight I was reading over some Ruby code in a gem I’m currently using and came across this line:]]></summary></entry></feed>