The weirdest code I've seen recently

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

  def prefix_source
    prefix # generate #prefix and #prefix_source methods first

  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
    silence_warnings { instance_eval code, __FILE__, __LINE__ }
    logger.error "Couldn't set prefix: #{$!}\n  #{code}"

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?

I'm Not Sure That Word Means What You Think It Means

GitHub, briefly