Monday, June 20, 2011

Making a Navigable Table of Contents in Vim

I love Vim. I never thought I'd say this, seeing as a decade ago I was strictly a notepad/Word guy, but I really like Vim. There is no doubt a learning curve, but it's well worth the effort.

Today I decided to create a simple document with a table of contents with a document in Vim. Text editors like Google Docs provide internal linking structures, but in Vim it's much, much simpler. Simply create a TOC with one item per line like the following:
>>SECTION_1<<
>>SECTION_2<<
    >>SECTION_2_a<<
>>SECTION_3<<
>>SECTION_4<<
    >>SECTION_4_a<<
    >>SECTION_4_b<<
>>SECTION_5<<

Once you create your TOC, it's really easy to navigate about your document. Simply move the cursor to the line in your TOC that you'd like to find in the main document, then hit the asterisk "*" key. Vim will find the closest word following the cursor's current location on the line and then search for the next value in the document that matches that word. You'll also notice I've used underscores in my section headings. The reason for this is that Vim will treat underscores as part of a word, rather than as a delimiting character like a space or hyphen. Does it get any simpler than this?

Cheers,
~Mike

Friday, May 20, 2011

Getting Google Tasks Into Remember the Milk

I was looking for a quick and easy way to import my existing Google Task lists into Remember the Milk today and found that, while fairly easy, you need to jump through a couple small hoops.

First off, Google Calendar and Gmail offer different functionality for Tasks. Originally, I was going to use the "print tasks" action and just copy-paste into an email that I'd send to RTM, but there's an easier way. I usually deal with tasks exclusively through the Calendar and so, initially, I didn't see that they offer an "Email Tasks" Action. You have to access Tasks via Gmail in order to see this action. Basically, you just use your RTM import email address and send the tasks to it. Done.

One thing to note here is that RTM doesn't support subtasks, so if you have any hierarchy built into your Google task list, it will be lost. If you need to keep these "buckets" alive, I'd suggest tagging your subtasks before sending them over. Just check out the smart add options for details - http://www.rememberthemilk.com/services/smartadd/

~Mike

Monday, March 28, 2011

Picking Up Some Ruby

I'm currently spending some spare hacking time by making my way through Seven Languages in Seven Weeks. Conveniently, it starts out with Ruby, which happens to be a language I've been looking to dive into for quite some time now. There's a question and task in the book that asks for a way to convert from a hash to an array. This is pretty simple as hashes have a "to_a" method. As an example, take the following hash:

aHash = {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}

Converting this to an array results in the following:

aArray = aHash.to_a
=> [["a", 100], ["b", 200], ["c", 300], ["d", 400]]

Now, going the other direction is a bit more complicated as there doesn't seem to be a "to_h" method to convert from an array to a hash. By using the "inject" method, this is what I've come up with:

aArray.inject({}) {|a,b| a[b[0]]=b[1]; a}

I pass an empty hash as an argument to the inject method, which is the initial value for "a." Since "b" is the array of arrays, the values can be accessed via the bracket [] method and used to assign a key/value pair for the hash. Ruby's dynamic nature means we don't have to worry about initializing the hash size up front as this is all handled by the Ruby interpreter at runtime. Finally, there's a bit of trickery after the semi-colon. Every expression in Ruby returns a value, so by having the last value of our code block be the hash "a," we're able to prime the next value for "a" by doing this, thereby allowing us to incrementally build our hash.

A more visual way of seeing this in action is as follows:

aHash.to_a.inject({}) {|a,b| a[b[0]]=b[1]; puts a.inspect; a}
{"a"=>100}
{"a"=>100, "b"=>200}
{"a"=>100, "b"=>200, "c"=>300}
{"a"=>100, "b"=>200, "c"=>300, "d"=>400}
=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}

I'm eager to hear feedback on this if anyone is willing to share.

~Mike

Thursday, March 3, 2011

I've Joined the 2011 Startup Bus!

Chances are, unless you're involved in it you have no idea what the Startup Bus is. I got my ticket bid to join the bus a couple days ago and officially accepted earlier today. Since I have a good deal of preparation I'd like to cram in before the bus rolls out on Tuesday, I'll simply refer you over to their website/blog - http://startupbus.com/. I'm truly thrilled and honored to be able to join a group of talented developers from the Cleveland area in the hopes of establishing a successful startup company!

Monday, February 21, 2011

Retrieving Errors From the .NET MVC ModelState

There's good reason to write extension methods that help adding errors to the .NET MVC ModelState Dictionary, and the Microsoft tutorials present some reasonable, readily-available solutions for this. But you might also desire to get back those errors in the form in which they were added in the first place, and for that it's a little bit trickier. Using the RuleViolation class pattern provided on the asp.net website, the code below uses LINQ query syntax with outer range variables to handle the SelectMany query projection into an IEnumerable<RuleRiolation> collection object.

public static IEnumerable<RuleViolation> GetRuleViolations(this ModelStateDictionary modelState)
        {
            var violations = from vals in modelState
                             from errors in vals.Value.Errors
                             where vals.Value.Errors.Count > 0
                             select new RuleViolation(errors.ErrorMessage, vals.Key);

            return violations;
        }

Sunday, January 9, 2011