Hello,
I was wondering if it is wise/common practice/allowed to have multiple
constructors for a class defined in a C extension.
Specifically, I’m currently doing the following in my extension:
rb_define_singleton_method(c…, “new”, t_new, 1);
rb_define_singleton_method(c…, “new_from_file”, t_new_from_file, 1);
With both calls a structure is allocated and wrapped with
Data_Wrap_Struct(), followed by rb_obj_call_init() on the created object.
The idea of course is to be able to say
obj1 = MyClass.new(…)
and
obj2 = MyClass.new_from_file(‘some_file’)
in my script.
The defined initialize method in the extension actually does nothing, so I
would rather leave it out. I don’t see any reason myself why object
construction and initialization can’t be done completely by the C functions
implementing new and new_from_file. Is there anything special about the
"new" singleton method other than that it implicitly calls “initialize”?
Thanks,
Paul
Paul E.C. Melis wrote:
Hello,
I was wondering if it is wise/common practice/allowed to have multiple
constructors for a class defined in a C extension.
Specifically, I’m currently doing the following in my extension:
rb_define_singleton_method(c…, “new”, t_new, 1);
rb_define_singleton_method(c…, “new_from_file”, t_new_from_file, 1);
With both calls a structure is allocated and wrapped with
Data_Wrap_Struct(), followed by rb_obj_call_init() on the created object.
The idea of course is to be able to say
obj1 = MyClass.new(…)
and
obj2 = MyClass.new_from_file(‘some_file’)
in my script.
The defined initialize method in the extension actually does nothing, so I
would rather leave it out. I don’t see any reason myself why object
construction and initialization can’t be done completely by the C functions
implementing new and new_from_file. Is there anything special about the
“new” singleton method other than that it implicitly calls “initialize”?
This brings another question, why does ruby use as default an instance
method (initialize) for doing what must be a class method (new). I think
that the current approach, does not follow the least surprise principle.
You can of course do:
class Test
def Test.new_from_file(‘someFile’)
aTempVariable=Test.new # because it seems to be the only
contructor allowed
# … whatever other things this routine does
return aTempVariable
end
end
This way you can then
aTestVar = Test.new_from_file(‘someFile’)
But it’s unnatural and strange (at least for me …)
Enric
Paul E.C. Melis wrote:
This brings another question, why does ruby use as default an instance
method (initialize) for doing what must be a class method (new). I think
that the current approach, does not follow the least surprise principle.
When you perform “x = Froz.new”, you’re not calling Froz.new, you’re callnig
Class.new, because Froz.is_a? Class.
Class.new performs some groundwork for creating an object, and then hands over
to Froz.initialize. :initialize does exactly what its name says: it
initialises the object. It couldn’t be a class method: it needs to access
instance data.
It may be restrictive, or not, but you do need some protocol.
You can of course do:
class Test
def Test.new_from_file(‘someFile’)
aTempVariable=Test.new # because it seems to be the only
contructor allowed
# … whatever other things this routine does
return aTempVariable
end
end
This way you can then
aTestVar = Test.new_from_file(‘someFile’)
But it’s unnatural and strange (at least for me …)
Not for me. Just rename to method to from_file, and the client code reads
test_var = Test.from_file(‘some_file’)
Not too bad, is it? I would go for Test.new(‘file’) (I’d probably use a File
object, actually). Anyway, there’s plenty of precendent:
Time.now
Time.local
Thread.current
…
Enric
Gavin
···
From: “Enric Lafont” enric@1smart.com
Hi,
From the “Programming Ruby” book:
"… Data_Make_Struct. However, although this allocates memory for the
object, it does not automatically call an initialize method; you need to
do that yourself:
…
rb_obj_call_init(…
This has the benefit of allowing subclasses in Ruby to override or augment
the basic initialize in your class. …"
If you just write everything in C, then it will be fine. However, when a
subclass is created in Ruby (not in C), then you will see the
consequence. I think the best way to understand this is by
experimentation by, as Nobu pointed out, having a base class and a derived
class.
Regards,
Bill
···
==========================================
nobu.nokada@softhome.net wrote:
Hi,
At Thu, 17 Oct 2002 19:46:45 +0900, > Paul E.C. Melis paul@floorball.nl wrote:
The defined initialize method in the extension actually does nothing, so I
would rather leave it out. I don’t see any reason myself why object
construction and initialization can’t be done completely by the C functions
implementing new and new_from_file. Is there anything special about the
“new” singleton method other than that it implicitly calls “initialize”?
You would be surprised when you create a subclass.
–
Nobu Nakada
Gavin Sinclair wrote:
Not for me. Just rename to method to from_file, and the client code reads
test_var = Test.from_file(‘some_file’)
Not too bad, is it? I would go for Test.new(‘file’) (I’d probably use a File
object, actually). Anyway, there’s plenty of precendent:
Time.now
Time.local
Thread.current
…
Sorry to ask, I’m a newbie with Ruby, and I don’t understand what means
“Just rename to method to from_file”
What method must I rename ? And how to do it ? I’ve done a search on the
ruby/lib sources and I can not find where Time.now is defined.
Thanks
Enric
Hi Enric,
I think what Gavin meant was to replace “Test.new_from_file” with
“Test.from_file” using a text editor.
There is an online version of the “Programming Ruby” book (which is great
for tutorial and reference, especially for beginners) at
http://www.rubycentral.com/book/. One of the chapters is “Built-in
Classes and Methods”, and in there you will find the classes Time and
Thread with all their methods.
Regards,
Bill
···
===========================================================================
Enric Lafont enric@1smart.com wrote:
Gavin Sinclair wrote:
Not for me. Just rename to method to from_file, and the client code reads
test_var = Test.from_file(‘some_file’)
Not too bad, is it? I would go for Test.new(‘file’) (I’d probably use a File
object, actually). Anyway, there’s plenty of precendent:
Time.now
Time.local
Thread.current
…
Sorry to ask, I’m a newbie with Ruby, and I don’t understand what means
“Just rename to method to from_file”
What method must I rename ? And how to do it ? I’ve done a search on the
ruby/lib sources and I can not find where Time.now is defined.
Thanks
Enric