<?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>disjoint thoughts &#187; ruby</title>
	<atom:link href="http://disjointthoughts.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://disjointthoughts.com</link>
	<description></description>
	<lastBuildDate>Thu, 29 Dec 2011 04:23:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How I became a web developer</title>
		<link>http://disjointthoughts.com/2011/05/19/how-i-became-a-web-developer/</link>
		<comments>http://disjointthoughts.com/2011/05/19/how-i-became-a-web-developer/#comments</comments>
		<pubDate>Fri, 20 May 2011 05:38:34 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[gaming]]></category>
		<category><![CDATA[geek community]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=523</guid>
		<description><![CDATA[When I was 6 or 7 years old, my Grandparents bought me the most awesome gift ever&#8230; a NES. This set me down a path [...]]]></description>
			<content:encoded><![CDATA[<p>When I was 6 or 7 years old, my Grandparents bought me the most awesome gift ever&#8230; a NES. This set me down a path that would ultimately lead me to where I am now. A web developer.</p>
<p>After the NES came the 286, then the Playstation 1. At some point between Commander Keen and Ridge Racer, I decided I wanted to design my own games. I didn&#8217;t to much programming though. Around the age of 10 I started writing .bat scripts&#8230; which never did anything useful. I hacked (mostly unsuccessfully) on ZZT.</p>
<p>When I was 13 I created an awesome Star Trek fan site on GeoCities giving me my first taste of HTML. Taking that to what I thought was the next step, I learned VRML&#8230; which turned out to be a step towards nowhere.</p>
<p>No, I didn&#8217;t do any real programming until college. Here I learned C, Java, ML, Prolog, Lisp, and Icon. Along with learning what real programming was, I learned that programming games is hard. I came to the realization sometime around my senior year that being a game developer would be awesome, but probably wasn&#8217;t in the cards for me. A dream job that I might have someday.</p>
<p>Towards the end of my college career, I started doing web development as a research assistant using JSPs and Servlets. This turned into somewhat of an expertise for me, and ended up landing me my first job out of college. </p>
<p>Still using Java at the time, I was introduced to Ruby and Rails by an outside consultant. This consultant was brought in to explore the possibility of using Rails on a new project. But like so many other companies at the time, using Rails (which at the time was new and unproven in the eyes of enterprise businesses) was too high of a risk. They went with the tried and true Java approach.</p>
<p>I didn&#8217;t take the Java approach. I left the company shortly after and joined a startup where I got paid to do Rails full time. I had drank the Rails kool aid. I was mainly hooked on how quickly you could implement ideas. I could come up with an idea in the morning, and have a working web app by the afternoon. I could gather requirements and show off working code the same day. I loved it.</p>
<p>At some point, I looked back and realized that game development wasn&#8217;t for me. Rapid development of games doesn&#8217;t exist the same way it does in web development. I&#8217;ve been a happy web developer ever since.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2011/05/19/how-i-became-a-web-developer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>github and submodule weirdness</title>
		<link>http://disjointthoughts.com/2009/12/08/github-and-submodule-weirdness/</link>
		<comments>http://disjointthoughts.com/2009/12/08/github-and-submodule-weirdness/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 05:18:20 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[wtf]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=504</guid>
		<description><![CDATA[So I&#8217;m not a git or github master by any means. For the most part, I know enough commands to get by and to get [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m not a git or github master by any means. For the most part, I know enough commands to get by and to get myself into trouble every now and then. My experience so far has been good with git and github, but today I ended up banging my head on a wall while I tried to figure out what was going on. Follow me, on this little trip down a rabbit hole. Maybe someone can explain what&#8217;s going on here.</p>
<p>First I generate a rails app:</p>
<blockquote><p>
lappy:tmp bsmith$ <strong>rails foo</strong>
</p></blockquote>
<p>Then I add restful authentication, using the command listed in the install documentation:</p>
<blockquote><p>
lappy:tmp bsmith$ <strong>cd foo/</strong><br />
lappy:foo bsmith$ <strong>cd vendor/plugins/</strong><br />
lappy:plugins bsmith$ <strong>git clone git://github.com/technoweenie/restful-authentication.git restful_authentication</strong><br />
Initialized empty Git repository in /Users/bsmith/tmp/foo/vendor/plugins/restful_authentication/.git/<br />
remote: Counting objects: 1741, done.<br />
remote: Compressing objects: 100% (590/590), done.<br />
remote: Total 1741 (delta 1010), reused 1641 (delta 957)<br />
Receiving objects: 100% (1741/1741), 733.93 KiB | 831 KiB/s, done.<br />
Resolving deltas: 100% (1010/1010), done.</p></blockquote>
<p>Then I add everything to git:</p>
<blockquote><p>
lappy:plugins bsmith$ <strong>cd ../..</strong><br />
lappy:foo bsmith$ <strong>git init</strong><br />
Initialized empty Git repository in /Users/bsmith/tmp/foo/.git/<br />
lappy:foo bsmith$ <strong>git add .</strong><br />
lappy:foo bsmith$ <strong>git commit -m &#8216;init&#8217;</strong><br />
[master (root-commit) f63c7ea] init<br />
 43 files changed, 8462 insertions(+), 0 deletions(-)<br />
 create mode 100644 README<br />
 create mode 100644 Rakefile<br />
&#8230;[followed by lots more output]</p></blockquote>
<p>Then I pushed it to github:</p>
<blockquote><p>
lappy:foo bsmith$ <strong>git remote add github git@github.com:benjaminleesmith/submodule_test.git</strong><br />
lappy:foo bsmith$ <strong>git push github master</strong><br />
Counting objects: 64, done.<br />
Delta compression using up to 2 threads.<br />
Compressing objects: 100% (56/56), done.<br />
Writing objects: 100% (64/64), 79.32 KiB, done.<br />
Total 64 (delta 10), reused 0 (delta 0)<br />
To git@github.com:benjaminleesmith/submodule_test.git<br />
 * [new branch]      master -> master
</p></blockquote>
<p>Next, I take a look at the vendor directory that got committed go github:</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.com/lh/photo/q3k8001wNzSpvdpRe18SWw?feat=embedwebsite"><img src="http://lh5.ggpht.com/_m1FTfX8GeeI/Sx8xVK8ZZEI/AAAAAAAAAYM/lcPtKR-BpHw/s800/Screen%20shot%202009-12-08%20at%209.53.51%20PM.png" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right">From <a href="http://picasaweb.google.com/benjamin.lee.smith/ScreenShots?feat=embedwebsite">screen shots</a></td>
</tr>
</table>
<p>I assume that the little folder icon with an arrow means that the restful authentication code is being hosted elsewhere&#8230; as a submodule maybe? The folder is not clickable, so I can&#8217;t see what&#8217;s inside&#8230; if there&#8217;s anything!</p>
<p>So my next step is to pull the project, say for a deployment on another system:</p>
<blockquote><p>
lappy:tmp bsmith$ <strong>mkdir bar</strong><br />
lappy:tmp bsmith$ <strong>cd bar/</strong><br />
lappy:bar bsmith$ <strong>git init</strong><br />
Initialized empty Git repository in /Users/bsmith/tmp/bar/.git/<br />
lappy:bar bsmith$ <strong>git pull git://github.com/benjaminleesmith/submodule_test.git</strong><br />
remote: Counting objects: 64, done.<br />
remote: Compressing objects: 100% (56/56), done.<br />
remote: Total 64 (delta 10), reused 0 (delta 0)<br />
Unpacking objects: 100% (64/64), done.<br />
From git://github.com/benjaminleesmith/submodule_test<br />
 * branch            HEAD       -> FETCH_HEAD
</p></blockquote>
<p>Then&#8230; I go searching for restful auth in this latest pull&#8230;</p>
<blockquote><p>lappy:bar bsmith$ <strong>cd vendor/plugins/</strong><br />
lappy:plugins bsmith$ <strong>ls</strong><br />
restful_authentication<br />
lappy:plugins bsmith$ <strong>cd restful_authentication/</strong><br />
lappy:restful_authentication bsmith$ <strong>ls</strong><br />
lappy:restful_authentication bsmith$
</p></blockquote>
<p>Yikes! The directory is there, but the contents are missing! I don&#8217;t know if this is a problem with the way I added restful auth to my local git repo, or if this is an issue with github. I noticed this when a production deployment was failing saying &#8220;uninitialized constant User::Authentication (NameError)&#8221; as if restful auth was no where to be found.</p>
<p>It seems less than intuitive to say the least. If anyone knows what&#8217;s going on here, leave me a comment please! Otherwise, my fix was to clone restful auth, then remove its git directory. This allowed me to push to github exactly what my local copy contained. </p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2009/12/08/github-and-submodule-weirdness/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>flickr gadget for google wave</title>
		<link>http://disjointthoughts.com/2009/10/21/flickr-gadget-for-google-wave/</link>
		<comments>http://disjointthoughts.com/2009/10/21/flickr-gadget-for-google-wave/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 17:00:04 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[photography]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=485</guid>
		<description><![CDATA[So I got an invite to Google Wave last week. It was pretty fun to play around with the latest and greatest toy from google, [...]]]></description>
			<content:encoded><![CDATA[<p>So I got an invite to <a href="https://wave.google.com" target="_google">Google Wave</a> last week. It was pretty fun to play around with the latest and greatest toy from google, but the novelty quickly wore off. After everyone realized they were using it like a chatroom + wiki, I think we all got bored. When I was first invited, everyone was adding and editing waves&#8230; now no one I know is still using it.</p>
<p>ANYWAYS&#8230; during the first few hours of being excited about Google Wave, I decided to write a &#8220;Gadget&#8221; which would embed a <a href="http://www.flickr.com/" target="_flickr">Flickr</a> user&#8217;s latest images into a Wave. It was super easy to write. Gadgets are basically JavaScript and HTML wrapped in some XML. So whipping up a Rails app that fetched Flickr images via the Flickr API and rendered some HTML to display them was cake. I deployed the Rails app/Gadget to heroku&#8230; check it out here: <a href="http://flickr-wave.heroku.com/" target="_gadget">http://flickr-wave.heroku.com/</a></p>
<p>There seems to be some bugs, for some reason after embedding the Gadget and reloading the wave SOMETIMES it won&#8217;t load the Gadget. I don&#8217;t know why&#8230; and I probably won&#8217;t worry about fixing it unless I start getting excited about Google Wave again. Ohh well, check out the source here: <a href="http://github.com/benjaminleesmith/Flickr-Wave" target="_gadget">http://github.com/benjaminleesmith/Flickr-Wave</a></p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2009/10/21/flickr-gadget-for-google-wave/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ruby: the scope of ARGV</title>
		<link>http://disjointthoughts.com/2007/09/04/ruby-the-scope-of-argv/</link>
		<comments>http://disjointthoughts.com/2007/09/04/ruby-the-scope-of-argv/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 05:12:35 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/2007/09/04/ruby-the-scope-of-argv/</guid>
		<description><![CDATA[So it turns out that ARGV has global scope. This is completely fine 99% of the time, but when you&#8217;re trying to do something funky [...]]]></description>
			<content:encoded><![CDATA[<p>So it turns out that ARGV has global scope. This is completely fine 99% of the time, but when you&#8217;re trying to do something funky this can bite you in the ass. Here&#8217;s an example of what made me scratch my head. Script I want to run:</p>
<pre style="width:500px;height:200px;border:solid 1px #ccc;overflow:scroll;">
puts 'arguments passed in:'
ARGV.each do |arg|
  puts arg
end

puts "\\ndoing require\\n"

require 'arg_fucker'

puts 'arguments after require:'
ARGV.each do |arg|
  puts arg
end
</pre>
<p>&#8230; and here&#8217;s arg_fucker.rb:</p>
<pre style="width:500px;height:70px;border:solid 1px #ccc;overflow:scroll;">
ARGV.delete_at(0)
</pre>
<p>When I run this script, as you&#8217;d probably expect, the first element of ARGV gets deleted:</p>
<pre style="width:500px;height:200px;border:solid 1px #ccc;overflow:scroll;">
>ruby test_runner.rb a b c d
arguments passed in:
a
b
c
d

doing require
arguments after require:
b
c
d
</pre>
<p>This behavior is fine, unless you&#8217;re someone like me who is trying to make modules into runnable scripts where the first argument is the method to be run. And you&#8217;re including one module inside another. &lt;shoots-self/&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2007/09/04/ruby-the-scope-of-argv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>example of using hpricot, rexml, and kml</title>
		<link>http://disjointthoughts.com/2007/08/16/example-of-using-hpricot-rexml-and-kml/</link>
		<comments>http://disjointthoughts.com/2007/08/16/example-of-using-hpricot-rexml-and-kml/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 17:39:54 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[google maps]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[the great outdoors]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/2007/08/16/example-of-using-hpricot-rexml-and-kml/</guid>
		<description><![CDATA[I wrote a little Ruby script to parse some hot spring data and throw it into a kml file for display in google maps or [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a little Ruby script to parse some <a href="http://www.hotspringsenthusiast.com/TextLinks.asp" target="_new">hot spring data</a> and throw it into a <a href="http://code.google.com/apis/kml/documentation/" target="_new">kml file</a> for display in google maps or google earth (<a href="http://disjointthoughts.com/hot-spring-maps/" target="_new">here&#8217;s the results</a>). It doesn&#8217;t do anything too complex, but it makes for a decent example of using <a href="http://code.whytheluckystiff.net/hpricot/" target="_new">hpricot</a> to parse html and <a href="http://www.germane-software.com/software/rexml/" target="_new">rexml</a> to generate kml. Here&#8217;s the source if anyone would like to take a look:</p>
<pre style="width:500px;height:400px;border:solid 1px #ccc;overflow:scroll;">
#written by Benjamin Smith
#www.disjointthoughts.com
#benjamin.lee.smith@gmail.com

require 'hpricot'
require 'open-uri'
require 'rexml/document'
require 'erb'

#arg: url to parse from http://www.hotspringsenthusiast.com/TextLinks.asp
#doesnt work for arkansas, georgia, new york, north carolina, south dakota, virgina
url = ARGV[0]

#parse name of state out of url
state = url[url.index('.com')+5..url.length-5]

#grab the html
doc = Hpricot(open(url))

#create xml for the kml file
xml = REXML::Document.new
kml = xml.add_element 'kml', {'xmlns' => 'http://earth.google.com/kml/2.1'}
kml_doc = kml.add_element 'Document'

#add the name of the state to the xml
(kml_doc.add_element 'name').text = "#{state} Hot Springs"

#add reference to me
(kml_doc.add_element 'description').text = 'created by Benjamin Smith http://www.disjointthoughts.com'
doc_folder = kml_doc.add_element 'Folder'

#add reference to the data source
(doc_folder.add_element 'name').text = 'http://www.hotspringsenthusiast.com/'
(doc_folder.add_element 'description').text = "data source #{url}"

#iterate over the rows in the table
doc.search('//tr').each do |tr|
  tds = tr.search('//td')
  if tds.first.inner_html != 'STATE'
    link = tds[3].search('//a').to_s
    lat = link[link.index('lat=')+4..link.index('&#038;',link.index('lat='))-1]
    lon = link[link.index('lon=')+4..link.index('"',link.index('lon='))-1].gsub('E','')
    topo = link[link.index('href="')+6..link.index('"',link.index('href="')+7)-1] 

    placemark = doc_folder.add_element 'Placemark'
    (placemark.add_element('name')).text = tds[3].search('//a').inner_html.to_s.gsub("\r\n",'').squeeze.downcase
    (placemark.add_element('description')).text = REXML::CData.new('Temperature: '+tds[4].inner_html+'F/'+tds[5].inner_html+'C&lt;br/>&lt;a href="'+topo+'">Topo</a>')
    point = placemark.add_element 'Point'
    (point.add_element 'coordinates').text = "#{lon.to_s},#{lat.to_s}"
  end
end

f = File.new("#{state.downcase}.kml",'w')
f.write('<?xml version="1.0" encoding="UTF-8"?>'+"\n")
xml.write(f,4)
f.close

#create html file to display kml in google map
erb = ERB.new(File.read('template_hot_springs.erb'))
f = File.new("#{state.downcase}_hot_springs.html",'w')
f.write(erb.result(binding))
f.close
</pre>
<p>If you&#8217;d like to run it on your own, you can <a href="http://disjointthoughts.com/static_blog_files/hotsprings/converter.rb" target="_new">download it here</a>. You&#8217;ll also need <a href="http://disjointthoughts.com/static_blog_files/hotsprings/template_hot_springs.erb" target="_new">this template file</a> and <a href="http://disjointthoughts.com/static_blog_files/hotsprings/map.css" target="_new">this css file</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2007/08/16/example-of-using-hpricot-rexml-and-kml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rails: getting ip address from request</title>
		<link>http://disjointthoughts.com/2007/06/07/rails-getting-ip-address-from-request/</link>
		<comments>http://disjointthoughts.com/2007/06/07/rails-getting-ip-address-from-request/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 15:29:17 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=171</guid>
		<description><![CDATA[This is useful if you want to log the ip address of the person using your rails app (except when the person is behind a [...]]]></description>
			<content:encoded><![CDATA[<p>This is useful if you want to log the ip address of the person using your rails app (except when the person is behind a proxy, thanks <a href="http://www.jamesbritt.com/" target="new">James</a>!):<br />
<code><br />
  request.env['REMOTE_ADDR']<br />
</code><br />
Taken from <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238784" target="new">this ruby thread</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2007/06/07/rails-getting-ip-address-from-request/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ruby: variable scope and passing blocks to methods</title>
		<link>http://disjointthoughts.com/2007/03/21/ruby-variable-scope-and-passing-blocks-to-methods/</link>
		<comments>http://disjointthoughts.com/2007/03/21/ruby-variable-scope-and-passing-blocks-to-methods/#comments</comments>
		<pubDate>Thu, 22 Mar 2007 05:56:49 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=145</guid>
		<description><![CDATA[Here&#8217;s the latest bit of Ruby that had me scratching my head for a minute&#8230; Say you have a method that takes a block of [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the latest bit of Ruby that had me scratching my head for a minute&#8230;</p>
<p>Say you have a method that takes a block of code, and this block of code is executed using yield inside of the method:</p>
<blockquote><pre style="font-size:1.2em">
def foo
  yield
end

foo { puts "boobs"}
</pre>
</blockquote>
<p>Now let&#8217;s say that block needs to access some variable defined inside of that method:</p>
<blockquote><pre style="font-size:1.2em">
def foo
  n = 'boobs'
  yield
end

foo { puts "n: #{n}"}
</pre>
</blockquote>
<p>This doesn&#8217;t work, you end up with &#8220;undefined local variable or method `n&#8217; for main:Object (NameError)&#8221;. But if n is defined right before the call to method foo it works:</p>
<blockquote><pre style="font-size:1.2em">
def foo
  n = 'boobs'
  yield
end

n = 'titties'
foo { puts "n: #{n}"}
</pre>
</blockquote>
<p>This, as expected, will output &#8220;n: titties&#8221;. So how can you use n from within the method inside of the block? Like so:</p>
<blockquote><pre style="font-size:1.2em">
def foo
  n = 'boobs'
  yield n
end

n = 'titties'
foo { |n| puts "n: #{n}"}
</pre>
</blockquote>
<p>The result of running this code is &#8220;n: boobs&#8221;. You give the variable n as an argument to yield, and add &#8220;|n|&#8221; to the beginning of the block. At times blocks still feel foreign to me, but I think I&#8217;m starting to get the hang of them.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2007/03/21/ruby-variable-scope-and-passing-blocks-to-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>using negate in ruby regex</title>
		<link>http://disjointthoughts.com/2007/03/02/using-negate-in-ruby-regex/</link>
		<comments>http://disjointthoughts.com/2007/03/02/using-negate-in-ruby-regex/#comments</comments>
		<pubDate>Fri, 02 Mar 2007 17:04:50 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=143</guid>
		<description><![CDATA[In Ruby regular expressions, the ^ character is used to denote the beginning of the String. The the following&#8230; a = 'abcde' s.match(/^a/) &#8230;returns true/makes [...]]]></description>
			<content:encoded><![CDATA[<p>In Ruby regular expressions, the ^ character is used to denote the beginning of the String. The the following&#8230;</p>
<blockquote>
<pre style="font-size:1.2em">
a = 'abcde'
s.match(/^a/)
</pre>
</blockquote>
<p>&#8230;returns true/makes a match because the string s begins with the character &#8220;a&#8221;. But if you want to match on all characters that are not &#8220;a&#8221;, how do you do it?</p>
<blockquote>
<pre style="font-size:1.2em">
s.match(/[^a]/)
</pre>
</blockquote>
<p>Once ^a is placed inside of the square brackets it becomes a character class where the ^ character acts as negation rather than beginning-of-string.</p>
<p>I&#8217;m sure most people who pay attention picked this up the first time reading through a Ruby book, but I don&#8217;t pay 100% attention all the time and this minor detail slipped by.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2007/03/02/using-negate-in-ruby-regex/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>file locking in ruby using flock</title>
		<link>http://disjointthoughts.com/2007/01/15/file-locking-in-ruby-using-flock/</link>
		<comments>http://disjointthoughts.com/2007/01/15/file-locking-in-ruby-using-flock/#comments</comments>
		<pubDate>Mon, 15 Jan 2007 23:54:22 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=134</guid>
		<description><![CDATA[I was writing a bit a ruby code that used flock to write and read from a file using two different processes. I ran into [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a bit a ruby code that used flock to write and read from a file using two different processes. I ran into some funny issues, so I&#8217;m going to try to explain the process I went through to reach my final working code.</p>
<p>First let&#8217;s say I have a two ruby processes, one is writing a small amount of text to a file while the other is reading from that file. For example the process doing the writing looks like:</p>
<blockquote>
<pre style="font-size:1.2em">for i in 0..1000 do
  puts i.to_s
  file = File.new('shared_file','w')
  file.write("line1\\nline2")
  file.close
end</pre>
</blockquote>
<p>&#8230;and the process reading the file looks like:</p>
<blockquote><pre style="font-size:1.2em">for i in 0..1000 do
  puts i.to_s
  file = File.new('shared_file','r')
  lines = file.readlines
  if(lines[0]!="line1\\n")
    puts("got #{lines[0]} instead\\n")
    exit
  end
  file.close
end</pre>
</blockquote>
<p>Unfortunately when these two chunks of code run at the same time I end up with a message something like..</p>
<blockquote><pre style="font-size:1.2em">file_writer.rb:3:in `initialize': Text file busy - shared_file (Errno::ETXTBSY)</pre>
</blockquote>
<p>This message comes from the process doing the writing. So I think I can positively say that even though I&#8217;m only writing a small amount of text, the file needs to be locked in order for both processes to run correctly.</p>
<p>So the next step was to test out the flock File method using a couple of irb sessions. In irb I am able to lock a file by doing the following:</p>
<blockquote><pre style="font-size:1.2em">irb(main):001:0> (file = File.new('shared_file','w')).flock(File::LOCK_EX)
=> 0
irb(main):002:0></pre>
</blockquote>
<p>When I try to open the same file for reading in the second irb session I get:</p>
<blockquote><pre style="font-size:1.2em">irb(main):001:0> (file = File.new('shared_file','r')).flock(File::LOCK_EX)</pre>
</blockquote>
<p>&#8230;this will hang here until I unlock the file from the first irb session. So using this approach to locking the file I can update my writing code:</p>
<blockquote><pre style="font-size:1.2em">for i in 0..1000 do
  puts i.to_s
  (file = File.new('shared_file','w')).flock(File::LOCK_EX)
  file.write("line1\\nline2")
  file.flock(File::LOCK_UN)
  file.close
end</pre>
</blockquote>
<p>&#8230; and my reading code &#8230;</p>
<blockquote><pre style="font-size:1.2em">for i in 0..1000 do
  puts i.to_s
  (file = File.new('shared_file','r')).flock(File::LOCK_EX)
  lines = file.readlines
  if(lines[0]!="line1\\n")
    puts("got #{lines[0]} instead\\n")
    exit
  end
  file.flock(File::LOCK_UN)
  file.close
end</pre>
</blockquote>
<p>Now I would expect this to work, but it doesn&#8217;t behave quite as I&#8217;d expect. Sometimes the output of my reader is:</p>
<blockquote><pre style="font-size:1.2em"># ruby file_reader.rb
0
got  instead</pre>
</blockquote>
<p>When I get this output from my reader, the writer continues on its merry way until it terminates. Now this output is saying that instead of reading &#8220;line1&#8243; from the file, it read nothing or an empty string. This is weird since the writer is always writing the same thing and the file should always contain &#8220;line1\nline2&#8243; Other times once I start the reader the writer spits out:</p>
<blockquote><pre style="font-size:1.2em">file_writer.rb:3:in `initialize': Text file busy - shared_file (Errno::ETXTBSY)</pre>
</blockquote>
<p>This output is also strange, since this is the sort of error I was getting before I added the file locking code. This would lead me to believe that the file is actually not getting locked even though it worked in irb.</p>
<p>So what&#8217;s going on? I&#8217;m not exactly sure, but I found <a target="_blank" href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/24277">this thread</a> from someone else who seemed to be having a similar problem. The solution that was purposed included:</p>
<blockquote><p>&#8220;Also, opening files for writing and truncating them may introduce subtle<br />
problems &#8211; you cannot lock the file before it&#8217;s truncated, so you need to<br />
try open it in read-write mode, then lock and then truncate it&#8230;&#8221;</p></blockquote>
<p>So I tried updated my writing code:</p>
<blockquote><pre style="font-size:1.2em">for i in 0..1000 do
  puts i.to_s
  (file = File.new('shared_file','r+')).flock(File::LOCK_EX)
  file.truncate 0
  file.write("line1\nline2")
  file.flock(File::LOCK_UN)
  file.close
end</pre>
</blockquote>
<p>And what do you know, it works. I&#8217;m not sure why, but opening the file using &#8220;r+&#8221; (read-write) instead of just &#8220;w&#8221; (write) fixes all of the problems that I was seeing previously. I&#8217;m not sure if this is the only or best solution to this problem, but this is the only one I could come up with while still using flock to lock the file.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2007/01/15/file-locking-in-ruby-using-flock/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>desert code camp</title>
		<link>http://disjointthoughts.com/2006/10/26/desert-code-camp/</link>
		<comments>http://disjointthoughts.com/2006/10/26/desert-code-camp/#comments</comments>
		<pubDate>Thu, 26 Oct 2006 23:55:25 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[geek community]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://disjointthoughts.com/?p=118</guid>
		<description><![CDATA[The second Desert Code Camp is coming up this weekend. &#8220;Code Camp is a free, one-day event put on by the local Phoenix community to [...]]]></description>
			<content:encoded><![CDATA[<p>The second <a target="_blank" href="http://www.desertcodecamp.com/">Desert Code Camp</a> is coming up this weekend.</p>
<blockquote><p>&#8220;Code Camp is a free, one-day event  						put on by the local Phoenix community to help promote software development in  						general.  There is no right or wrong language, platform, or  						technology.  If a topic relates in any way to the code that causes a  						machine to produce a desired result, it&#8217;s welcome here.&#8221;</p></blockquote>
<p>The previous code camp was ok, but the sessions were too jammed together. There was no time to meet people and just talk. Everyone kept running from session to session trying to keep up. Some of the sessions were good, while others were a waste of my time.</p>
<p>I&#8217;ll be attending again, hopefully they&#8217;ve tweaked the schedule enough so it&#8217;s no so jam-packed. There aren&#8217;t any sessions that are must-attends for me, so I&#8217;ll mostly be there to hang out and support the community. If nothing else, at least it&#8217;s a free lunch.</p>
<p>Edit:  Apparently lunch isn&#8217;t free.</p>
]]></content:encoded>
			<wfw:commentRss>http://disjointthoughts.com/2006/10/26/desert-code-camp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

