Forum: Ruby Class methods in a module?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2009-05-25 17:07
At Scotland On Rails a few months ago, I was shown a nice way to put
class methods in a module, alongside instance methods, in such a way as
they will get added as class methods automatically, without any work
required on the part of the person including the module.

Unfortunately i failed to make a note of how to do this and i've since
forgotten.  Can anyone show me?

It was something along these lines...(the following doesn't work btw)

module Mappable

   #instance methods
   def foo
     "foo"
   end

   #class methods
   class << self.class
     def bar
       "bar"
     end
   end

end

can anyone set me straight?
thanks
max
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-05-25 17:52
(Received via mailing list)
2009/5/25 Max Williams <toastkid.williams@gmail.com>

> At Scotland On Rails a few months ago, I was shown a nice way to put
> class methods in a module, alongside instance methods, in such a way as
> they will get added as class methods automatically, without any work
> required on the part of the person including the module.
>
> Unfortunately i failed to make a note of how to do this and i've since
> forgotten.  Can anyone show me?
>
>

I'm not sure if you can pick up the singleton methods from the module
and
automatically copy them to the class -- I've got type errors before when
trying to move and rebind methods. I did come up with this, though,
which is
more expressive than writing an included() hook on every module:

http://gist.github.com/114042
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-05-25 17:53
(Received via mailing list)
On Mon, May 25, 2009 at 11:07 AM, Max Williams
<toastkid.williams@gmail.com> wrote:
> module Mappable
>     end
>   end
>
> end
>
> can anyone set me straight?
> thanks
> max


The usual way is to use the hook method Module#included, something like:

module Mappable

  #instance methods
  def foo
    "foo"
  end

  #class methods
  module ClassMethods
    def bar
      "bar"
    end
  end

  def self.included(other)
     other.extend(ClassMethods)
  end
end

By the way this nesting of a Module inside a module is another variant
on something discussed in another recent ruby-talk thread.
--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
Ff9e18f0699bf079f1fc91c8d4506438?d=identicon&s=25 James Britt (Guest)
on 2009-05-25 21:08
(Received via mailing list)
Max Williams wrote:
> At Scotland On Rails a few months ago, I was shown a nice way to put
> class methods in a module, alongside instance methods, in such a way as
> they will get added as class methods automatically, without any work
> required on the part of the person including the module.
>
> Unfortunately i failed to make a note of how to do this and i've since
> forgotten.  Can anyone show me?
>
> It was something along these lines...(the following doesn't work btw)
>

module Stuff
   module ClassMethods
     def biff(*args)
       # ...
     end

     def baz(*args)
       # ...
     end
   end

   def self.included(base)
     base.extend(ClassMethods)
   end


end

The methods defined in  Stuff::ClassMethods are added as class methods
when

   include Stuff

is called



--
James Britt

www.jamesbritt.com           - Playing with Better Toys
www.ruby-doc.org             - Ruby Help & Documentation
www.rubystuff.com            - The Ruby Store for Ruby Stuff
www.neurogami.com            - Smart application development
This topic is locked and can not be replied to.