Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module instance methods on later defined class

Tags:

ruby

I want to note that this is COMPLETELY a made up question. I am aware that there are other ways to accomplish this.

I want to declare a module like so

module Foo
 # some logic here to
 # get instance method 'foo' on
 # a later defined class
end

then later I want to declare a class like:

class Foo::Bar
end

Then WITHOUT using include or extend be able to do this:

Foo::Bar.new.foo

and have it call the foo method I defined in module Foo

like image 961
Erik Petersen Avatar asked Jul 02 '26 23:07

Erik Petersen


1 Answers

module Foo
 class Bar
   def foo
     puts "erik is a dummy"
     end
   end
 end
Foo::Bar.new.foo
=> erik is a dummy
like image 156
earlonrails Avatar answered Jul 04 '26 23:07

earlonrails