Wednesday, October 31, 2012

I'm really happy with this. There's two new features compared to what I've been using up until recently.

Firstly, I just learned about the core.pager option which solves the problems I was solving previously with this ugliness:ll = "!git --no-pager log --pretty=nice -n30; echo"

Secondly, the %+d. I generally don't use the --graph log formats, (such as these), that show you the branch paths, but seeing where the refs are is super useful. The + makes it appear on the next line without adding a line break when there are no refs. In short, it's perfect. :D

Here's a screenshot of how it looks (with details obscured to protect the innocent).

Wednesday, August 8, 2012

The shorter version of render :partial is widely known and used. These are equivalent, but the second version is so much more succinct and readable.
To use a partial with a yield in it, (what I call a "block partial"), you can use render :layout which a. doesn't read very well, (it's a partial, not a layout clearly), and b. can't be shortened nicely the way that render :partial can.
So here is block_render:
It lets you have nice neat render calls like this:
This is a real-life example of a block partial I like to use. Those classes are bootstrap classes for making a nice drop down menu.

I'm using Stylish to apply this, but I guess you could do it with a userChrome.css.
The main reason I want this is so the hit target is larger. I want to be able to click my pinned app tabs (currently a TiddlyWiki and my Zimbra corporate calendar) without aiming too hard.

Tuesday, September 20, 2011

This was causing me to scratch my head a bit today. I'm glad I know what's going on now and why you need to use safe_concat. It still seems a little unintuitive though.
For more into see the explanation here (under the "the tricky part") and the api docs on ActiveSupport::SafeBuffer

I fixed a little mysql configuration bug in Rails. It counted as three commits (even though they are the same thing, just in different branches), which puts me unfairly into the top 500 all time committers. ;) See here.

You'd think DHH would be number one, but actually he's not. Take a look at the list.

Friday, June 17, 2011

Inspired by http://bugs.python.org/issue12345 and of course The Tau Manifesto, here is a patch for ruby to define tau, the true circle constant. Of course we don't need to really need to patch ruby to get this functionality (but I had fun doing it anyway).

Here is tau.rb so you can just require 'tau'. (Maybe this should be a gem...) Ps, I just learned about module_function.

Thursday, March 17, 2011

It requires ActiveSupport and adds {next,end_of,beginning_of}_fortnight methods to the Date and Time class. They work the same as the *_week methods defined by ActiveSupport. You can set where you want fortnight boundaries to be (or just use the default).

I carefully made sure the docs look good in RDoc but, rubydoc.info uses YARD. So they don't look so great and actually the main usage examples which I put at the top of the file aren't showing at all...

I guess I will convert to using YARD. Is RDoc fading away? The website looks pretty musty.

Everyone probably has one of these, but here is mine. It's a bit rough but works for me.
If I was going to make it better I'd probably use getopt instead of $1 and $2 and then add options for changing the mysql user. (Currently it hard codes the user as root).
Put it in your .bash_aliases or .bashrc or wherever you keep these types of things.
Update: I made a set of rake tasks for doing this kind of thing, so I'm not actually using this any more.

Thursday, February 24, 2011

I still don't entirely understand the syntax for accessing the singleton class, but I have found a use for it. :) The goal is to be able to make an object include a mixin on the fly.

If you do self.class.send(:include,module_name) instead of using the singleton class then the change affects every instance, not just the one you intended.

So it seems to work pretty well. Ruby is all like "sure buddy, have some rope, take as much as you need". :)

I should say that this technique was designed to be a quick fix to let us work around some legacy code that really should be refactored and fixed properly. I'm not saying this is a sensible design pattern, but it is cool that ruby can do it.

Friday, January 7, 2011

I am currently working on my first gem which I hope to publish soon. It will be based on this beginning_of_fortnight method but will be more complete and flexible and (hopefully) it will be properly tested and documented. And it will be a gem of course, so it will be easy to install.

So the logic used to determine which half of a fortnight a given day falls in I wrote to work with Time objects. I decided that re-implementing it with Date objects would not be trivial. Take a look at this:

The result of subtracting two Dates surprised me a little. I guess Rational(7, 1) is 7 days. But anyway I didn't want to get any deeper into that so I did it like this:
So instead of figuring out how to apply my algorithm to Dates I will just convert them to Times, call the Time method on them, then convert them back to Dates afterwards.

Actually Date#to_time and Time#to_date are not part of the standard library but are defined by ActiveSupport. Since my gem requires ActiveSupport that's fine. Of I could have written out the three methods but why not add some meta-programming coolness to the lazy to DRY it right up.

The splat on *args is important. My Time methods take a argument with a default value. The splatted args takes care of that so no matter how many arguments there are (or aren't), they will get passed correctly into the send. If anyone wants to check out (and/or checkout) the WIP gem, you can find it here (github). Feedback is welcome.

Todos include cleaning the rdoc output and adding gem dependencies to the gemspec (though I don't know if I'd want it to auto-install active support if you didn't have it already...)