<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Live &#38; Code &#187; Programming</title>
	<atom:link href="http://www.liveandcode.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.liveandcode.com</link>
	<description>Enrico on programming, living, and everything in between</description>
	<lastBuildDate>Mon, 27 Jun 2011 01:10:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How PostRank Analytics Brought Me To Ruby Kaigi</title>
		<link>http://www.liveandcode.com/2010/11/13/how-postrank-analytics-brought-me-to-ruby-kaigi/</link>
		<comments>http://www.liveandcode.com/2010/11/13/how-postrank-analytics-brought-me-to-ruby-kaigi/#comments</comments>
		<pubDate>Sun, 14 Nov 2010 02:59:08 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PostRank]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby Kaigi]]></category>
		<category><![CDATA[social media]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=556</guid>
		<description><![CDATA[One of the coolest things about working for PostRank is that I am part of creating a product that I can be very passionate about because I am one of the sorts of people it&#8217;s made for. In short, I eat our dog food. So I wanted to dedicate today&#8217;s post to sharing a small [...]]]></description>
			<content:encoded><![CDATA[<p>One of the coolest things about working for PostRank is that I am part of creating a product that I can be very passionate about because I am one of the sorts of people it&#8217;s made for. In short, <a title="Our Team | PostRank Blog" href="http://blog.postrank.com/about/our-team/">I eat our dog food</a>. So I wanted to dedicate today&#8217;s post to sharing a small anecdote about one of the killer features of <a title="PostRank Analytics" href="http://analytics.postrank.com/">PostRank Analytics</a>, the <a title="PostRank Activity Streams: Friend Feed for your Content | PostRank Blog" href="http://blog.postrank.com/2010/05/postrank-activity-streams-friendfeed-for-your-content/">Activity Stream</a>.<span id="more-556"></span></p>
<p>It&#8217;s interesting that I&#8217;ve chosen to go the path of the Rubyist because it&#8217;s a path that combines two things that I love very much: Japan and programming. <a href="http://rubykaigi.org/2010/en">Ruby Kaigi</a> is a Ruby conference held in Japan, which I unfortunately missed this year. However, my blog didn&#8217;t, and I was made aware of it thanks to PostRank.</p>
<p><a title="LDAP Pass-through Authentication with Authlogic and ActiveLdap | PostRank Analytics" href="http://topblo.gs/bcQZCu">One of my most popular posts</a> is one I wrote about using Authlogic and ActiveLDAP to let a Rails app do pass-through authentication to an LDAP server. It has been bookmarked over 50 times on <a title="delicious - social bookmarking" href="http://www.delicious.com/">delicious</a> and has seen a bit of love in the comments and on Twitter as well. <a href="http://twitter.com/#!/ktou/status/22400052413">Onc of those tweets</a> was hash-tagged #rubykaigi, so I followed up to ask if my post showed up at the conference. Turns out <a href="http://www.slideshare.net/tasheeen/active-ldap">it did</a>. Ruby and LDAP integration was one of the topics at this year&#8217;s Ruby Kaigi and my blog post was one of the ideas presented for integrating LDAP with Rails.</p>
<p>I would not have known any of this were I not watching my blog&#8217;s Activity Stream on PostRank Analytics. Hopefully <em>I</em> can be at Ruby Kaigi next year — it&#8217;s being called &#8220;The Final RubyKaigi&#8221;, which I&#8217;m hoping doesn&#8217;t mean there will never be another one — but my blog was there and PostRank told me about it. Now <em>that&#8217;s</em> eating your own dog food.</p>
<p>By the by, if you&#8217;re a blogger, try PostRank Analytics. You can get a <a title="PostRank Analytics - Plans &amp; Pricing" href="https://analytics.postrank.com/pricing">30-day free trial</a>, or help us connect influencers with brands by signing up to <a href="http://connect.postrank.com/">PostRank Connect</a> and get a free PostRank Analytics account to help you measure and maximize your influence.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2010/11/13/how-postrank-analytics-brought-me-to-ruby-kaigi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>RSpec Matchers: More Than Just Assertions</title>
		<link>http://www.liveandcode.com/2010/11/10/rspec-matchers-more-than-just-assertions/</link>
		<comments>http://www.liveandcode.com/2010/11/10/rspec-matchers-more-than-just-assertions/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 03:51:34 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=513</guid>
		<description><![CDATA[I recently read a post from Carbon Five about RSpec best practices. The most delightful thing about it was reading it after I&#8217;d been writing a spec at work and noticing that how I was doing it was close to what was being described. It was a little bit of validation, a pat on the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read a <a title="RSpec best practices - Carbon Five Community" href="http://blog.carbonfive.com/2010/10/testing/rspec-best-practices">post</a> from Carbon Five about RSpec best practices. The most delightful thing about it was reading it after I&#8217;d been writing a spec at work and noticing that how I was doing it was close to what was being described. It was a little bit of validation, a pat on the back for all of the reading, practicing, and thinking about BDD that I&#8217;d done to that point. But then Carbon Five asks &#8220;so what else?&#8221;<span id="more-513"></span></p>
<p>I recalled some of my first experiences with RSpec. For one job interview with a company that was using Rails, I was asked to bring in a small Rails app, a toy problem that they previously set for me to test my skills. I worked really hard on that application, struggling with every feature because at the time I had done relatively little with Ruby and Rails. I was specifically asked to make sure that this app was well tested. I had been doing some reading about TDD around that time, which led me to BDD, and in the context of Ruby that led me to <a title="RSpec.info: Home" href="http://rspec.info/">RSpec</a>. So I used RSpec and <a title="Spec::Rails" href="http://rspec.info/rails/">Spec::Rails</a>, though I wasn&#8217;t entirely familiar with them.</p>
<p>During that interview, the developers sitting in asked me to add a new feature to my app. I remember that I needed to check that a particular value went up by 1 when I called a method. So I stored the old value in a local variable, invoked the method, and then checked that the new value was what I expected it to be.</p>
<p>&#8220;That&#8217;s good, but it can be better.&#8221;</p>
<p>From there, I was walked through something that at the time I couldn&#8217;t fully comprehend. I was asked to create a lambda with a block invoking the method, append <code>.should</code> to that, a space, and then <code>change</code>, with another block that would produce the value I wanted to check, and then <code>.by(1)</code>. The three-line example I&#8217;d originally written became a single (albeit long and, for me at the time, hard to read) line. I was assured that it did precisely the same thing and they asked me to run it. Sure enough, it passed. That was my introduction to a whole side of RSpec I&#8217;d never learned about before: <em>matchers</em>.</p>
<p>I always wrote <code>should ==</code> or <code>should_not ==</code>. I just thought of it as the particular way that RSpec did assertions, just a small part of the entire DSL. But he&#8217;s much more powerful than he actually looks. When you call <code>should</code> on something you pass an argument. That argument is a matcher. But what is a matcher? A matcher takes an object, evaluates some sort of predicate on it, and says &#8220;yes, that matches&#8221; or &#8220;no, it doesn&#8217;t&#8221;. It can also define a friendly message for failure (that&#8217;s what you see when an expectation fails while running your specs) and a description of what that matcher asserts, which appears in the documentation output format (&#8220;specdoc&#8221; if you&#8217;re still using RSpec 1.x) if you called <code>#it</code> with just a block and no string.</p>
<p>So let&#8217;s look at something similar to what I was walked through during that interview:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">describe Foo <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@foo</span> = Foo.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  describe <span style="color:#996600;">&quot;.harbl&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    it <span style="color:#996600;">&quot;should raise 'bar' by 1&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@foo</span>.<span style="color:#9900CC;">harbl</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">should</span> change <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@foo</span>.<span style="color:#9900CC;">bar</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">by</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</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;">end</span></pre></div></div>

<p>Now, say we&#8217;ve implemented everything except the part where calling <code>#harbl</code> on a <code>Foo</code> increases the value by 1. When we run this, we&#8217;ll see a message saying something to the effect of &#8220;{ @foo.bar } should have been changed by 1, but was changed by 0.&#8221; Here, we see the first benefit using a special matcher instead of a raw assertion: the failure message actually reflects the behaviour we were expecting to see! Instead of seeing &#8220;1 doesn&#8217;t equal 2&#8243; which tells us little about what we were actually testing, we see &#8220;@foo.bar should have changed, but it didn&#8217;t.&#8221;</p>
<p>So how does this actually work? We&#8217;ll take this from the inside and work our way out. The &#8220;magic&#8221; here is <code>change</code>. It&#8217;s a method, but what does it return? It returns a fresh instance of <code><a href="https://github.com/dchelimsky/rspec/blob/master/lib/spec/matchers/change.rb">Spec::Matchers::Change</a></code> (in fact, the definition of the method is right in that file!). The block that you pass it tells the matcher what it&#8217;s supposed to evaluate to get the value before and after. We call <code>#by</code> on that which is just a little decorator. It tells the matcher what kind of change it&#8217;s looking for. We&#8217;re telling the matcher &#8220;run that block I gave you before and after running the code I plan to give you later. The value you get from that block after running the code should be 1 greater than it was when you ran it before the code.&#8221;</p>
<p>So now the matcher knows how to get the value, and it knows what we expect it to change by. It needs one last ingredient: a subject. What piece of code does the matcher need to run so it can observe the change in the value? That&#8217;s where <code>#should</code> comes in. He&#8217;s pretty simple: call him on something and he&#8217;ll take that something, pass it over to the matcher that he&#8217;s given as an argument, and say &#8220;tell me if this matches or not.&#8221; And just like that we&#8217;ve turned the matcher that we&#8217;ve built up into an expectation on the behaviour of our code.</p>
<p>That&#8217;s pretty nifty all on it&#8217;s own, but here&#8217;s where it becomes truly beautiful: RSpec comes with matchers for <a href="https://github.com/dchelimsky/rspec/tree/master/lib/spec/matchers">just about every common predicate you can think of</a>. Not only does it come with all of these matchers, but it comes with methods that dynamically generate matchers! Wait, what?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">describe Orange <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@orange</span> = Orange.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it <span style="color:#996600;">&quot;should be tangy&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@orange</span>.<span style="color:#9900CC;">should</span> be_tangy
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>While the guys who made RSpec included a lot of matchers, I&#8217;m pretty sure a &#8220;be tangy&#8221; matcher is not one of them. And yet this is a perfectly valid RSpec example, without any special extensions or monkey patching. How? It turns out that RSpec&#8217;s &#8220;be&#8221; matcher is a lot smarter than it looks. If you connect it to something with an underscore, it will send that something with a <code>?</code> on the end as a message to the subject and determine pass or fail based on the response.</p>
<p>Here&#8217;s the code we would write to make that spec pass:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Orange
  <span style="color:#9966CC; font-weight:bold;">def</span> tangy?
    <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></pre></div></div>

<p>And there you have it. We have a tangy orange, and RSpec lets us specify that it should be tangy. There is so much of this kind of awesomeness in RSpec that I can&#8217;t fit it all into this post. You really should explore RSpec matchers for yourself. If you somehow find a predicate that RSpec cannot express with one of its matchers, you can easily create your own. Just follow the model of RSpec&#8217;s other matcher classes; define a few necessary methods and some decorators if you need them and you&#8217;re done. If you&#8217;re using RSpec 2, I hear they&#8217;ve even created a small DSL to make custom matchers even easier to generate. Spiffy!</p>
<p>So if I could give only one piece of advice about RSpec best practices, it would be this: if your expectation looks messy, RSpec probably has a matcher that can make it cleaner.</p>
<p><strong><em>Use the matchers, Luke.</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2010/11/10/rspec-matchers-more-than-just-assertions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing gabriel</title>
		<link>http://www.liveandcode.com/2010/08/31/introducing-gabriel/</link>
		<comments>http://www.liveandcode.com/2010/08/31/introducing-gabriel/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 22:18:34 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[shell scripting]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=408</guid>
		<description><![CDATA[If you&#8217;re running a large-scale Ruby project, chances are that god is monitoring your long-running, background processes. I&#8217;ve had some encounters with god and I decided that I was fed up with not having tab completion for it. So I created gabriel. gabriel helps you communicate with god by offering completions for subcommands, options, tasks, [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re running a large-scale Ruby project, chances are that <a title="mojombo's god at master - GitHub" href="http://github.com/mojombo/god">god </a>is monitoring your long-running, background processes. I&#8217;ve had some encounters with god and I decided that I was fed up with not having tab completion for it.</p>
<p>So I created <a title="enricob's gabriel at master" href="http://github.com/enricob/gabriel">gabriel</a>. gabriel helps you communicate with god by offering completions for subcommands, options, tasks, and group names for your bash shell. Installing it is pretty simple:</p>
<pre style="padding-left: 30px;">$ gem install gabriel
$ gabriel-install
(From there, follow the on-screen instructions)
</pre>
<p>To ensure you get completions in every session, confirm that your <code>.bashrc</code> is sourced in your <code>.bash_profile</code> (or <code>.profile</code>, if you&#8217;re a bit more old-school).</p>
<p>This is my first foray into shell scripting and into publishing a ruby gem, so check out the <a title="enricob's gabriel at master" href="http://github.com/enricob/gabriel">source</a> and if you can make gabriel even better, please do contribute your patches.</p>
<p>May god watch kindly over all of your processes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2010/08/31/introducing-gabriel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Context Switching</title>
		<link>http://www.liveandcode.com/2010/01/11/context-switching/</link>
		<comments>http://www.liveandcode.com/2010/01/11/context-switching/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 02:57:37 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[The Human Computer]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=337</guid>
		<description><![CDATA[One of the reasons this blog is called &#8220;Live &#38; Code&#8221; is because Computer Science (and by extension programming) is one of the lenses through which I view the world. There are often parallels between the way we think and the way computers work. I don&#8217;t think that this is an accident; the term &#8220;computer&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>One of the reasons this blog is called &#8220;Live &amp; Code&#8221; is because Computer Science (and by extension programming) is one of the lenses through which I view the world. There are often parallels between the way we think and the way computers work. I don&#8217;t think that this is an accident; the term &#8220;computer&#8221; used to refer to a human being whose profession was computing values. These people would sit and work through algorithms to calculate values that would be used in scientific research.</p>
<p>One of these parallels between humans and computers is the way that we multitask. Computers only <em>seem</em> to multitask by switching between tasks rapidly, with the exception of new multi-core and multi-processor systems, which can run as many tasks in parallel as there are cores or processors. So it was interesting to read recently that <a title="Human multitasking - Wikipedia" href="http://en.wikipedia.org/wiki/Human_multitasking">the prevailing theory is that humans multitask in precisely the same way</a>!</p>
<p><span id="more-337"></span></p>
<p>Analogous to a multitasking CPU, current experimental psychology research shows that <a title="Dual-Task Interference in Simple Tasks: Data and Theory - Harold Pashler [PDF]" href="http://www.pashler.com/Articles/Pashler_PB1994.pdf">interference occurs</a> when we attempt to perform even simple tasks simultaneously. There are certain exceptions to this, particularly when one of the two tasks is stored in &#8220;muscle memory&#8221;, but this is because we can perform such tasks without even thinking about them. In many other cases, the effectiveness with which we perform the tasks is diminished when we are doing more than one task at the same time.</p>
<p>It is also common wisdom in IT that when somebody is deep in his/her work (commonly referred to as <em><a title="Flow (psychology) - Wikipedia" href="http://en.wikipedia.org/wiki/Flow_(psychology)">flow</a></em>), an interruption of even a few seconds can result in <em>15 minutes</em> of lost productivity. While I&#8217;ve never verified the actual number, I have often had trouble putting myself back on a task after being jolted out of it by a short interruption.</p>
<p>When a CPU switches tasks, it must perform a <em><a title="Context Switch - Wikipedia" href="http://en.wikipedia.org/wiki/Context_switch">context switch</a></em>. The current state of the program that is running on the computer, including the values stored in the registers, a reference to the next instruction to execute, and other housekeeping information are stored in memory (RAM). When the program comes back, the stored information is put back into its proper places so that the CPU can pick up where it left off. This is a fairly costly operation as the CPU cannot access RAM even a fraction as fast as it can access its registers. Of course, this means that when a CPU runs two programs simultaneously, it is less efficient at each than if it were concentrating entirely on one program or the other. Determining when to switch to a different program is therefore an incredibly important concern in the design of modern operating systems.</p>
<p>I&#8217;ve witnessed human &#8220;context switching&#8221; in a co-worker of mine. When I knock on her door to ask her a quick question about work that I&#8217;m doing for her, she asks me to give her a moment and looks to be deep in thought. She then asks me to repeat my question and answers it with surprising conciseness and accuracy. She has told me that she needs to switch from the mindset of what she&#8217;s currently working on to the mindset of what I&#8217;m asking her or she can&#8217;t answer properly.</p>
<p>This isn&#8217;t to say that multitasking is <em>bad</em>, but I am acutely aware that when I do multitask, I am not going to be able to perform each task as effectively as if I were focused entirely on one. Also, there are certain points at which it is easier or more natural to switch from doing one thing to doing another, particularly where I&#8217;ve accomplished a sub-goal in the task or where I&#8217;ve written down detailed notes on the current state of what I&#8217;m doing, which makes it easier to switch back to it later.</p>
<p>What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2010/01/11/context-switching/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Life advice in code</title>
		<link>http://www.liveandcode.com/2009/12/21/life-advice-in-code/</link>
		<comments>http://www.liveandcode.com/2009/12/21/life-advice-in-code/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 15:53:58 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Personal Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[life advice]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=298</guid>
		<description><![CDATA[Live &#38; Code has been largely about programming and life lessons. So, you can imagine that I was incredibly delighted to see these two combined in a small exchange on Facebook started by Reginald &#8220;raganwald&#8221; Braithwaite. It&#8217;s a valuable life lesson made incredibly concise in my favourite programming language, Ruby. I leave the interpretation to [...]]]></description>
			<content:encoded><![CDATA[<p>Live &amp; Code has been largely about programming and life lessons. So, you can imagine that I was incredibly delighted to see these two combined in a small exchange on Facebook started by Reginald &#8220;raganwald&#8221; Braithwaite.</p>
<div id="attachment_299" class="wp-caption aligncenter" style="width: 490px"><img class="size-full wp-image-299  " title="Life Advice in Code (raganwald)" src="http://www.liveandcode.com/wp-content/uploads/2009/12/Life-Advice-in-Code-raganwald.png" alt="Life Advice in Code (raganwald)" width="480" height="146" /><p class="wp-caption-text">Self-esteem expressed in Ruby</p></div>
<p style="text-align: left;">It&#8217;s a valuable life lesson made incredibly concise in my favourite programming language, Ruby. I leave the interpretation to the reader. It relates to some themes that I&#8217;ve already written about before and will probably write more about in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/12/21/life-advice-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LDAP-based RBAC with ActiveLdap and declarative_authorization</title>
		<link>http://www.liveandcode.com/2009/12/14/ldap-based-rbac-with-activeldap-and-declarative_authorization/</link>
		<comments>http://www.liveandcode.com/2009/12/14/ldap-based-rbac-with-activeldap-and-declarative_authorization/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 01:40:22 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[identity management]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=271</guid>
		<description><![CDATA[If you followed my previous tutorial on implementing pass-through authentication to LDAP with Authlogic, you might be wondering how it can be extended to give different permissions to members of different LDAP groups. ActiveLdap and declarative_authorization make this incredibly simple. As with the first tutorial, I&#8217;ve pushed a branch based on authlogic_example that you can [...]]]></description>
			<content:encoded><![CDATA[<p>If you followed my <a title="LDAP Pass-through Authentication with Authlogic and ActiveLdap - Live &amp; Code" href="http://www.liveandcode.com/2009/08/30/ldap-pass-through-authentication-with-authlogic-and-activeldap/">previous tutorial</a> on implementing pass-through authentication to LDAP with Authlogic, you might be wondering how it can be extended to give different permissions to members of different LDAP groups. ActiveLdap and declarative_authorization make this incredibly simple.<br />
<span id="more-271"></span></p>
<p>As with the first tutorial, I&#8217;ve pushed a <a title="enricob's authlogic_example at ldap-rbac - GitHub" href="http://github.com/enricob/authlogic_example/tree/ldap-rbac">branch</a> based on <code>authlogic_example</code> that you can use as a concrete example.</p>
<p>First, let&#8217;s bring in the <a title="stffn's declarative_authorization at master - GitHub" href="http://github.com/stffn/declarative_authorization">declarative_authorization</a> gem:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># My example code was written before GitHub decided that they don't want to</span>
<span style="color:#008000; font-style:italic;"># build gems anymore but this line should do the trick.</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;declarative_authorization&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'http://gemcutter.org'</span></pre></div></div>

<p>Now, we need to make an ActiveLdap model class like <code>LdapUser</code> but for groups. We&#8217;ll call this <code>LdapGroup</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> LdapGroup <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveLdap::Base</span>
  ldap_mapping <span style="color:#ff3333; font-weight:bold;">:dn_attribute</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;cn&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:scope</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; :<span style="color:#CC0066; font-weight:bold;">sub</span>,
    <span style="color:#ff3333; font-weight:bold;">:prefix</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;ou=groups,o=users&quot;</span>
&nbsp;
  has_many <span style="color:#ff3333; font-weight:bold;">:members</span>, <span style="color:#ff3333; font-weight:bold;">:class_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;LdapUser&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:wrap</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;member&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:primary_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;dn&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;dn&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The <code>ldap_mapping</code> part is very similar to the previous one for <code>LdapUser</code>, but the <code>has_many</code> is where the real ActiveLdap magic starts!</p>
<p>We are letting ActiveLdap know that an <code>LdapGroup</code> has <code>LdapUser</code> members, which are defined in the multi-valued attribute <code>member</code>, where each of the values is the DN of an <code>LdapUser</code>. If you&#8217;re looking for a bit more detail, refer to the <a href="http://ruby-activeldap.rubyforge.org/doc/">ActiveLdap documentation</a>. Keep in mind that the code above is for the specific schema used by my example application and you may have to change it to match yours.</p>
<p>With that, we should be able to list all of our LDAP groups and find the members of any particular one in the console:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; LdapGroup.<span style="color:#9900CC;">all</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> g.<span style="color:#9900CC;">cn</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;super_admin&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; LdapGroup.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;super_admin&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">members</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> m.<span style="color:#9900CC;">dn</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;uid=ebianco,o=users,dc=example,dc=com&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Now we&#8217;ll create an association between <code>LdapUser</code> and <code>LdapGroup</code> so that given a user, we can figure out which groups he/she is a member of. We&#8217;re going to tell ActiveLdap that a user&#8217;s groups are those groups in which the user&#8217;s DN appears in the group&#8217;s <code>member</code> attribute:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">belongs_to <span style="color:#ff3333; font-weight:bold;">:groups</span>, <span style="color:#ff3333; font-weight:bold;">:class_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;LdapGroup&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:many</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;member&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;dn&quot;</span></pre></div></div>

<p>Let&#8217;s check our handiwork in the console:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&amp;</span>gt; LdapUser.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ebianco&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">groups</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> g.<span style="color:#9900CC;">cn</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;super_admin&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Now why did we go through all of this effort? declarative_authorization allows us to provide an instance method for <code>User</code>, <code>#role_symbols</code>, that returns an array of symbols representing the list of roles that the user has. With our ActiveLdap setup, mapping a user&#8217;s roles to LDAP groups becomes a one-liner:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> role_symbols
  ldap_entry.<span style="color:#9900CC;">groups</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>g<span style="color:#006600; font-weight:bold;">|</span> g.<span style="color:#9900CC;">cn</span>.<span style="color:#9900CC;">to_sym</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>From here, we can define the authorization rules for our LDAP-based roles. declarative_authorization reads these from <code>config/authorization_rules.rb</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">authorization <span style="color:#9966CC; font-weight:bold;">do</span>
  role <span style="color:#ff3333; font-weight:bold;">:super_admin</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    has_permission_on <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:manage</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  role <span style="color:#ff3333; font-weight:bold;">:guest</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    has_permission_on <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:read</span>
    has_permission_on <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:update</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      if_attribute <span style="color:#ff3333; font-weight:bold;">:id</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; is <span style="color:#006600; font-weight:bold;">&#123;</span>user.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</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;">end</span>
&nbsp;
privileges <span style="color:#9966CC; font-weight:bold;">do</span>
  privilege <span style="color:#ff3333; font-weight:bold;">:manage</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    includes <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:read</span>, <span style="color:#ff3333; font-weight:bold;">:update</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we can use declarative_authorization as usual. I won&#8217;t go through all the details but if you want a primer on using declarative_authorization, you can start <a title="README.rdoc at master from stffn's declarative_authorization" href="http://github.com/stffn/declarative_authorization/blob/master/README.rdoc">here</a> and browse the commits on my <code>authlogic_example</code> branch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/12/14/ldap-based-rbac-with-activeldap-and-declarative_authorization/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Adventures in Ruby: When Constants Aren&#8217;t</title>
		<link>http://www.liveandcode.com/2009/11/16/adventures-in-ruby-when-constants-arent/</link>
		<comments>http://www.liveandcode.com/2009/11/16/adventures-in-ruby-when-constants-arent/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 18:14:48 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mystery]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=249</guid>
		<description><![CDATA[I just squashed a bug that had me scratching my head for at least a good half-hour or so involving a class constant that kept on getting changed. Here&#8217;s the setup (anonymized so that I&#8217;m not exposing gooey proprietary secrets): class WebTransaction &#160; # Base URL for transaction web service SERVICE_URL = &#34;http://accountingservice.com/&#34; &#160; def [...]]]></description>
			<content:encoded><![CDATA[<p>I just squashed a bug that had me scratching my head for at least a good half-hour or so involving a class constant that kept on getting changed. Here&#8217;s the setup (anonymized so that I&#8217;m not exposing gooey proprietary secrets):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> WebTransaction
&nbsp;
  <span style="color:#008000; font-style:italic;"># Base URL for transaction web service</span>
  SERVICE_URL = <span style="color:#996600;">&quot;http://accountingservice.com/&quot;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> transaction_url
    url = SERVICE_URL
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;VAL1=foo&quot;</span>
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;&amp;VAL2=bar&quot;</span>
    url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The class is meant to represent a transaction being posted against a rather odd web service that actually uses GET rather than POST for posting transactions (bad web service!). In my actual code, the URL parameters appended to the SERVICE_URL base would be determined on a per-object basis but I&#8217;ve simplified it here.</p>
<p>Here&#8217;s the punchline: <code>SERVICE_URL</code> was <em>changing!</em> Calls to <code>transaction_url</code> would keep on appending more variables to it. If you&#8217;re particularly clever with Ruby, you&#8217;ve already figured out exactly why. But if you&#8217;re scratching your head like I was, here are some hints:</p>
<ul>
<li>In Ruby, constants <em>aren&#8217;t</em>. In fact, they&#8217;re really no different from variables except for the fact that Ruby detects that variable names in all-caps are probably supposed to stay constant and warns if you try to assign to them.</li>
<li><code>&lt;&lt;</code>, for strings, will append to the end of the string.</li>
<li>Ruby strings are mutable. That is, operations on a string variable will usually be done in place, rather than returning a new string. (Method calls, on the other hand, are a different story!)</li>
<li>Assigning a String variable to another String variable will assign the reference. That is, the two variables will be pointing at the same object.</li>
</ul>
<p>Hit the jump to see the solution to this little mystery&#8230;<br />
<span id="more-249"></span></p>
<p><code>SERVICE_URL</code> is only directly assigned to when it is declared. But what happens when I assign <code>SERVICE_URL</code> to the local variable in <code>transaction_url</code>, <code>url</code>. <em>They&#8217;re pointing to the same object!</em></p>
<p>Then I go ahead and I start appending things to the end of the string. Ruby has no complaints because I&#8217;m not assigning to <code>SERVICE_URL</code> but <em>the object that it is pointing to is being changed!</em> The operation is causing the URL parameters to be appended in place.</p>
<p>And that, friends, is what happens when constants aren&#8217;t. Let&#8217;s fix this little error:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> WebTransaction
&nbsp;
  <span style="color:#008000; font-style:italic;"># Base URL for transaction web service</span>
  SERVICE_URL = <span style="color:#996600;">&quot;http://accountingservice.com/&quot;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> transaction_url
    url = SERVICE_URL.<span style="color:#9900CC;">clone</span>
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;VAL1=foo&quot;</span>
    url <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;&amp;VAL2=bar&quot;</span>
    url
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, instead of appending to the string object referenced by <code>SERVICE_URL</code> directly, I&#8217;m creating a copy of the object and then appending URL parameters to that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/11/16/adventures-in-ruby-when-constants-arent/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8220;So&#8230; what do you do?&#8221;</title>
		<link>http://www.liveandcode.com/2009/09/03/so-what-do-you-do/</link>
		<comments>http://www.liveandcode.com/2009/09/03/so-what-do-you-do/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 14:55:40 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Personal Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[self esteem]]></category>
		<category><![CDATA[social skills]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=216</guid>
		<description><![CDATA[I think that this is a dreaded question for many programmers and others in fields with a scientific or mathematical bent. How do you explain the full extent of what it is that you do and why it&#8217;s valuable without being dismissed as just another creepy nerd? How do you fight the stereotype of unkempt [...]]]></description>
			<content:encoded><![CDATA[<p>I think that this is a dreaded question for many programmers and others in fields with a scientific or mathematical bent. How do you explain the full extent of what it is that you do and why it&#8217;s valuable without being dismissed as just another creepy nerd? How do you fight the stereotype of unkempt basement-dwellers living with their parents, spending most of their time on the Internet because they&#8217;re too socially awkward to interact with people in other settings?</p>
<p>About a week ago there was a <a title="&quot;This is why I hate programming.&quot; - Programming Reddit" href="http://www.reddit.com/r/programming/comments/9emkz/this_is_why_i_hate_programming/">discussion</a> about this on programming Reddit. The original poster, a programmer, feels a sense of inferiority because while others can succinctly explain the &#8220;awesome&#8221; in what they&#8217;re doing, he cannot. I sympathize with this sentiment. I love what I do but how do I transfer that passion without drowning the other person in the technical details? It feels like without the finer, technical details, my job description becomes distilled to &#8220;I make websites.&#8221;</p>
<p>Really? That&#8217;s <em>it</em>? That&#8217;s what I do for a living?</p>
<p><span id="more-216"></span></p>
<p>But is the problem really that what we&#8217;re doing isn&#8217;t interesting to the layperson? Web developers built Google, Facebook, MySpace, Wikipedia, and many more technologies that people use every day. We are building cool web applications that, while they might not gain the same degree of popularity or critical mass, can potentially provide just as much value. If we didn&#8217;t think that web development was an awesome career, a lot of us probably wouldn&#8217;t be doing it.</p>
<p>(And by the way, if you&#8217;re making money doing something you have absolutely no passion for, you might want to try and fix that.  Just sayin&#8217;.)</p>
<p>So why do we have difficulty getting people to identify with this?  Perhaps <a title="mistertim comments on &quot;This is why I hate programming.&quot; - Programming Reddit" href="http://www.reddit.com/r/programming/comments/9emkz/this_is_why_i_hate_programming/c0ch10i">this comment</a> from the Programming Reddit discussion might shed some light on the topic (emphasis mine):</p>
<blockquote><p>Obnoxious braying girl: (To me and a friend) Oh, i bet i can guess what you do! go on, let me! (Obnoxious braying girl makes a few guesses about my friend&#8217;s job, eventually works out he does some sort of admin thing in publishing, polite words exchanged about this field) Obnoxious braying girl: (to me) Hmm.. you&#8217;re&#8230; a graphic designer? Me: hah! god no OBG: A journalist? Me: Well, that does sound like fun, but no (back and forth for a bit until she eventually asks me what i do Me: (in jokey, self deprecating tone) &#8211; <strong><em>I&#8217;m a massive nerd, basically &#8211; I program computers and make websites for people</em></strong> OBG: oh, right&#8230; (she stares down into her drink) Me: (trying to salvage this conversation) &#8211; so, and what do you do? OBG: Oh, i work at a corporate events company! it&#8217;s really exciting, basically we&#8230;. Cue 15 minutes of skull-numbingly boring monologue about basically being a travel-agent for corporations. It puzzles me though &#8211; why is anyone who doesn&#8217;t work with computers given licence to go on and on about their job (Despite the fact that asking people about work is basically a social nicety and not interesting at all to anyone socially), wheras anyone whose interests (whether work based or not) include anything technology or science-based are automatically percieved as boring socially-inept people who can&#8217;t talk about anything else? Fuck that shit &#8211; next time i&#8217;m introduced to someone I&#8217;m going straight into explaining recursion.</p></blockquote>
<p>There are a few things that one could criticize here but the point I&#8217;ve decided to focus on here is the blatant self-deprecation. If you read the rest of the comments in the discussion, you might notice, like I have, that this is actually surprisingly common — it&#8217;s like many of us have been conditioned to believe that what we do is simply not interesting to a vast majority of people, so we might as well just say &#8220;I do computer stuff&#8221; and get it over with.</p>
<p>Alun Anderson, Editor-in-Chief of <a href="http://www.newscientist.com/">New Scientist</a> magazine, would <a title="Notable Alumni - Interview with Alun Anderson - University of Sussex" href="http://www.sussex.ac.uk/Units/alumni/notable_alumni/interviews/Anderson_interview.html">beg to differ</a> (emphasis mine again):</p>
<blockquote><p>Science writing used to be slightly apologetic: [<em>puts on whiny voice</em>] &#8220;this is all going to be terribly difficult, but I&#8217;ll try and make it easy for you&#8221;. Like they&#8217;ve sugar coated something you don&#8217;t really want to take. Our goal was to really change that &#8211; change the people and the ideas &#8211; to be self-confident. Science often suffers from this sort of cringe factor &#8211; &#8220;I&#8217;m a boring scientist, you probably don&#8217;t want to talk to me&#8221;. <strong><em><span style="font-style: normal;">My policy was if you&#8217;re talking to someone else the approach is: </span>&#8220;what&#8217;s happening in science is the most interesting thing in the world, and if you don&#8217;t agree with me just fuck off, because I&#8217;m not interested in talking to you&#8221;<span style="font-style: normal;">.</span> <span style="font-weight: normal;"><span style="font-style: normal;">You had to have that kind of attitude.</span></span></em></strong> That tended to be the kind of attitude of people in the arts: [<em>in snooty voice - think Brian Sewell</em>] &#8220;Of course I am doing something interesting&#8221;, so I took the same attitude. If you&#8217;re not interested, I don&#8217;t want to explain to you &#8211; you&#8217;re just a fucking idiot, so get out of my way! And it worked, because <strong><em><span style="font-style: normal;">if you write like,</span><span style="font-weight: normal;"> </span>&#8220;I&#8217;m really interested in this, it&#8217;s not only interesting its really important. If you can&#8217;t see this, you&#8217;re probably a moron!&#8221; </em>It works.</strong> It has to be true to a degree. Otherwise it&#8217;s just piped bullshit, or triumphalism or something. The thing is, it is really interesting and important. People from the sciences do often have massive inferiority complexes.</p></blockquote>
<p><strong>I believe that what I do is interesting, important, and valuable.</strong><strong> </strong>If I didn&#8217;t, <em>I&#8217;d be doing something else</em>. And so, I will endeavour to find a way to convey that value and importance and when someone asks me what I do, that is how I will answer.</p>
<p>Anyone who absolutely can&#8217;t identify with my passion for what I do is probably somebody I really wouldn&#8217;t like anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/09/03/so-what-do-you-do/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>LDAP Pass-through Authentication with Authlogic and ActiveLdap</title>
		<link>http://www.liveandcode.com/2009/08/30/ldap-pass-through-authentication-with-authlogic-and-activeldap/</link>
		<comments>http://www.liveandcode.com/2009/08/30/ldap-pass-through-authentication-with-authlogic-and-activeldap/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 17:57:47 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[identity management]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=195</guid>
		<description><![CDATA[Today, I pushed a branch to my fork of authlogic_example: with-activeldap. This branch shows a way of implementing pass-through authentication to an LDAP server using ActiveLdap and Authlogic, with just some small changes to the User and UserSession models. First, we&#8217;ll need to bring in the net-ldap and activeldap gems. We edit config/environment.rb to include [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I pushed a branch to my fork of <code>authlogic_example</code>: <a title="enricob's authlogic_example at with-activeldap - GitHub" href="http://github.com/enricob/authlogic_example/tree/with-activeldap"><code>with-activeldap</code></a>.</p>
<p>This branch shows a way of implementing pass-through authentication to an LDAP server using <a title="RubyForge: Ruby/ActiveLdap Project Page" href="http://rubyforge.org/projects/ruby-activeldap/">ActiveLdap</a> and <a title="binarylogic's authlogic at master - GitHub" href="http://github.com/binarylogic/authlogic">Authlogic</a>, with just some small changes to the <code>User</code> and <code>UserSession</code> models.</p>
<p><span id="more-195"></span><br />
First, we&#8217;ll need to bring in the <code>net-ldap</code> and <code>activeldap</code> gems. We edit <code>config/environment.rb</code> to include the following two lines:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;net-ldap&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'&gt;=0.0.5'</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;activeldap&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;active_ldap&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'1.0.9'</span></pre></div></div>

<p>If you use <code>sudo gem install net-ldap</code>, you&#8217;ll get 0.0.4. Here, I&#8217;ve built the 0.0.5 gem from the <a title="innovationfactory's ruby-net-ldap at master - GitHub" href="http://github.com/innovationfactory/ruby-net-ldap/">GitHub repo</a>. This is because there are some bug fixes in the GitHub master that aren&#8217;t in the RubyForge gem. In the <code>with-activeldap</code> branch, the two gems are vendored.</p>
<p>Now, we create config/ldap.yml to configure ActiveLdap&#8217;s connection to our LDAP server. Here&#8217;s mine:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">development:
  host: 127.0.0.1
  base: dc=dev,dc=Asuka,dc=local
  bind_dn: cn=Manager,dc=dev,dc=Asuka,dc=local
  password: secret
&nbsp;
test:
  host: 127.0.0.1
  base: dc=test,dc=Asuka,dc=local
  bind_dn: cn=Manager,dc=Asuka,dc=local
  password: secret
&nbsp;
production:
  host: 127.0.0.1
  base: dc=prod,dc=Asuka,dc=local
  bind_dn: cn=Manager,dc=Asuka,dc=local
  password: secret</pre></div></div>

<p>This tells ActiveLdap the server/port to connect to, what the base entry for our LDAP objects is, and what user to bind as for operations on the server. Now, we create a <code>LdapUser</code> class to represent user entries in the LDAP:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> LdapUser <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveLdap::Base</span>
  ldap_mapping <span style="color:#ff3333; font-weight:bold;">:dn_attribute</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;uid&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:scope</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> :<span style="color:#CC0066; font-weight:bold;">sub</span>,
    <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;o=users&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This defines an LDAP user as being an entry from the <code>o=users</code> organization, where all of the entries are distinguished by the <code>uid</code> attribute. Now, we should be able to use the console to list all of our LDAP users like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> LdapUser.<span style="color:#9900CC;">all</span></pre></div></div>

<p>Now, we&#8217;ll add some methods to the <code>User</code> model that allow us to look up users by login in the LDAP and create entries in the database if they don&#8217;t already exist. We&#8217;ll also need a method for forwarding the credentials provided on the login form to the LDAP and see if they are valid:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  acts_as_authentic <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#008000; font-style:italic;"># Don't validate password, since that will be held in the LDAP</span>
    c.<span style="color:#9900CC;">validate_password_field</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> ldap_entry
    LdapUser.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">login</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Tries to find a User first by looking into the database and then by</span>
  <span style="color:#008000; font-style:italic;"># creating a User if there's an LDAP entry for the given login</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">find_or_create_from_ldap</span><span style="color:#006600; font-weight:bold;">&#40;</span>login<span style="color:#006600; font-weight:bold;">&#41;</span>
    find_by_login<span style="color:#006600; font-weight:bold;">&#40;</span>login<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> create_from_ldap_if_valid<span style="color:#006600; font-weight:bold;">&#40;</span>login<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Creates a User record in the database if there is an entry in the LDAP</span>
  <span style="color:#008000; font-style:italic;"># with the given login</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">create_from_ldap_if_valid</span><span style="color:#006600; font-weight:bold;">&#40;</span>login<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      User.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> login<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> LdapUser.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>login<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">ActiveLdap::EntryNotFound</span>
      <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#008000; font-style:italic;"># Don't do anything since we can't find an entry</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  protected
    <span style="color:#008000; font-style:italic;"># Authenticates the user against the LDAP.</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> valid_ldap_credentials?<span style="color:#006600; font-weight:bold;">&#40;</span>password_plaintext<span style="color:#006600; font-weight:bold;">&#41;</span>
      ldap_entry.<span style="color:#9900CC;">bind</span><span style="color:#006600; font-weight:bold;">&#40;</span>password_plaintext<span style="color:#006600; font-weight:bold;">&#41;</span>
      ldap_entry.<span style="color:#9900CC;">remove_connection</span>
      <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#6666ff; font-weight:bold;">ActiveLdap::AuthenticationError</span>, <span style="color:#6666ff; font-weight:bold;">ActiveLdap::LdapError::UnwillingToPerform</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></pre></div></div>

<p>With this, there is no longer a need for the <code>crypted_password</code> and <code>password_salt</code> columns in the <code>users</code> table, so if those columns exist, you&#8217;ll have to write a migration to remove them (or at least allow NULL values for them). Now, we modify the <code>UserSession</code> to use our custom methods for looking up users and validating their credentials:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> UserSession <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Authlogic::Session::Base</span>
  find_by_login_method <span style="color:#ff3333; font-weight:bold;">:find_or_create_from_ldap</span>
  verify_password_method <span style="color:#ff3333; font-weight:bold;">:valid_ldap_credentials</span>?
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>With that, we should now be able to log in as a user by providing her <code>uid</code> as the login and the password. LDAP pass-through authentication achieved! There is a downside though: ActiveLdap is not particularly efficient with its queries but this can be mitigated by storing the user&#8217;s LDAP entry (the <code>LdapUser</code> object) in the <code>User</code> instance when it is first looked up.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> ldap_entry
  <span style="color:#0066ff; font-weight:bold;">@ldap_entry</span> <span style="color:#006600; font-weight:bold;">||</span>= LdapUser.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">login</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In a future post, I will extend this further by modeling LDAP groups and bringing in declarative_authorization to implement role-based access control based on LDAP group membership.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/08/30/ldap-pass-through-authentication-with-authlogic-and-activeldap/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Where is why?</title>
		<link>http://www.liveandcode.com/2009/08/19/where-is-why/</link>
		<comments>http://www.liveandcode.com/2009/08/19/where-is-why/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 21:11:07 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mystery]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[_why]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=185</guid>
		<description><![CDATA[&#8220;Why The Lucky Stiff&#8221;, one of the most influential characters in the Ruby community, has simply vanished. His Twitter account, GitHub account, and most of his websites are gone without a trace. Why The Lucky Stiff&#8217;s contributions to the Ruby community include &#8220;Why&#8217;s Poignant Guide to Ruby&#8221;, a book which many state was the reason [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Why The Lucky Stiff&#8221;, one of the most influential characters in the Ruby community, has simply vanished. His <a href="http://twitter.com/_why">Twitter account</a>, <a title="why's Profile - GitHub" href="http://github.com/why/">GitHub account</a>, and <a title="Discussion on Hacker News about _why's disappearance" href="http://news.ycombinator.com/item?id=773108">most of his websites</a> are gone without a trace.</p>
<p>Why The Lucky Stiff&#8217;s contributions to the Ruby community include &#8220;Why&#8217;s Poignant Guide to Ruby&#8221;, a book which many state was the reason they got into Ruby, Shoes, an easy-to-use cross-platform GUI toolkit with innovative online distribution features, and Hpricot, a very slick HTML parser that is also a joy to use.</p>
<p>Some believe that he has decided to move on from software development because his last tweet reads &#8220;programming is rather thankless. u see your works become replaced by superior ones in a year. unable to run at all in a few more.&#8221; Some believe that his accounts were all hacked. A few others believe that his anonymity has been <a title="Who is why the lucky stiff?" href="http://whoiswhytheluckystiff.wordpress.com/">compromised</a> and that he has decided the destroy the pseudonym.</p>
<p>Wherever Why The Lucky Stiff goes from here, I hope he knows that there are so many of us who looked up to him and have him to thank for knowing the joy that is programming in Ruby. He will be sorely missed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/08/19/where-is-why/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

