Scope of class.initialize
-
Does anyone know what scope the "initialize" method of a class is?
The reason I'm asking is because I accidently put an initialize method in the global scope, and noticed it was called when dcloader.rb and dynamiccomponents.rb loaded.
I tried some more tests and noticed that some classes executed the initialize method, while some didn't. It seems like classes that were meant to be initialized in ruby didn't execute it, while ones that weren't (Model,Edge,Face,etc.) did.
Has anyone else experienced this?
-
I know that classes that are created using ruby "C" extensions have the class's 'initialize' method called when a new instance of that class is created (using class.new). I don't know if this follows for classes defined strictly in ruby, but I would assume so. Why your method was called by other rubies, I have no idea, unless it was somehow interpreting your initialize as the initialize for those other classes.
-
@avariant said:
unless it was somehow interpreting your initialize as the initialize for those other classes.
I think that is part of it, although it looks like the classes original initialize method runs, too. the global initialize method doesn't run when I instantiate pure ruby classes, so I wonder if it is the C extensions that are triggering it.
-
The short answer is don't do it.
initialize
is the method called whennew
is used to instantiate a new object.class Person def initialize(name) @name = name end end jim = Person.new("Jim")
Person.new creates an instance (instantiates) of a Person object based on the Person class. You define what happens when new is called in your initialize method.
If you were to spell out the default behavior of the top-level definition, it would look like this:
class Object private def initialize # ... end end
So what happens now is that whenever
new
is called anywhere, Ruby looks for theinitialize
method. It looks first in whatever object isself
. But ifself
does not have aninitialize
method, Ruby looks up the chain of inheritance forinitialize
and calls the first one it finds. In some cases, it bubbles all the way up to theObject#initialize
that you have redefined - because everything in Ruby is a descendant ofObject
. Or something like that. -
... also if you def initialize in Ruby console it gives a warning about Object#initialize redefinition can cause infinite loop -> so better class it the initialize method
-
The reason I was asking was in regards to this post:
http://forums.sketchucation.com/viewtopic.php?f=180&t=22567
Is it possible that the bounding box's initialize was interfering with the web dialog's?
-
@cjthompson said:
The reason I was asking was in regards to this post:
http://forums.sketchucation.com/viewtopic.php?f=180&t=22567
Is it possible that the bounding box's initialize was interfering with the web dialog's?
I don't think so because I have seen invalid dialogs aldo, and I'm fairly certain I have not redefined initialize at the top-level.
I see invalid dialogs through variables that reference a dialog that has been closed. I've added some example code here.
Advertisement