Erudition and Inanity

brian guthrie blogs

The weirdest code I've seen recently

Posted by Brian Guthrie Sat, 02 Aug 2008 22:15:00 GMT

I was kicking around in ActiveResource::Base recently and it took me a good solid ten minutes to figure out why the prefix method didn’t recur infinitely.

def prefix(options={})
  default = site.path
  default << '/' unless default[-1..-1] == '/'
  # generate the actual method based on the current site path
  self.prefix = default
  prefix(options)
end
def prefix_source
  prefix # generate #prefix and #prefix_source methods first
  prefix_source
end
def prefix=(value = '/')
  # Replace :placeholders with '#{embedded options[:lookups]}'
  prefix_call = value.gsub(/:\w+/) { |key| "\#{options[#{key}]}" }
  # Redefine the new methods.
  code = <<-end_code
    def prefix_source() "#{value}" end
    def prefix(options={}) "#{prefix_call}" end
  end_code
  silence_warnings { instance_eval code, FILE, LINE }
rescue
  logger.error "Couldn't set prefix: #{$!}\n  #{code}" 
  raise
end

Do you see it? When prefix is called, it calls prefix=, which redefines prefix and returns. prefix in turn returns by calling not itself but the newly-created method of the same name.

I am not known for my low tolerance of metalanguage hackery (exhibit). But I do have my limits, and this exceeds them; calling it needlessly obfuscatory would be kind. Or am I wrong? Is there no better way to provide a method with the same semantics?

Posted in | 1 comment |

You can take the software company out of the jungle

Posted by Brian Guthrie Mon, 14 May 2007 16:22:00 GMT

But etc. In my job search this past semester I declined to apply there, even though I have contacts, and now I remember why.

Microsoft takes on the free world

Posted in | 2 comments |

They what said it best

Posted by Brian Guthrie Mon, 23 Apr 2007 15:05:00 GMT

Thanks, The Economist:

When it comes to most dangerous products—be they drugs, cigarettes or fast cars—this newspaper advocates a more [classically] liberal approach than the American government does. But when it comes to handguns, automatic weapons and other things specifically designed to kill people, we believe control is necessary, not least because the failure to deal with such violent devices often means that other freedoms must be curtailed. Instead of a debate about guns, America is now having a debate about campus security.

I recommend that you all subscribe; it’s excellent. Much of their content is also available online for free. I promise I’ll return to technical stuff soon. link

Posted in | no comments |

This is not a free speech issue

Posted by Brian Guthrie Tue, 17 Apr 2007 14:26:00 GMT

I am not a constitutional scholar, but it’s my understanding that the Bill of Rights is silent on the issue of an inalienable right to one’s own talk show. Don Imus isn’t in jail. This is all about community speech and the government isn’t involved. This is precisely how it’s supposed to work.

Posted in | 2 comments |