[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Difference between Foo = Class.new and class Foo

Yukihiro Matsumoto

10/7/2008 2:58:00 PM

Hi,

In message "Re: Difference between Foo = Class.new and class Foo"
on Tue, 7 Oct 2008 23:51:03 +0900, Brandon Dimcheff <bdimchef@wieldim.com> writes:

|I'm playing around with classes and discovered that
|
|class Foo
| ...
|end
|
|and
|
|Foo = Class.new do
| ...
|end
|
|seem to behave slightly differently. I assumed that "class Foo" was
|just a shortcut that got translated into "Foo = Class.new", but when I
|muck with class in the following way:
|
|class Class
| class << self
| def new
| puts "new called"
| super
| end
| end
|end
|
|and then do "Foo = Class.new", I get "new called" printed to stdout.
|If I do "class Foo..." I get nothing. So it seems that the "class"
|keyword does not end up calling new on Class when you define a new
|class. Does anybody know what's going on here? Is something else
|called instead?

I think you're comparing the different code. See the following code:

class Foo
class << self
def new
puts "foo new called"
super
end
end
end

Foo.new


Bar = Class.new do
class << self
def new
puts "bar new called"
super
end
end
end

Bar.new
--
matz.

1 Answer

Brandon Dimcheff

10/7/2008 3:33:00 PM

0

matz,

I don't think I'm doing conceptually different things below. I'm
looking at creating and defining classes, not creating objects from
those classes. I'm attempting to create a class called Foo in both
cases. The first time, I use the class keyword. The second time, I
call Class.new. It seems that using the class keyword doesn't call
Class's metaclass's "new" method, while Class.new (obviously) does.

Thanks,
Brandon

On Oct 7, 2008, at 10:57, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Re: Difference between Foo = Class.new and class Foo"
> on Tue, 7 Oct 2008 23:51:03 +0900, Brandon Dimcheff <bdimchef@wieldim.com
> > writes:
>
> |I'm playing around with classes and discovered that
> |
> |class Foo
> | ...
> |end
> |
> |and
> |
> |Foo = Class.new do
> | ...
> |end
> |
> |seem to behave slightly differently. I assumed that "class Foo" was
> |just a shortcut that got translated into "Foo = Class.new", but
> when I
> |muck with class in the following way:
> |
> |class Class
> | class << self
> | def new
> | puts "new called"
> | super
> | end
> | end
> |end
> |
> |and then do "Foo = Class.new", I get "new called" printed to stdout.
> |If I do "class Foo..." I get nothing. So it seems that the "class"
> |keyword does not end up calling new on Class when you define a new
> |class. Does anybody know what's going on here? Is something else
> |called instead?
>
> I think you're comparing the different code. See the following code:
>
> class Foo
> class << self
> def new
> puts "foo new called"
> super
> end
> end
> end
>
> Foo.new
>
>
> Bar = Class.new do
> class << self
> def new
> puts "bar new called"
> super
> end
> end
> end
>
> Bar.new
> --
> matz.
>