<?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>James Gregory &#187; Nuggets</title>
	<atom:link href="http://jagregory.com/writings/category/nuggets/feed/" rel="self" type="application/rss+xml" />
	<link>http://jagregory.com</link>
	<description>Monkeying with the code</description>
	<lastBuildDate>Tue, 22 Jun 2010 11:07:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Console colours wrapper</title>
		<link>http://jagregory.com/writings/console-colours-wrapper/</link>
		<comments>http://jagregory.com/writings/console-colours-wrapper/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 19:56:04 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=109</guid>
		<description><![CDATA[Continuing on from my post about an alternative syntax for the non-disposable using statements, here&#8217;s a class I&#8217;ve been using lately. It serves as a wrapper around changing the colours in a console window. It&#8217;s not a difficult thing to do, it&#8217;s just a bit awkward because you have to maintain the original colour in [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing on from my post about an <a href="http://blog.jagregory.com/2008/10/07/alternative-to-abusing-using/">alternative syntax for the non-disposable using statements</a>, here&#8217;s a class I&#8217;ve been using lately. It serves as a wrapper around changing the colours in a console window. It&#8217;s not a difficult thing to do, it&#8217;s just a bit awkward because you have to maintain the original colour in a variable while you do your business.</p>
<pre name="code" class="c-sharp">
Console.WriteLine("Start...")

var originalColour = Console.ForegroundColor;
Console.ForegroundColour = ConsoleColor.Red;

Console.WriteLine("WARNING!");

Console.ForegroundColour = originalColour;
</pre>
<p>It&#8217;s not too bad when you&#8217;re only doing it once, but when you start swapping colours all over the place, then it can become very noisy. So this is where my class comes into play. Using the same syntax I described in my previous post, I&#8217;ve wrapped up the colour changing in a helper method that takes an Action delegate. This allows you to write much more intention revealing code.</p>
<pre name="code" class="c-sharp">
ConsoleColours.Foreground(ConsoleColor.Yellow, () =>
{
  Console.WriteLine("Different text");
});
</pre>
<p>I find this much cleaner and the blocks gives my console code some much needed separation.</p>
<p>Here&#8217;s the full class code:</p>
<pre name="code" class="c-sharp">
public class ConsoleColours
{
  public static void Foreground(ConsoleColor colour, Action action)
  {
    var original = Console.ForegroundColor;
    Console.ForegroundColor = colour;

    action();

    Console.ForegroundColor = original;
  }

  public static void Background(ConsoleColor colour, Action action)
  {
    var original = Console.BackgroundColor;
    Console.BackgroundColor = colour;

    action();

    Console.BackgroundColor = original;
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/console-colours-wrapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternative to abusing using</title>
		<link>http://jagregory.com/writings/alternative-to-abusing-using/</link>
		<comments>http://jagregory.com/writings/alternative-to-abusing-using/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 19:26:24 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Nuggets]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=107</guid>
		<description><![CDATA[There&#8217;s been a bit of discussion of late about using statements, and how they&#8217;re more often being used for purposes other than just releasing resources. As always, there are those people who think it&#8217;s a flagrant abuse of a feature and shouldn&#8217;t be done, then there are those that like it. I&#8217;m in between. I [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s been a bit of discussion of late about using statements, and how they&#8217;re more often being used for purposes other than just releasing resources. As always, there are those people who think it&#8217;s a flagrant abuse of a feature and shouldn&#8217;t be done, then there are those that like it. I&#8217;m in between. I do like what the using statement gives us, but I also think it is a bit of an abuse.</p>
<p>The &#8220;traditional&#8221; usage of the using statement can be found quite often in the land of files and streams. Take the following example, which opens a file and then closes it when it drops out of the using scope.</p>
<pre name="code" class="c-sharp">
using (var stream = File.OpenRead("myFile.txt"))
{
  // do something with the file
}
</pre>
<p>Examples of the alternative usage can be found all over the place, but Rhino Mocks is one that&#8217;s close to my heart. Here&#8217;s from the record/replay syntax, anything in the scope of the using is recorded, and once it drops out of scope it&#8217;s no longer in record mode.</p>
<pre name="code" class="c-sharp">
using (mocks.Record())
{
  Expect.Call(customer.Address)
    .Return("123 Rester St");
}
</pre>
<p>Again, I do like what the using statement gives us outside of releasing resources (I&#8217;m not disputing it&#8217;s usefulness there). However, I think the using keyword itself adds noise and clouds intention.</p>
<p>With the adoption of 3.5, I&#8217;ve started using an alternative syntax instead of usings. Actions and anonymous methods to the rescue.</p>
<pre name="code" class="c-sharp">
Scope(() =>
{
  // do something within this scope
});
</pre>
<p>It&#8217;s a little bit more noisy in the compiler satisfying department, but because you have full control over naming, you can reveal intention more. No more unclear &#8220;using&#8221;.</p>
<p>So how does it work? Simple really, the method takes an Action delegate, which it the executes almost immediately. I say almost, because you can execute code before and after the execution. That gives you the benefits of the using statements wrapping ability.</p>
<pre name="code" class="c-sharp">
public void Scope(Action action)
{
  // do something before
  action();
  // do something after
}
</pre>
<p>Some more examples:</p>
<pre name="code" class="c-sharp">
File.OpenRead("myFile.txt", file =>
{
  // do something with the file
});
</pre>
<pre name="code" class="c-sharp">
mocks.Record(() =>
{
  Expect.Call(customer.Address)
    .Return("123 Rester St");
});
</pre>
<p>I prefer this syntax over the using statement. Of course, it&#8217;s only valid for 3.5 projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/alternative-to-abusing-using/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>JavaScript Console in Safari</title>
		<link>http://jagregory.com/writings/javascript-console-in-safari/</link>
		<comments>http://jagregory.com/writings/javascript-console-in-safari/#comments</comments>
		<pubDate>Mon, 15 May 2006 14:31:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=12</guid>
		<description><![CDATA[Just a quick tip to enable the Debug menu in Safari, the main reason being to get the JavaScript Console (ala Firefox).
Simply close down your open instances of Safari and enter this into the Terminal:
defaults write com.apple.Safari IncludeDebugMenu 1
That’ll enable a new Debug menu next time you start Safari, simple as that. I was on [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick tip to enable the Debug menu in Safari, the main reason being to get the JavaScript Console (ala Firefox).</p>
<p>Simply close down your open instances of Safari and enter this into the Terminal:</p>
<pre><code>defaults write com.apple.Safari IncludeDebugMenu 1</code></pre>
<p>That’ll enable a new Debug menu next time you start Safari, simple as that. I was on the verge of loading up Firefox on the Mac<br />
before I came across this, bad James!</p>
<p>Also, just in-case, running the above again with 0 will turn off the menu.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/javascript-console-in-safari/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails Recognition Failed in Production mode</title>
		<link>http://jagregory.com/writings/ruby-on-rails-recognition-failed-in-production-mode/</link>
		<comments>http://jagregory.com/writings/ruby-on-rails-recognition-failed-in-production-mode/#comments</comments>
		<pubDate>Sat, 29 Apr 2006 07:56:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Nuggets]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=13</guid>
		<description><![CDATA[The title pretty much covers it, but i’ll elaborate.
A few days ago I decided it was about time I had a tinker with Google Sitemaps, part of which involves uploading a temporary file so google can be sure the website you’re trying to claim is actually yours. Anyway, fun things aside, google gave me an [...]]]></description>
			<content:encoded><![CDATA[<p>The title pretty much covers it, but i’ll elaborate.</p>
<p>A few days ago I decided it was about time I had a tinker with <a href="http://www.google.com/webmasters/sitemaps">Google Sitemaps</a>, part of which involves uploading a temporary file so google can be sure the website you’re trying to claim is actually yours. Anyway, fun things aside, google gave me an error message (“We’ve detected that your 404 (file not found) error page returns a status of 200 (OK) in the header.”) when it tried to read the file.</p>
<p>The error message was caused by a feature of Ruby on Rails, which when in Development mode, invalid pages are still served by Rails and sent with a 200 header, but logged as having a Routing error of “Recognition failed”.</p>
<p>All information pointed to a simple fix of setting the rails environment (<code>ENV['RAILS_ENV']</code>) to production mode, which would disable the aforementioned feature. Unfortunately for me, my environment was <em>already</em> set to production! I double checked this, logged into the ruby console on my web-server and checked the variables, then even checked which log file was being written to; everything was pointing to it being in production mode.</p>
<p>After a lot of browsing of forums and chatting in IRC channels, I succumbed to randomly trying anything, which is when i noticed in my environment.rb file there was a line of code that I couldn’t for the life of me remember why it was in there, or what it was doing; removing this line of code worked a treat, everything snapped back to how it should be functioning.</p>
<p>For future reference, the line in question was:</p>
<pre><code>require 'error_handler_basic' # defines AC::Base#rescue_action_in_public
</code></pre>
<p>So there you go, now I have a 404 page and google accepted my site map, so we can all live happily ever after.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/ruby-on-rails-recognition-failed-in-production-mode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uncrippling Windows XP&#8217;s IIS 5.1</title>
		<link>http://jagregory.com/writings/uncrippling-windows-xps-iis-51/</link>
		<comments>http://jagregory.com/writings/uncrippling-windows-xps-iis-51/#comments</comments>
		<pubDate>Wed, 26 Apr 2006 08:35:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=15</guid>
		<description><![CDATA[I apparently got around to reading Coding Horror a bit too late, as I missed this gem. It’s an article about a small application used to bend XP’s IIS to your every whim, perfect for those of us suffering at the hands of Microsoft.
The basic gist of the application is that it removes the limits [...]]]></description>
			<content:encoded><![CDATA[<p>I apparently got around to reading <a href="http://www.codinghorror.com">Coding Horror</a> a bit too late, as I missed <a href="http://www.codinghorror.com/blog/archives/000329.html" title="Uncrippling Windows XP's IIS 5.1">this gem</a>. It’s an article about a small application used to bend XP’s IIS to your every whim, perfect for those of us suffering at the hands of Microsoft.</p>
<p>The basic gist of the application is that it removes the limits of XP’s IIS, namely the problem of only being allowed one website and 10 concurrent users. It takes a bit of getting used to, especially if you’re using Visual Studio; if you are, remember to reset IIS to it’s default website before opening a website created prior to using IISAdmin.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/uncrippling-windows-xps-iis-51/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trust Wireless Scroll Tablet with Dual Monitors</title>
		<link>http://jagregory.com/writings/trust-wireless-scroll-tablet-ampamp-dual-monitors/</link>
		<comments>http://jagregory.com/writings/trust-wireless-scroll-tablet-ampamp-dual-monitors/#comments</comments>
		<pubDate>Tue, 07 Mar 2006 19:48:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=23</guid>
		<description><![CDATA[This was a task and a half! As I mentioned earlier I somehow managed to get my Trust Wireless Scroll Tablet working on a dual monitor system, mapped to only the primary monitor; so here’s my little guide on how I managed it.
As far as I am aware there isn’t an option in the trust [...]]]></description>
			<content:encoded><![CDATA[<p>This was a task and a half! As I mentioned earlier I somehow managed to get my <a href="http://www.amazon.co.uk/exec/obidos/ASIN/B0002DCL6G/202-4350795-3732661">Trust Wireless Scroll Tablet</a> working on a dual monitor system, mapped to only the primary monitor; so here’s my little guide on how I managed it.</p>
<p>As far as I am aware there isn’t an option in the trust software to allow you to map to a specific monitor, only to restrict the area on the tablet (which isn’t at all helpful). So what I’ve figured out is purely a work around, and it isn’t great either<sup>1</sup>. This is mainly for my reference, but someone else might find it useful, so here we go:</p>
<ol>
<li>Open up Display Properties and disable (“un-attach”) your secondary monitor.</li>
<li>Open the Trust Control Panel from the icon in your system tray.</li>
<li>Re-enable your second monitor.</li>
<li>Click ok in your Trust Control Panel.</li>
</ol>
<p>From there on out your tablet should only be mapped to the monitor that stayed enabled the whole time, that is until you re-open the Trust Control Panel. I didn’t say it was pretty!</p>
<p>What I can gather is that after disabling your monitor, when you open up the control panel it maps the overall screen size so it can apply that to the tablet, but when you click OK after re-enabling the monitor it doesn’t refresh with the screen change and so uses the single monitor values.</p>
<p><sup>1</sup> Also note that I have only tested this on my system.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/trust-wireless-scroll-tablet-ampamp-dual-monitors/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Venkman for Firefox 1.5.0.1 (Javascript Debugger)</title>
		<link>http://jagregory.com/writings/venkman-for-firefox-1501-javascript-debugger/</link>
		<comments>http://jagregory.com/writings/venkman-for-firefox-1501-javascript-debugger/#comments</comments>
		<pubDate>Tue, 07 Feb 2006 21:49:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=26</guid>
		<description><![CDATA[Once again the guys at Mozilla have broken quite possibly the best plug-in for Firefox: the Venkman Javascript Debugger. This is becoming quite commonplace, with every update for firefox disabling the debugger until a fix is released.
Anyway, even though both the extensions manager and Venkmans official website both say there aren’t any updates available, Joe [...]]]></description>
			<content:encoded><![CDATA[<p>Once again the guys at Mozilla have broken quite possibly the best plug-in for Firefox: the <a href="http://www.mozilla.org/projects/venkman/">Venkman Javascript Debugger</a>. This is becoming quite commonplace, with every update for firefox disabling the debugger until a fix is released.</p>
<p>Anyway, even though both the extensions manager and Venkmans official website both say there aren’t any updates available, Joe Walker from <a href="http://getahead.ltd.uk/home">Getahead</a> has created and update that contains some bug fixes from Bugzilla, but most importantly Venkman now works with Firefox 1.5.0.1!</p>
<p>Source: <a href="http://getahead.ltd.uk/ajax/venkman">Venkman for Firefox 1.5</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/venkman-for-firefox-1501-javascript-debugger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DateDiff() minus one VisualBasic assembly</title>
		<link>http://jagregory.com/writings/datediff-minus-one-visualbasic-assembly/</link>
		<comments>http://jagregory.com/writings/datediff-minus-one-visualbasic-assembly/#comments</comments>
		<pubDate>Fri, 16 Sep 2005 23:11:00 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Nuggets]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=36</guid>
		<description><![CDATA[I’m not a big fan of using the Microsoft.VisualBasic assembly, I avoid it at all costs basically.
As we all know, the visual basic assembly comes with a large collection of built-in functions one of which is the DateDiff function. This takes two dates and a comparison value (e.g. “d” for day) and then spits out [...]]]></description>
			<content:encoded><![CDATA[<p>I’m not a big fan of using the Microsoft.VisualBasic assembly, I avoid it at all costs basically.</p>
<p>As we all know, the visual basic assembly comes with a large collection of built-in functions one of which is the DateDiff function. This takes two dates and a comparison value (e.g. “d” for day) and then spits out the difference.</p>
<p>Here’s the same thing just minus the visual basic usage:</p>
<pre><code>DateTime firstDate = DateTime.Now;
DateTime secondDate = new DateTime(2005, 8, 20);
TimeSpan difference = secondDate.Subtract(firstDate);

// days: difference.Days</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/datediff-minus-one-visualbasic-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
