【问题标题】:How to get keys from hash - ruby c extension如何从哈希中获取密钥 - ruby​​ c 扩展
【发布时间】:2015-02-08 22:19:07
【问题描述】:

我正在寻找一个可以从哈希中获取所有键的函数,或者我可以循环遍历哈希以一次检索单个键。

目前我正在硬编码密钥

VALUE option = rb_hash_aref(options, rb_str_new2("some_key"));

【问题讨论】:

    标签: ruby ruby-c-extension


    【解决方案1】:

    您总是可以调用 Ruby 方法本身:

    VALUE keys = rb_funcall(hash, rb_intern("keys"), 0)
    

    【讨论】:

      【解决方案2】:

      您可以使用rb_hash_foreach (blog post w/an example) 使用回调函数迭代键/值对:

      void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
      

      有一个rb_hash_keys in MRI,但它似乎不在任何头文件中,因此使用它可能会有风险。

      【讨论】:

      • 是的 rb_hash_keys 抱怨 use of undeclared identifier 'rb_hash_keys'
      • 您有任何工作示例吗?我收到此错误candidate function not viable: no known conversion from 'int (VALUE, VALUE, VALUE)' to 'int (*)(...)' for 2nd argument void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
      • MRI 源在rb_hash_keys 中有一个例子:rxr.whitequark.org/mri/source/hash.c#1627
      • 错误:使用未声明的标识符“rb_hash_keys”;
      • 什么红宝石版本?编译器?编译器标志?你是如何建造这个的?使用these basic instructions 对我来说具有这些名称的空函数编译得很好。
      猜你喜欢
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多