Feature Requests: Browse | Submit New | Admin

[#17457] Use <class>.new instead of <class>.alloc

Date:
2008-01-24 18:59
Priority:
3
Submitted By:
Matt Someone (v01d)
Assigned To:
Nobody (None)
Category:
None
State:
Open
Summary:
Use <class>.new instead of <class>.alloc

Detailed description
Unless I'm missing something, all 'alloc' class method definitions, could be changed to definitions of 'initialize'
as an instance method.
This would allow usage of the 'new' class method.

What SomeClass.new actually does, is to call SomeClass.allocate to get an instance of SomeClass. Then it calls
someClassInstance.initialize.

If you're interested I can try to develop a patch, it is a fairly simple change.
But maybe you know more of how memory is managed on rb-gsl, and this change is not so trivial.

Add A Comment: Notepad

Please login


Followup

Message
Date: 2008-01-28 18:20
Sender: Matt Someone

I'm attaching a patch that does the following:

- Removed alloc() and calloc() methods and renamed them (if
not already) to new() and zero() class methods.
- Renamed others like alloc_uniform to uniform
- Probably other aliases here and there (not many)
- Updated all tests to use these renames
- Updates the run-tests.sh script to:
  * work for tests under subdirectories (wasn't working)
  * Disable testing of qrng.rb since it wasn't working (some
constant wasn't defined in my case.. you can ignore these
change)

There are other things the could be changed a little like:
- removing memcpy/swap/copy class methods (I don't know the
purpose of them) for something else more Ruby-esque
- set_ methods should be renamed to '!' methods probably

Let me know what you think of the patch =)
Date: 2008-01-28 15:49
Sender: Matt Someone

No problem. I'll see if I come up with a patch for this.
Date: 2008-01-28 03:41
Sender: Yoshiki Tsunesada

Some other people had also mensioned the object creations in
Ruby/GSL. They say the method in the current Ruby/GSL is old-fasioned
and should be changed. I don't know how to do that, so I welcome
your contribution if you can develop a patch or show some examples
of the changes.
Date: 2008-01-24 19:42
Sender: Matt Someone

Actually, now that I've read about this a little more, the
first way to change the binding would be to:
a) define the class method 'new', as a call to the C
function that does 'alloc'
b) modify this C alloc function to call rb_obj_call_init().
This mimics the functioning of the 'new' method, and allows
subclassing.

The more precise way, would be to:
a) extract the part that uses ALLOC and Data_Wrap_Struct()
and move it to a separate C function.
b) extract the part that initializes this wrapped structure
and move it to a separate C function.
c) define a ruby 'allocate' class method that will call the
function in (a)
d) define a ruby 'initialize' instance method that will call
the function in (b)

Attached Files:

Name Description Download
rubyesque_renames.patch.gz Renames patch Download

Changes:

Field Old Value Date By
File Added3266: rubyesque_renames.patch.gz2008-01-28 18:20v01d
summaryUse <class>.new instead of <class>.alloc2008-01-28 18:20v01d
summaryUse <class>.new instead of <class>.alloc2008-01-28 15:49v01d
summaryUse <class>.new instead of <class>.alloc2008-01-24 19:42v01d