How to call Ruby C internal methods in Ruby?

I am trying to create a hash from an array, and the documentation for http://ruby-doc.org/ruby-1.9/classes/Array.src/M000744.html shows an internal ruby ​​method called ary_make_hash. The source uses this for difference arrays. Corresponding line in the source: ary_make_hash (to_ary (ary2), 0);

Is there a way to access the ary_make_hash function and other Ruby internal functions from within Ruby? I ask, since I'm trying to convert an array to a hash, and I would like to use the built-in C methods, since they are much faster. (FYI. I see the difference in speed by subtracting two arrays that internally call the converter method). Thanks for any thoughts.

Robert

+3
source share
3 answers

in general, if it is not in ruby.h, then this is not a “public” api, searching for a method called rb_xxx can also help. GL

+1
source

Is it fast enough? You are not involved in Ruby at all, but rely on the built-in conversion of the array to a hash.

a1 = [[:a,1],[:b,2],[:c,3]]
h1 = Hash[a2]
#=> {:a=>1, :b=>2, :c=>3} 

a2 = a1.flatten
h2 = Hash[*a2]
#=> {:a=>1, :b=>2, :c=>3}     
+1
source

This function is static, which is in accordance with this: http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/SYNTAX/static.htm

means that it cannot be referenced outside the array.c file.

So, to use it, you will need to crack the source code.

0
source

Source: https://habr.com/ru/post/1781734/


All Articles