Archive for 2010
November 28th, 2010
While running rake spec should you happen to run across a stack trace including...
backward_compatibility.rb:20:in `const_missing'
... it could be for a number of reasons, but in my case, it was because I had accidentally named a model plural then manually renamed it without renaming the *_spec.rb file as well.
November 23rd, 2010
I’ve recently begun doing more work with Ruby on Rails. I’ve dabbled a bit with it here and there and enjoyed it but it never really took hold with me. The biggest obstacle for me was simply just trying to keep on top of all the myriad new things that seemed to emerge daily. With the Rails 3.0 launch I finally decided that the time has come and that I would do some work with Rails.
If you’ve done a fair amount of C#/.NET development and you’re looking for something to excite the programmer’s spark within you and you haven’t already experienced Ruby then right now is a really good time to start. (By “right now” I am referring to post Rails 3 release.) Here are some of the resources I’ve used to begin with, ordered in what I feel is most important to me as a new Ruby/Rails programmer first:
OS X
Sure you could do Ruby development on Windows and use RubyMine but my experience with this approach has been sketchy at best. I haven’t been a big fan of Windows on the server, its just something I’ve had to live with as a C# developer writing web-based applications. I’ve had a MacBook Pro sitting around for several years (an Intel Core2 Duo) which really does fit the bill for Ruby development and the operating system (OS X, of course) is just better than Windows for developing software on. (Especially if that software is Ruby.) I have a decent amount of experience using Linux on the server and all of that knowledge translates directly into using OS X’s BSD-flavor *nix. Acceptable MBPs can be found on eBay in the $600-$800 range used. Then there is also Linux on the desktop, of which I’ve never been a big fan, but its there and you could use it to get your Windows box running a good Ruby development environment today with no additional hardware commitments. Why do I suffer the Apple tax? Because…
TextMate
TextMate for writing code to me is like painting with a high quality brush. It takes a fun activity and makes it much more pleasurable (dare I say… relaxing?). The alternative is VIM which I haven’t yet given a shot but I am assured it is wonderful as well (and has the bonus of running on Linux). To get started:
http://projects.serenity.de/textmate/tutorials/basics/
The TextMate Manual is also a great resource. I bought a copy of TextMate: Power Editing for the Mac and I’m about 1/3rd of the way through it; you could probably get by with online-only free resources but having the book speeds that discovery process up a bit.
For VIM there is vimcasts.org.
Git
There’s no two ways about it: to do Rails development you’re going to need to learn about git. And get a github.com account (the free one works just fine). My experience moving from svn to git has been sublime. I long for the day when we have enough time to migrate our daily work environment from svn to git.
http://crypto.stanford.edu/~blynn/gitmagic/ch01.html
PDF download: http://crypto.stanford.edu/~blynn/gitmagic/book.pdf)
Ruby5
So the biggest confusion I had in 2009 when I was trying to learn Rails was trying to keep up with the barrage of constant new stuff. Ruby5 is a podcast released twice a week and runs under ten minutes with details about new things primarily for Rails. This is what I needed back in 2009 and what I have now in 2010 to combat the dreadful new hotness tax.
http://ruby5.envylabs.com/
Ruby Best Practices
This is an O’Reilly published book which is also free as a PDF:
http://sandal.github.com/rbp-book/pdfs/rbp_1-0.pdf
The thing is, being an experienced software developer, I’m able to grok Ruby pretty quickly. The PickAxe book (which is $10 for either the print or PDF version, or $20 for both at the time of this writing; anniversary sale) is great for fleshing out all of the nooks and crannies about Ruby but Ruby Best Practices has got the BRAWNDO my inner programmer craves.
PragProg
My favorite publisher produces a lot of high quality technical books with a great focus on Ruby. They have a 40% off Black Friday deal going on for November 26th 2010 PST (use the coupon code “turkey” before selecting payment method). I recommend:
Agile Web Development with Rails (4th edition) (4th edition is being updated for Rails 3 and available as a beta download)
The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends (yes updated for Rails 3)
Programming Ruby 1.9 (3rd Edition): The Pragmatic Programmers’ Guide (PickAxe) (You won’t get another 40% off this since its already $10.)
It also helps that they have an excellent website and they make it very easy for you to re-download your purchases from other devices whenever you need them. (Manning does this as well.)
Railscasts
Railscasts are great roughly 10 minute videos demonstrating how to do something with Ruby. If you are coming from a C#/ASP.NET MVC project and curious about how Rails handles a common-yet-complicated problem, have a look at #240: Search, Sort, Paginate with AJAX. Be careful of some of the older content; changes in Rails 3 invalidates some of the techniques used in the screencasts. (I got bit by that when I was working with multiform models.)
Peepcode
Peepcode is a quality pay screen cast with approximately a 1.5 screencast/month release schedule. I bought the $199 annual subscription which I feel was worth the money. The PeepOpen app for OS X is great, it functions like Cmd-T in TextMate and can be used with other text editors. (Think Ctrl+Shift+T in ReSharper to open a file by its name using wildcards.)
November 18th, 2010
I did a little looking around at alternative view engines for ASP.NET MVC 2 and gave Spark a go. My biggest problem is that its not Haml. I was moving some *.aspx views over to *.spark and pretty much its an exercise in replacing <% with { with some XMLish <if> blocks thrown in. (Didn’t we already determine that XML if’s are bad?)
Anyway decided not to use it but I did sort out a problem with 1.1.0.0 (as of November 2010) where ASP.NET MVC 2 Areas simply did not work with the view engine. I suspect this is also the same reason why NHaml doesn’t work with Areas as well.
In the source for Spark in the Spark.Web.Mvc assembly within the DefaultDescriptorBuilder class around line 183 I’ve replaced the methods PotentialViewLocations, PotentialMasterLocations and PotentialDefaultMasterLocations with the following code:
protected virtual IEnumerable<string> PotentialViewLocations(string controllerName, string viewName, IDictionary<string,object> extra)
{
if (extra.ContainsKey("area"))
{
return ApplyFilters(new[]
{
controllerName + "\\" + viewName + ".spark",
"Shared\\" + viewName + ".spark",
"~\\Areas\\" + extra["area"] + "\\Views\\Shared\\" + viewName + ".spark",
"~\\Areas\\" + extra["area"] + "\\Views\\" + controllerName+ "\\" + viewName + ".spark"
}, extra);
}
return ApplyFilters(new[]
{
controllerName + "\\" + viewName + ".spark",
"Shared\\" + viewName + ".spark",
}, extra);
}
protected virtual IEnumerable<string> PotentialMasterLocations(string masterName, IDictionary<string, object> extra)
{
if (extra.ContainsKey("area"))
{
return ApplyFilters(new[]
{
"~\\Areas\\" + extra["area"] + "\\Views\\Layouts\\" + masterName + ".spark",
"~\\Areas\\" + extra["area"] + "\\Views\\Shared\\" + masterName + ".spark",
"Layouts\\" + masterName + ".spark",
"Shared\\" + masterName + ".spark"
}, extra);
}
return ApplyFilters(new[]
{
"Layouts\\" + masterName + ".spark",
"Shared\\" + masterName + ".spark"
}, extra);
}
protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
{
if (extra.ContainsKey("area"))
{
return ApplyFilters(new[]
{
"~\\Areas\\" + extra["area"] + "\\Views\\Layouts\\" + controllerName + ".spark",
"~\\Areas\\" + extra["area"] + "\\Views\\Shared\\" + controllerName + ".spark",
"Layouts\\Application.spark",
"Shared\\Application.spark"
}, extra);
}
return ApplyFilters(new[]
{
"Layouts\\" + controllerName + ".spark",
"Shared\\" + controllerName + ".spark",
"Layouts\\Application.spark",
"Shared\\Application.spark"
}, extra);
}
This doesn’t work with Hanselman’s Mobile Device Capabilities ViewEngine out of the box, you’ll have to do more work for that.
If you don’t want to go through the legwork of patching Spark 1.1.0.0 I’ve zipped up a Release build of the relevant files from my patch.
I had to dig pretty deep for the original solution.
November 5th, 2010
Just discovered this little gem while on a hunt for the Lisp code that is responsible for FORMAT’s ~R. I am over halfway through Practical Common Lisp
now – Successful Lisp looks like a good compliment to it. (And I have been reading Paul Graham’s ANSI Common LISP
in parallel.)
November 1st, 2010
_webRequest is a HttpWebRequest instance, _headers is a WebHeaderCollection from the original HttpWebRequest. I am relaying headers from one request to another, as required by a third party application. Why do I have to write such terrible code?
foreach (var key in _headers.AllKeys)
{
// of course MS would have to fuck this up somehow
var value = _headers[key];
Log.Debug(key + ": " + value);
switch (key.ToUpper())
{
case "ACCEPT": _webRequest.Accept = value; break;
case "REFERER": _webRequest.Referer = value; break;
case "USER-AGENT": _webRequest.UserAgent = value; break;
case "TRANSFER-ENCODING": _webRequest.UserAgent = value; break;
case "DATE":
if (DateTime.TryParse(value, out dt))
_webRequest.Date = dt;
break;
case "IF-MODIFIED-SINCE":
if (DateTime.TryParse(value, out dt))
_webRequest.IfModifiedSince = dt;
break;
case "CONTENT-LENGTH":
int cl;
if (Int32.TryParse(value, out cl))
_webRequest.ContentLength = cl;
break;
default:
_webRequest.Headers.Add(key, _headers[key]);
break;
}
}
It should be what I originally tried:
//_webRequest.Headers.Add(_headers);
This is precisely the sort of code that creates a great dissatisfaction within me when working with the .NET Framework.