On Thu, 24 Mar 2005, Adelle Hartley wrote:
> Hi all,
>
> I''ve been poking around the insides of active record to see what I
can
> learn, and I am intrigued (read: confused) by the section of base.rb that
> starts on line 278:
>
> class << self # Class methods
> ...
> def find(*args)
> ...
> end
> ...
>
> What does this do?
>
> Is this just a quicker way of writing
>
> def self.find(*args)
> ...
> end
>
> ?
>
> Or does it accomplish something more?
depends on what''s in your ''...''. doing
class << self
end
puts everything you type here in class scope. so you can do things like
class Foo
class << self
attr_accessor :bar
end
attr_accessor :bar
end
and then be able to do
Foo::bar = 42
foo = Foo::new
foo.bar = 42
so, for one method it''s not that different. basically ''class
<< some_object''
is much more generic and can do really neat stuff like
harp:~ > cat a.rb
a = Array::new
class << a; def foo; 42; end; end
p a.foo
harp:~ > ruby a.rb
42
here you just duck into a''s singleton class and define a method in it.
you
can''t do that with
def a.foobar
end
HTH.
-a
--
==============================================================================|
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| When you do something, you should burn yourself completely, like a good
| bonfire, leaving no trace of yourself. --Shunryu Suzuki
===============================================================================