Tuesday, June 21, 2011

The most useful VIM plugin

So I've been researching more about VIM recently. I took a look at a few plugins used by a certain unnamed company and found one that I think is extremely useful, although the syntax for the command is a bit onerous. Here goes anyway. It's called Surround.vim (find it here)

If you have text that looks like this:
"Some text"
you can change the double quotes to single quotes by issuing the following command:
cs"'
Pretty powerful, huh? That's only a fraction of what it allows you to do. Check out the vimscripts site for more info on it

Friday, June 17, 2011

Unfickle: A new constant way

Through my programming in Ruby I see a lot of people doing things like this:



I really don't like this.. It just seems messy. So, I wrote a gem that I think solves it in a somewhat elegant way. The gem is called Unfickle and can be used like so:



The result is somewhat the same, only it's stored in a hash. This gives users the ability to iterate over that hash to find the constant they want. Example:



You can even traverse the constants using the '.each' method provided by the Hash class :D

The entire code for this gem is very simple:



The first method, 'set_const' just creates a new hash, if it doesn't exist, and adds the key/value pair to that hash. 'const_missing' is the method that retrieves the hash value based on the key passed in.

I extended the 'Forwardable' module and delegated four methods to the Hash instance created by the set_const method. This means I don't have to re-implement that.

One of the issues with this code is optimization. Using 'const_missing' means we have to go through a begin/rescue block to figure out a constant is missing. This is a performance hit. The other speed issue comes with the use of delegators. Although these two things can slow down code, I think the speed hit is negligible. If you have a better way, don't hesitate to let me know.

You can find this code on github here.
Install the gem like normal:
gem install unfickle

Monday, June 13, 2011

VIM Searching

Every once in a while I'll want to find the second occurrence of a match in command mode so I can edit from that point. An example would be a line like this:
gem 'haml',             '3.0.25'
gem 'my_own_gem',       '0.1.10', path: "/path/to/my_own_gem"
gem 'pg',               '0.10.1',     :require => false

Say my cursor is at the beginning of the second line and I want to comment out the 'path' part cuz I pushed my code and don't need to develop from my local directory anymore. I used to have to hit 'l' tons of times till I got to the comma just before the path.

I tried using this search pattern '/\,', but that matches the first comma. I'd still have to hit 'l' a bunch of times to get the next one, or I could hit enter and 'n'.

I finally figured out that if I hit '2/\,/' that will find the second match in the expression :D That works with any number, 5, 9 10, etc. VIM is great. the end.

So this is how it ends up
`in command mode`
2/\,
gem 'haml',             '3.0.25'
gem 'my_own_gem',       '0.1.10' #, path: "/path/to/my_own_gem"
gem 'pg',               '0.10.1',     :require => false

Wednesday, May 25, 2011

undefined method task

There's been a few people struggling with the dreaded 'undefined method task' when installing rails 3 with rake 0.9.0.beta4 (currently the latest version of rake that comes with rails automatically). You can get around this issue by putting the following in your Rakefile


Just make sure the modules are added before the Application.load_tasks call.

Monday, May 23, 2011

splat

Found a nifty way to convert arrays to hashes and vise-versa,
ruby-1.9.2-p0 :037 > data = [["a", 1], ["b", 2], ["c", 3]]
 => [["a", 1], ["b", 2], ["c", 3]] 
ruby-1.9.2-p0 :039 > h = Hash[*data.flatten]
 => {"a"=>1, "b"=>2} 
and back
ruby-1.9.2-p0 :048 > ar = *h
 => [["a", 1], ["b", 2]] 

Wednesday, May 18, 2011

in_groups vs in_groups_of

I was looking through the methods for arrays in active support and found in_groups. I already knew about in_groups_of so, naturally, I thought, "what does in_groups do?"
%w(a b c d e f g h i j k l m).in_groups(4)
splits an array into 4 groups of 4 elements (use on bigger arrays to really get a feel for what it does.
[["a", "b", "c", "d"], ["e", "f", "g", nil], ["h", "i", "j", nil], ["k", "l", "m", nil]]

%w(a b c d e f g h i j k l m).in_groups_of(4)
splits the array into groups of 4 elements (again, bigger arrays will give you a better idea)
[["a", "b", "c", "d"], ["e", "f", "g", "h"], ["i", "j", "k", "l"], ["m", nil, nil, nil]]

If you wanted all those nil values to go away, just pass false as a second parameter to either of these functions. You can also just fill in nil with any other object by passing that object instead of false.

Thursday, May 12, 2011

Flyrb

Found a nifty gem the other day called utility_belt. It's actually pretty old, back to about 2006 or so. I thought the idea of being able to use VI to edit some code and have it run in the console without ever really leaving the console was an excellent idea. Only problem was the library hadn't been updated since 2008, which means I couldn't use it in Ruby 1.9.. Gay.
I eventually found a new little gem called FlyRB, which is a fork of utility_belt but it's 1.9 compatible.
Now, I need to figure out how to get flyrb to work in the rails/daemon_kit console without having to put it in the Gemfile (~/.irbrc doesn't seem to do the trick in anything but irb).

Utility Belt
FlyRB