<?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>Deployment Zone &#187; Ruby</title>
	<atom:link href="http://www.deploymentzone.com/category/computers/programming/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deploymentzone.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 04:03:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>I&#8217;m Dangerous with Ruby!</title>
		<link>http://www.deploymentzone.com/2009/10/22/im-dangerous-with-ruby/</link>
		<comments>http://www.deploymentzone.com/2009/10/22/im-dangerous-with-ruby/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 04:24:14 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/10/22/im-dangerous-with-ruby/</guid>
		<description><![CDATA[My solution was selected as the winning solution for RubyLearning.com’s RPCFN #2 “Average Arrival Time for A Flight.”
This challenge involved averaging times of the day without the actual day in the context.&#160; When I first started to tackle the problem I thought to myself that this will be very easy.&#160; Then I hit the “no [...]]]></description>
			<content:encoded><![CDATA[<p>My solution was selected as the winning solution for <a href="http://www.rubylearning.com">RubyLearning.com’s</a> <acronym title="Ruby Programming Challenge For Newbies">RPCFN</acronym> #2 “<a href="http://rubylearning.com/blog/2009/10/08/rpcfn-average-arrival-time-for-a-flight-2/">Average Arrival Time for A Flight</a>.”</p>
<p>This challenge involved averaging times of the day without the actual day in the context.&#160; When I first started to tackle the problem I thought to myself that this will be very easy.&#160; Then I hit the “no day” context and realized that this problem was much tougher than I anticipated.&#160; When I finally saw the posted solutions including <a href="https://gist.github.com/4f6807eef49064027a3c">Chris Strom’s</a> (<a href="http://japhr.blogspot.com/">blog</a>) it was like decades old high school math came rushing back to me.&#160; I would have never thought of plotting points on a graph but now that I’ve been exposed I’m certain I will never forget it!</p>
<p>My friend [and commuting body] Matt and I talked through the problem during our drive home as we sat in Virginia I-495 outer loop and I-95S traffic.&#160; He had some ideas about plotting the problem linearly around 0 but ultimately I ended up going with making assumptions about how close the provided times were to midday and midnight.</p>
<pre class="brush: ruby;">require 'time'

SECONDS_IN_DAY = 86400
MIDNIGHT = Time.parse(&quot;12:00AM&quot;).to_i
MIDDAY = Time.parse(&quot;12:00PM&quot;).to_i

def average_time_of_day(times)
  seconds = []
  times.each {|time| seconds &lt;&lt; Time.parse(time).to_i}
  seconds.sort!
  if (seconds.first - MIDNIGHT) &lt; (seconds.last - MIDDAY)
    seconds.map! {|s| s &lt; MIDDAY ? s += SECONDS_IN_DAY : s }
  end
  Time.at(seconds.inject { |sum,n| sum += n }.to_f / seconds.length).strftime(&quot;%I:%M%p&quot;).downcase
end</pre>
<p>&#160;</p>
<p>Gist: <a title="https://gist.github.com/5b371226faf83af50d7e" href="https://gist.github.com/5b371226faf83af50d7e">https://gist.github.com/5b371226faf83af50d7e</a></p>
<p>Interview: <a title="http://rubylearning.com/blog/2009/10/22/charles-feduke-winner-rpcfn-2/" href="http://rubylearning.com/blog/2009/10/22/charles-feduke-winner-rpcfn-2/">http://rubylearning.com/blog/2009/10/22/charles-feduke-winner-rpcfn-2/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/10/22/im-dangerous-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Temp Files and Ruby 1.8.6 on Windows</title>
		<link>http://www.deploymentzone.com/2009/09/29/temp-files-and-ruby-1-8-6-on-windows/</link>
		<comments>http://www.deploymentzone.com/2009/09/29/temp-files-and-ruby-1-8-6-on-windows/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 05:56:30 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/09/29/temp-files-and-ruby-1-8-6-on-windows/</guid>
		<description><![CDATA[Working on the RPCFN: Shift Subtitle I found myself having to work with files input as a stream (or anyway that’s how I wanted to approach the problem; streams are efficient to me).&#160; In order to give my code any sort of unit testing justice I needed to mock the file system.&#160; The challenge expressly [...]]]></description>
			<content:encoded><![CDATA[<p>Working on the <a href="http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/">RPCFN: Shift Subtitle</a> I found myself having to work with files input as a stream (or anyway that’s how I wanted to approach the problem; streams are <em>efficient</em> to me).&#160; In order to give my code any sort of unit testing justice I needed to mock the file system.&#160; The challenge expressly forbids any Ruby gems from being used in the script itself – and maybe by extension the unit tests as well – but I could not see devoting the time necessary to write a mocking framework for the file system.</p>
<p>I found a gem that does precisely what I needed named <a href="http://devver.net/blog/2009/08/unit-testing-filesystem-interaction/">Construct</a>.&#160; Unfortunately there is a <a href="http://redmine.ruby-lang.org/issues/show/1494">bug with Ruby 1.8.6 on Windows</a> in regards to clean up of temp files.&#160; The problem is that when attempting to clean up a Errno::EACCES is raised causing the unit test to fail (or you to write a lot of rescue blocks).</p>
<p>A workaround I came up with was to replace the rmtree method in the Pathname class within my unit test to perform no clean up.&#160; Not the best approach I am sure, but it let me get on with my work.</p>
<pre class="brush: ruby;"># something_test.rb
require &quot;test/unit&quot;
require 'construct'
require 'something'

class Pathname
  # windows has problems with temp files created by Ruby
  # http://redmine.ruby-lang.org/issues/show/1494
  def rmtree
    nil
  end
end

class SomethingTest &lt; Test::Unit::TestCase
   # test methods...
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/09/29/temp-files-and-ruby-1-8-6-on-windows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby: Mocking Kernel Exit</title>
		<link>http://www.deploymentzone.com/2009/09/26/ruby-mocking-kernel-exit/</link>
		<comments>http://www.deploymentzone.com/2009/09/26/ruby-mocking-kernel-exit/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 14:45:03 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/09/26/ruby-mocking-kernel-exit/</guid>
		<description><![CDATA[I’m working on the RubyLearning blog’s Ruby Programming Challenge for Newbies #1 to learn the language – I’ve done a bit with Rails and some admin scripts so I could use the exposure.&#160; Since I love TDD approaching Ruby development through RSpec is only natural, but it was a pain in the ass trying to [...]]]></description>
			<content:encoded><![CDATA[<p>I’m working on the RubyLearning blog’s <a href="http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/">Ruby Programming Challenge for Newbies #1</a> to learn the language – I’ve done a bit with Rails and some admin scripts so I could use the exposure.&#160; Since I love TDD approaching Ruby development through RSpec is only natural, but it was a pain in the ass trying to find how I could have RSpec verify that my program properly exited when certain conditions were met.&#160; <a href="http://stackoverflow.com/questions/1480537/how-can-i-validate-exits-and-aborts-in-rspec">Here’s how I solved it</a>, reposted here:</p>
<pre class="brush: ruby;"># something.rb
class Something
    def initialize(kernel=Kernel)
        @kernel = kernel
    end

    def process_arguments(args)
        @kernel.exit
    end
end

# something_spec.rb
require 'something'
describe Something do
    before :each do
        @mock_kernel = mock(Kernel)
        @mock_kernel.stub!(:exit)
    end

    it &quot;should exit cleanly&quot; do
        s = Something.new(@mock_kernel)
        @mock_kernel.should_receive(:exit)
        s.process_arguments([&quot;-h&quot;])
    end
end</pre>
<p>What I learned was that you can define a constructor with optional arguments (in this case, initialize(kernel=Kernel) and then proceed to use @kernel’s methods instead of the methods that Kernel provides when you do not specify a class instance.&#160; With a properly mocked and stubbed exit method in my spec things operate as expected.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/09/26/ruby-mocking-kernel-exit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
