[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class methods in a module?

Max Williams

5/25/2009 3:08:00 PM

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
--
Posted via http://www.ruby-....

3 Answers

James Coglan

5/25/2009 3:46:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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....

Rick DeNatale

5/25/2009 3:49:00 PM

0

On Mon, May 25, 2009 at 11:07 AM, Max Williams
<toastkid.williams@gmail.com> 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. =A0Can anyone show me?
>
> It was something along these lines...(the following doesn't work btw)
>
> module Mappable
>
> =A0 #instance methods
> =A0 def foo
> =A0 =A0 "foo"
> =A0 end
>
> =A0 #class methods
> =A0 class << self.class
> =A0 =A0 def bar
> =A0 =A0 =A0 "bar"
> =A0 =A0 end
> =A0 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.
--=20
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

James Britt

5/25/2009 7:07:00 PM

0

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