Created
July 24, 2011 14:38
-
-
Save deathbob/1102683 to your computer and use it in GitHub Desktop.
Meta module madness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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