0

I'm writing a Ruby binding to a C library. I want to remove the new method from some of my classes - I'm creating objects with other class methods like create and load and the default implementation of new just leaves me with an invalid pointer (the objects of the class are just pointers to opaque structs).

I've read this but

class Test
end

class <<Test
   remove_method :new
end

Just gave me

NameError: method `new' not defined in Class

And anyways I want to do it from C (and avoid rb_eval_string).

Community
  • 1
  • 1
Cubic
  • 14,902
  • 5
  • 47
  • 92

1 Answers1

1

The new method does 3 things:

  • It calls allocate
  • It calls initialize
  • It returns the new object (and ignores the return value of initialize).

I don't think you can succesfully remove it. However making new private is fine:

class X

  def initialize(a,b)
    @a=a
    @b=b
  end

  private_class_method :new

  def X.create(x,y)
    new(x,y)
  end

end
steenslag
  • 79,051
  • 16
  • 138
  • 171