Created
January 3, 2012 21:01
-
-
Save terotil/1556915 to your computer and use it in GitHub Desktop.
Cross-loading ruby classes from multiple files
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
puts 'foo' | |
class Foo | |
@@loaded ||= [] | |
@@loaded << 'foo.rb' | |
def foo | |
'Foo#foo in foo' | |
end | |
def bar | |
'Foo#bar in foo' | |
end | |
load 'foo2.rb' unless @@loaded.include? 'foo2.rb' | |
load 'foo3.rb' unless @@loaded.include? 'foo3.rb' | |
end | |
puts Foo.new.foo | |
puts Foo.new.bar | |
puts Foo.new.baz |
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
puts 'foo2' | |
class Foo | |
@@loaded ||= [] | |
@@loaded << 'foo2.rb' | |
def bar | |
'Foo#bar in foo2' | |
end | |
def baz | |
'Foo#baz in foo2' | |
end | |
load 'foo.rb' unless @@loaded.include? 'foo.rb' | |
load 'foo3.rb' unless @@loaded.include? 'foo3.rb' | |
end |
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
puts 'foo3' | |
Foo.class_eval '@@loaded ||= []' | |
Foo.class_eval '@@loaded << "foo3.rb"' | |
def Foo.baz | |
'Foo#baz in foo3' | |
end | |
load 'foo.rb' unless Foo.class_eval('@@loaded').include? 'foo.rb' | |
load 'foo2.rb' unless Foo.class_eval('@@loaded').include? 'foo2.rb' |
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
puts 'foo3' | |
class Foo | |
@@loaded ||= [] | |
@@loaded << 'foo3.rb' | |
def baz | |
'Foo#baz in foo3' | |
end | |
load 'foo.rb' unless @@loaded.include? 'foo.rb' | |
load 'foo2.rb' unless @@loaded.include? 'foo2.rb' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment