From: "ara.t.howard" Date: 2008-11-10T06:32:28+09:00 Subject: Re: shared-0.4.2 On Nov 9, 2008, at 2:00 PM, Stefan Rusterholz wrote: > Hi Ara > > Thanks a lot for sharing another of your fine libraries :) > May I ask why I would introduce the dependency 'shared' instead of > using > rubys native code sharing facility that is inheritance and (IMO more > powerful) modules? Where do you see the striking advantages of shared? > > Regards > Stefan well, for starters it *is* based on modules ;-) the striking advantage is, as a for instance, that you can do this in rails in lib/shared.rb shared :before_filters do before_filter :foo before_filter :bar def foo() end def bar() end end in app/controllers/foo_controller.rb class Foo < ApplicationController include shared(:before_filters) end in app/controllers/bar/controller.rb class Bar < ApplicationController include shared(:before_filters) end this is absolutely not easily done with modules. shared.rb has all the advantages of ruby module sharing *plus* the simplicity of completely POLS class level method sharing. another example, which is not easily done using modules in lib/shared.rb shared :home_controller do before_filter :must_have_user def index render 'shared/home' end protected def must_have_user() end end in app/controllers/teacher/home_controller.rb class Teacher::HomeController < ApplicationController include shared(:home_controller) end in app/controllers/student/home_controller.rb class Student::HomeController < ApplicationController include shared(:home_controller) end so, in that example, we can directly setup a public method (index action) and protected method, and call a class method. try doing that with modules and you'll see that it's rather un-natural. the call to before_filter, for instance, cannot simply be wrapped with the normal 'ClassMethods' hack, but must actually be deferred because it only *exists* in the target classes - aka it doesn't exist in the mixin but only exists in the mixxee (controllers) making it a chicken-and-egg issue when code sharing is desired. shared let's you factor out chunks of code *exactly* as you would have typed them before and then, later, to inject them into classes or module or objects *exactly as if you'd entered the code there* make sense? cheers. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama