Skip to content

Instantly share code, notes, and snippets.

@deathbob
Created July 24, 2011 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deathbob/1102683 to your computer and use it in GitHub Desktop.
Save deathbob/1102683 to your computer and use it in GitHub Desktop.
Meta module madness
module Foo
def self.included(base)
class << base
attr_accessor :bar
end
base.instance_eval do
@bar ||= proc{self.snakes}.call
end
end
end
class Baz
def self.snakes
[:cobra]
end
include Foo
end
class Boom < Baz
@bar = [:copperhead]
end
class Bam < Baz
def self.snakes
[:mamba]
end
end
Baz.bar # [:cobra], cool, so far so good
Boom.bar # [:copperhead], also cool, setting @bar like we want
Bam.bar # nil !!! I want this to be [:mamba]
# I understand that the proc is called at the time of include, and thus Bam (and its snakes method) haven't been defined yet.
# What I want to know is, can I delay the evaluation of the proc somehow so that it won't be triggered until I try and get the value of @bar?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment