What's the difference between
class Foo
class << self
def foo; end
end
end
and
class Foo
def self.foo; end
end
Joe
What's the difference between
class Foo
class << self
def foo; end
end
end
and
class Foo
def self.foo; end
end
Joe
As far as my understanding goes there is none. The rubies will
probably use the first one when they are defining more than one class
methods and this would save typing 5 * (methods - 1) chars :-).
./alex
On 10/10/06, joevandyk@gmail.com <joevandyk@gmail.com> wrote:
What's the difference between
class Foo
class << self
def foo; end
end
endand
class Foo
def self.foo; end
endJoe
--
.w( the_mindstorm )p.
Hi --
What's the difference between
class Foo
class << self
def foo; end
end
endand
class Foo
def self.foo; end
end
In the general case of class << obj; def x, vs. def obj.x, there's a
difference in the scoping of constants:
A = 1
class C
A = 2
end
c = C.new
class << c
def x
puts A # this is C's A
end
end
def c.y
puts A # this is top-level A
end
c.x
c.y
But I don't think this will loom very large in the class-method case.
Any constants you define in the class scope will be visible in both
the methods. There may be some way to squeeze a difference out of
them by defining methods in Class or something... but they're
basically interchangeable.
David
On Tue, 10 Oct 2006, joevandyk@gmail.com wrote:
--
David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
joevandyk@gmail.com wrote:
What's the difference between
class Foo
class << self
def foo; end
end
endand
class Foo
def self.foo; end
end
AFAIK nothing. But you can't do the second one with most
metaprogrammed methods, for example attr_accessor.
--Ken
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
I've added a signing subkey to my GPG key. Please update your keyring.
What's the difference between
class Foo
class << self
def foo; end
end
end
Such definition evals area of code in scope of object self, referenced to constant Foo and adds
singleton methods to it.
class Foo
def self.foo; end
end
Such definition creates a singleton method on object self, which is equal to constant Foo in this scope.
On Oct 10, 2006, at 3:55 AM, joevandyk@gmail.com wrote:
Ken Bloom wrote:
But you can't do the second one with most
metaprogrammed methods, for example attr_accessor.
Can you elaborate a little bit more?
Jeff
Hi --
What's the difference between
class Foo
class << self
def foo; end
end
endand
class Foo
def self.foo; end
endAFAIK nothing. But you can't do the second one with most
metaprogrammed methods, for example attr_accessor.
That's a different matter, though (if I'm understanding your point
correctly); the difference between:
class C
attr_accessor :x
# and
class << self
attr_accessor :x
is not a limitation or exception -- it's just that you're calling
attr_accessor on two different objects (C and C's singleton class).
The first attr_accessor doesn't write methods in the singleton class,
so it's not analogous to def self.x.
David
On Tue, 10 Oct 2006, Ken Bloom wrote:
joevandyk@gmail.com wrote:
--
David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
Jeff wrote:
Ken Bloom wrote:
> But you can't do the second one with most
> metaprogrammed methods, for example attr_accessor.
>Can you elaborate a little bit more?
irb(main):001:0> class C; attr_accessor :foo; end
irb(main):002:0> C.foo
NoMethodError: undefined method `foo' for C:Class
from (irb):2
irb(main):003:0> C.new.foo
=> nil
irb(main):004:0> class C; class << self; attr_accessor :bar; end; end
irb(main):005:0> C.bar
=> nil
from :0
There's no analagous syntax to using
def self.foo; end
for the attr_accessor method (and the like).
There, you must use
class << self; attr_accessor :foo; end
--Ken
On Tue, 10 Oct 2006 12:28:02 +0900, Jeff wrote:
Ken Bloom wrote:
But you can't do the second one with most
metaprogrammed methods, for example attr_accessor.Can you elaborate a little bit more?
Jeff
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
I've added a signing subkey to my GPG key. Please update your keyring.