[lnkForumImage]
TotalShareware - Download Free Software

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


 

Douglas Livingstone

4/14/2005 3:08:00 AM

class MyClass
def self.do_something
puts 'success'
end
do_something
end

This puts 'success' - this is a a class method right?

How do I move the "def self..end" section into a module? Trying the obvious:

module MyModule
def self.do_something
puts 'success'
end
end

class MyClass
include MyModule
do_something
end

doesn't seem to work, but this does:

module MyModule
def do_something
puts 'success'
end
end

class MyClass
include MyModule
def ex
do_something
end
end

MyClass.new.ex

Here, is "run" an instance method, which is why it can only be called
from a #new instance? How do I get it to get called from this:

class MyClass
include MyModule
do_something
end

Thanks,
Douglas



16 Answers

Gennady

4/14/2005 3:37:00 AM

0

module MyModule
def do_something
puts "success"
end
end

class MyClass
extend MyModule
end

MyClass.do_something # ==> success

This will work too:

class MyClass
extend MyModule
do_something
end

On Apr 13, 2005, at 8:07 PM, Douglas Livingstone wrote:

> class MyClass
> def self.do_something
> puts 'success'
> end
> do_something
> end
>
> This puts 'success' - this is a a class method right?
>
> How do I move the "def self..end" section into a module? Trying the
> obvious:
>
> module MyModule
> def self.do_something
> puts 'success'
> end
> end
>
> class MyClass
> include MyModule
> do_something
> end
>
> doesn't seem to work, but this does:
>
> module MyModule
> def do_something
> puts 'success'
> end
> end
>
> class MyClass
> include MyModule
> def ex
> do_something
> end
> end
>
> MyClass.new.ex
>
> Here, is "run" an instance method, which is why it can only be called
> from a #new instance? How do I get it to get called from this:
>
> class MyClass
> include MyModule
> do_something
> end
>
> Thanks,
> Douglas
>



Trans

4/14/2005 3:37:00 AM

0

1) use a superclass rather than a module:

class MySuperClass
def self.do_something
puts "success"
end
end

class MyClass < MySuperClass
do_something
end

2) use extend instead of include:

module MyModule
def do_something
puts "success"
end
end

class MyClass
extend MyModule
do_something
end

3) get tricky with it and use append_features:

module MyModule
def self.append_features( aClass )
def aClass.do_something
puts "success"
end
super # must do this!
end
end

class MyClass
include MyModule
do_something
end

I do not recommend the later though.

T.

Robert Klemme

4/16/2005 9:37:00 AM

0


"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1113449849.596475.60490@z14g2000cwz.googlegroups.com...
> 1) use a superclass rather than a module:
>
> class MySuperClass
> def self.do_something
> puts "success"
> end
> end
>
> class MyClass < MySuperClass
> do_something
> end

Did you verify that this works? :-)

> 2) use extend instead of include:
>
> module MyModule
> def do_something
> puts "success"
> end
> end
>
> class MyClass
> extend MyModule
> do_something
> end
>
> 3) get tricky with it and use append_features:
>
> module MyModule
> def self.append_features( aClass )
> def aClass.do_something
> puts "success"
> end
> super # must do this!
> end
> end
>
> class MyClass
> include MyModule
> do_something
> end
>
> I do not recommend the later though.

Here's what I do if I want to have a set of singleton methods available for
all classes in a class hierarchy:

class Base
module MySpecialClassMethods
# make inheritance work:
def inherited(cl) cl.extend MySpecialClassMethods end

attr_accessor :bar
def foo() "foo" end
# other methods...
end

extend MySpecialClassMethods
end

class Derived < Base
end

>> Base.foo
=> "foo"
>> Derived.foo
=> "foo"

>> Base.bar = 1
=> 1
>> Base.bar
=> 1
>> Derived.bar
=> nil
>> Derived.bar = 10
=> 10
>> Derived.bar
=> 10
>> Base.bar
=> 1

The nice thing is that there is just one copy of those method definitions
plus you can change them or add new ones later and automatically all classes
in the hiearchy have them.

Some background: you need to use the trick with #inherited() because
singleton methods do not inherit along the class hierarchy because the class
of all classes is Class.

>> Base.class
=> Class
>> Derived.class
=> Class

Kind regards

robert

Trans

4/16/2005 12:34:00 PM

0


Robert Klemme wrote:
> "Trans" <transfire@gmail.com> schrieb im Newsbeitrag
> news:1113449849.596475.60490@z14g2000cwz.googlegroups.com...
> > 1) use a superclass rather than a module:
> >
> > class MySuperClass
> > def self.do_something
> > puts "success"
> > end
> > end
> >
> > class MyClass < MySuperClass
> > do_something
> > end
>
> Did you verify that this works? :-)
>
> > 2) use extend instead of include:
> >
> > module MyModule
> > def do_something
> > puts "success"
> > end
> > end
> >
> > class MyClass
> > extend MyModule
> > do_something
> > end
> >
> > 3) get tricky with it and use append_features:
> >
> > module MyModule
> > def self.append_features( aClass )
> > def aClass.do_something
> > puts "success"
> > end
> > super # must do this!
> > end
> > end
> >
> > class MyClass
> > include MyModule
> > do_something
> > end
> >
> > I do not recommend the later though.
>
> Here's what I do if I want to have a set of singleton methods
available for
> all classes in a class hierarchy:
>
> class Base
> module MySpecialClassMethods
> # make inheritance work:
> def inherited(cl) cl.extend MySpecialClassMethods end
>
> attr_accessor :bar
> def foo() "foo" end
> # other methods...
> end
>
> extend MySpecialClassMethods
> end
>
> class Derived < Base
> end
>
> >> Base.foo
> => "foo"
> >> Derived.foo
> => "foo"
>
> >> Base.bar = 1
> => 1
> >> Base.bar
> => 1
> >> Derived.bar
> => nil
> >> Derived.bar = 10
> => 10
> >> Derived.bar
> => 10
> >> Base.bar
> => 1
>
> The nice thing is that there is just one copy of those method
definitions
> plus you can change them or add new ones later and automatically all
classes
> in the hiearchy have them.
>
> Some background: you need to use the trick with #inherited() because
> singleton methods do not inherit along the class hierarchy because
the class
> of all classes is Class.
>
> >> Base.class
> => Class
> >> Derived.class
> => Class
>
> Kind regards
>
> robert

Trans

4/16/2005 1:02:00 PM

0


Robert Klemme wrote:
> "Trans" <transfire@gmail.com> schrieb im Newsbeitrag
> news:1113449849.596475.60490@z14g2000cwz.googlegroups.com...
> > 1) use a superclass rather than a module:
> >
> > class MySuperClass
> > def self.do_something
> > puts "success"
> > end
> > end
> >
> > class MyClass < MySuperClass
> > do_something
> > end
>
> Did you verify that this works? :-)

Of course not! :-) But if that doesn't work then I need go back to
training wheels!!! Did you verify that is doesn't? if so then then
again! ;o)

> Here's what I do if I want to have a set of singleton methods
available for
> all classes in a class hierarchy:
>
> class Base
> module MySpecialClassMethods
> # make inheritance work:
> def inherited(cl) cl.extend MySpecialClassMethods end
>
> attr_accessor :bar
> def foo() "foo" end
> # other methods...
> end
>
> extend MySpecialClassMethods
> end

You're right, this is a great approach.

T.

Robert Klemme

4/16/2005 3:46:00 PM

0


"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1113654704.368730.203380@l41g2000cwc.googlegroups.com...
>
> Robert Klemme wrote:
>> "Trans" <transfire@gmail.com> schrieb im Newsbeitrag
>> news:1113449849.596475.60490@z14g2000cwz.googlegroups.com...
>> > 1) use a superclass rather than a module:
>> >
>> > class MySuperClass
>> > def self.do_something
>> > puts "success"
>> > end
>> > end
>> >
>> > class MyClass < MySuperClass
>> > do_something
>> > end
>>
>> Did you verify that this works? :-)
>
> Of course not! :-) But if that doesn't work then I need go back to
> training wheels!!! Did you verify that is doesn't? if so then then
> again! ;o)

Of course not - cause I *knew* that it does not work. But if that comforts
you

>> class MySuperClass
>> def self.do_something
>> puts "success"
>> end
>> end
=> nil
>>
?> class MyClass < MySuperClass
>> do_something
>> end
success
=> nil
>>

Oooops... :-)))

Looks like it's rather me that has to go back to school. Now *you* explain
why it works.

>> Here's what I do if I want to have a set of singleton methods
> available for
>> all classes in a class hierarchy:
>>
>> class Base
>> module MySpecialClassMethods
>> # make inheritance work:
>> def inherited(cl) cl.extend MySpecialClassMethods end
>>
>> attr_accessor :bar
>> def foo() "foo" end
>> # other methods...
>> end
>>
>> extend MySpecialClassMethods
>> end
>
> You're right, this is a great approach.

But apparently superfluous

>> class My2 < MyClass
>> do_something
>> end
success
=> nil

Could it be that this didn't work in older versions? I'm almost sure that I
did try this once and it failed. This is strange...

Thanks for crushing my certainty!

Kind regards

robert

Trans

4/16/2005 4:13:00 PM

0


Robert Klemme wrote:
> Oooops... :-)))
>
> Looks like it's rather me that has to go back to school. Now *you*
explain
> why it works.

Oh dear. Why? Uh.....It just do!

> >> Here's what I do if I want to have a set of singleton methods
> > available for
> >> all classes in a class hierarchy:
> >>
> >> class Base
> >> module MySpecialClassMethods
> >> # make inheritance work:
> >> def inherited(cl) cl.extend MySpecialClassMethods end
> >>
> >> attr_accessor :bar
> >> def foo() "foo" end
> >> # other methods...
> >> end
> >>
> >> extend MySpecialClassMethods
> >> end
> >
> > You're right, this is a great approach.
>
> But apparently superfluous
>
> >> class My2 < MyClass
> >> do_something
> >> end
> success
> => nil
>
> Could it be that this didn't work in older versions? I'm almost sure
that I
> did try this once and it failed. This is strange...

Actually I think it always has. But what doesn't is this:

module MyNotSoSuperModule
def self.do_something
puts "success"
end
end

class MyClass
include MyNotSoSuperModule
do_something
end

#=> undefined local variable or method `do_something' for MyClass:Class
(NameError)

Maybe you got the cases crossed? Actually I got them crossed myself, as
I was thinking your example did us this service by allowing modules to
include class-level methods, but that would actually require using
#included rather then #inherited, like this:

module Base
module MySpecialClassMethods
# make inheritance work:
def included(cl)
cl.extend MySpecialClassMethods
p self, cl
end

attr_accessor :bar
def foo() "foo" end
# other methods...
end

extend MySpecialClassMethods
end

class Derived
include Base
end

p Derived.foo

class Derived2 < Derived
end

p Derived2.foo

And that is a good idea, I think.

> Thanks for crushing my certainty!

Hey, no problem. I'm an expert at crushing my own, so the least I could
do was share :-)

T.

Trans

4/16/2005 5:31:00 PM

0

robert,

i have one for you. tell me how to create a class whose subclasses DO
NOT inherit some particular state from. In other words I need:

class Base
def self.state ; true ; end # or what have you
end

class Derived < Base
end

Derived.state #=> nil (was NOT inherited)

how do i get something like that to work?

t.

Alexandru Popescu

4/16/2005 5:48:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[quote Trans::on 4/16/2005 7:34 PM]
> robert,
>
> i have one for you. tell me how to create a class whose subclasses DO
> NOT inherit some particular state from. In other words I need:
>
> class Base
> def self.state ; true ; end # or what have you
> end
>
> class Derived < Base
> end
>
> Derived.state #=> nil (was NOT inherited)
>
> how do i get something like that to work?
>
> t.
>
>
>
absolutely ruby newbie 2c:

probably private will do this.

- --
:alex |.::the_mindstorm::.|
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFCYV1WOCPjdDT2FEURAq1iAJ46ld2XYqKDXd1jVuJuvp+wEpnb+QCgsbf7
bTreChtwhdxKBXVlMGUpsK4=
=cyQ2
-----END PGP SIGNATURE-----


Sean O'Halpin

4/16/2005 10:19:00 PM

0

On Sun, 2005-04-17 at 01:14 +0900, Trans wrote:
> Actually I think it always has. But what doesn't is this:
>
> module MyNotSoSuperModule
> def self.do_something
> puts "success"
> end
> end
>
> class MyClass
> include MyNotSoSuperModule
> do_something
> end

Is this what you're after?

module MySuperDuperModule
def do_something
puts "success"
end
end

class MyClass
extend MySuperDuperModule
do_something
end

#=> success

Sean