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