Project

General

Profile

Actions

Bug #8091

closed

Class.new is not called when class is created using class keyword

Added by anuraguniyal (anurag uniyal) over 11 years ago. Updated over 11 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
2.0.0p0
Backport:
[ruby-core:53390]

Description

=begin
When a class is created using ((|class|)) keyword, Class.new is not called but if class is created using ((|Class.new|)) it is obviously called, there seems to be a dyssymmetry between ((|class X|)) and ((|Class.new|)) e.g.

class Class
  class << self
  alias new_orig new
  def new(*args)
    obj = new_orig *args
    print "created",obj,"\n"
    obj
  end
  end
end

class X
end

It doesn't print anything, but Class.new does

=end

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

  • Status changed from Open to Rejected

Correct, not a bug.

Updated by marcandre (Marc-Andre Lafortune) over 11 years ago

Note that if you need to detect class creation, that can be done using the inherited callback.

Actions #3

Updated by anuraguniyal (anurag uniyal) over 11 years ago

marcandre (Marc-Andre Lafortune) wrote:

Note that if you need to detect class creation, that can be done using the inherited callback.

I don't want to detect class creation I wan to overload new, which should be possible, at-least it would be elegant if class X and 'Class.new` behave similarly

Actions #4

Updated by anuraguniyal (anurag uniyal) over 11 years ago

nobu (Nobuyoshi Nakada) wrote:

Correct, not a bug.

Why this is not a bug if classes created differently have different creation mechanism, question is why Class.new is not called when class is created?

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

new' is one of the ways to create an object, and most obvious probably, but not the only way. For examples, string interpolation, File#open, Thread#start, and many. class' statement is just one of them.

Updated by alexeymuranov (Alexey Muranov) over 11 years ago

anuraguniyal (anurag uniyal) wrote:

I don't want to detect class creation I wan to overload new, which should be possible, at-least it would be elegant if class X and 'Class.new` behave similarly

I think partial overloading can be done by defining the initialize private method.

Updated by anuraguniyal (anurag uniyal) over 11 years ago

Initialize is also not called on class X but it is called on Class.new

class Class

  alias old_initialize initialize
  def initialize(*args)
    obj = old_initialize *args
    print "initialize",obj,"\n"
    obj
  end

end

Updated by anuraguniyal (anurag uniyal) over 11 years ago

alexeymuranov (Alexey Muranov) wrote:

I think partial overloading can be done by defining the initialize private method.
no initilize is called on class X that is why it is a bug

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0