How can I search a Ruby Maglev array of objects?
How can I search a maglev array of objects? Maglev is great but it doesn't seem to have any querying capabilities. Are there any external libra开发者_如何学Gories for this?
The underlying MagLev VM has had index support for quite a while. There is an example of indexing and querying a collection in http://github.com/MagLev/maglev/tree/master/examples/persistence/indexing/
We had to turn indexing off for a while, but we'll have it turned on, with an updated example, in the next release (probably in a few days).
Maglev 1.0.0 has IdentitySet
now, this might become a pretty powerful query mechanism with multi attribute search and index maintenance, however at the moment it is not very "Ruby-like". You will have to define all the attributes for which you ever want to define an index when you first define your class using the static method self.__fixed_instvars :@inst_var1, :@inst_var2, ...
which will throw an exception when it is called the second time (so you have to add a separate guard that prevents it from running when the class is loaded a second time).
This seems understandable from a Smalltalk point of view where classes have versions and remain static, however it does not quite fit the Maglev situation of one "current" class definition which evolves over time.
If your problem is really just to query an array you can always resort the Ruby select-method which is implemented for most data structures, but it will usually work by just traversing all objects and will not use indices.
I realize this is a bit late, but...
First, there's no such thing as a "MagLev array" it's just a Ruby array. Since it is just a Ruby array, you can use Array#select
and Array#detect
. As Peter said, there's indexing built in (which has been turned back on) but indexing is only available on unordered collections.
精彩评论