[LRUG] ShRUG Golf update

Tom Stuart tom at experthuman.com
Thu May 12 03:10:06 PDT 2011

On 12 May 2011, at 10:30, Murray Steele wrote:
> I think it might be useful to do a bit of a review of things people learned while doing rubygolf

1. You may omit the curly braces in string interpolation if you're interpolating a global, instance or class variable, which may constitute a net saving in characters (unlikely in the latter case). Particularly useful if you can arrange for the desired values to already magically be in global variables, e.g. $` and $' after a regular expression match, rather than having to spend dollar signs assigning them yourself.

2. Recursive solutions may well be shorter than iterative ones, partly because you can avoid typing "inject".

3. Inequality is cheaper than equality when you can get away with it, e.g. turn "n == 2" into "n < 3", or "a == '' ? b : c" into "a > '' ? c : b".

4. Passing a default value to Hash.new is shorter than assigning defaults later with ||=.

5. Symbol#to_proc never stops being useful, e.g. &:last is shorter than {|_,x|x}.

6. The shortest solutions are probably going to involve code-as-data + eval to leverage some kind of deduplication.

7. Murray wrote: "the tests don't say that Golf needs to be a class, so we can define Golf as a constant and just add methods to it.  So we can [...] open up the metaclass of Golf, which happens to be a constant of an empty string, rather than a class". You might also save characters by assigning a more useful object to Golf if its methods are worth calling in your solution. e.g. if you do Golf=Hash (only 2 characters longer than Golf='') then you can turn "Hash.new" into "new" (5 characters shorter).


More information about the Chat mailing list