Skip to content

Instantly share code, notes, and snippets.

@groveriffic
Created November 21, 2012 22:04
Show Gist options
  • Save groveriffic/4128166 to your computer and use it in GitHub Desktop.
Save groveriffic/4128166 to your computer and use it in GitHub Desktop.
class MetaDerp
### DENSE HORRIBLE CODE
# This should be broken up into smaller chunks possibly moved out into a mixin
# to hide it's horribleness
class << self
def hunt(name, *keys)
class_eval %[
def #{name}
#{keys.map{|key| "row[#{key.inspect}]" }.join(' || ')}
end
]
end
end
### Pretty Expressive Beatiful Code
attr_accessor :row
hunt :message, :message, :are_you_there?, :hello
end
m = MetaDerp.new
m.row = { :hello => 'Metaprogramming' }
puts m.message
m.row = { :are_you_there? => 'Yup', :hello => 'Metaprogramming' }
puts m.message
m.row = { :message => 'It works!' }
puts m.message
m.row = { :"no one will look here" => 'And why should they?' }
puts m.message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment