Tuesday, September 20, 2011
A Rails 3 gotcha with html_safe
My (first) contribution to Rails
You'd think DHH would be number one, but actually he's not. Take a look at the list.
Here is my original pull request if you'd like to read more about it.
Ps, I also reported a capybara bug (along with a failing test) that got fixed pretty quickly. But my fork didn't get merged so none of my commits are recorded. :P
Friday, June 17, 2011
Tau in Ruby
Here is tau.rb so you can just require 'tau'. (Maybe this should be a gem...) Ps, I just learned about module_function.
Update 1: I made this ticket which has been mostly ignored.
Update 2: Ticket has been noticed and discussed! :) Ticket has been categorised as joke! :(
Update 3: Some active debate is going on now and the ticket is now 'assigned'! :) See the details.
Update 4: (28-Jun-2014) Made a pull request in Github.
Update 5: See https://github.com/jneen/math-tau/
Thursday, March 17, 2011
beginning_of_fortnight gem is out
Install in the usual way:
sudo gem install beginning_of_fortnight
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).
For more info see:
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.
(Just for fun, here are tweets where it all began).
Bash functions to dump and load a mysql database
Thursday, February 24, 2011
Using the singleton class to include a module on-the-fly
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.
Here is a good explanation of singleton classes in ruby.
Friday, January 7, 2011
Some meta-progamming fun with Ruby (a define_method example)
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:
>> (Date.today + 1.week) - Date.today => Rational(7, 1) >> (Time.now + 1.week) - Time.now => 604799.999613 >> (Time.now + 1.week) - Time.now => 604799.999532 >> 7.days.to_i => 604800
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 course 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 an 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...)
Monday, November 22, 2010
hamrbl: write ruby with python style whitespace (a haml hack)
Did you ever wish your code looked more like your haml?
Did you ever wonder what ruby would be like with python style significant whitespace?
Without further ado, I present hamrbl.rb
Surprisingly the syntax highlighting works fine in vim and github. :)
Ps, it's pronounced "hammerable"
Disclaimer: This is for amusement purposes only. It is almost certainly a terrible misuse of Haml :).