Created
March 2, 2015 19:19
-
-
Save agargiulo/465dd45a30161d9027e3 to your computer and use it in GitHub Desktop.
Playing around with Ruby classes and modules.
This file contains 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 | |
class << self | |
def logger | |
@logger ||= Logger.new($stdout) | |
end | |
end | |
class FunThing | |
def self.is_fun? item | |
item.is_a? FunThing | |
end | |
end | |
class LameThing | |
def self.is_fun? item | |
FunThing.is_fun? item | |
end | |
def self.log_thing | |
Foo.logger.debug 'This does work, right?' | |
## How do I run the above line without explicitly calling Foo.logger? | |
## Sort of like how I can just call FunThing.is_fun? instead of Foo::FunThing.is_fun? | |
# logger.debug 'This will not actually work' | |
end | |
end | |
end | |
fun = Foo::FunThing.new | |
puts Foo::FunThing.is_fun? fun | |
lame = Foo::LameThing.new | |
puts Foo::LameThing.is_fun? lame | |
Foo::LameThing.log_thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment