Skip to content

to_na instance method #32

@dicom

Description

@dicom

After subclassing NArray, I have discovered that NArray is missing a similar feature compared to Ruby's Array where you easily convert back to an Array.

With Array I can do this:

a = Array.new(3)
=> [nil, nil, nil]
a.class
=> Array
a.to_a.class
=> Array

class M < Array
end
=> nil
m = M.new(3)
=> [nil, nil, nil]
m.class
=> M
m.to_a.class
=> Array

I propose we add a to_na instance method to NArray, similar as the to_a instance method for Array. The documentation for Ruby's to_a method says:

Returns self. If called on a subclass of Array, converts the receiver to an Array object.

We can then do:

n = NArray.new(1,3)
=> NArray.byte(3):
[ 0, 0, 0 ]
n.class
=> NArray
n.to_na.class
=> NArray

class C < NArray
end
=> nil
c = C.new(1,3)
=> C.byte(3):
[ 0, 0, 0 ]
c.class
=> C
c.to_na.class
=> NArray

Is this possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions