Question

I am trying to find a way to test symbol equality in the Ruby C API. Consider the following C function:

static VALUE test_symbol_equality(VALUE self, VALUE symbol) {
  if (rb_intern("test") == symbol) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

From the Ruby point of view, this method does not behave as expected:

test_symbol_equality(:test)    # => false

Why is this the case? How do I need to change the code to achieve the expected behaviour?

Was it helpful?

Solution

You are not comparing the same thing in your example.

rb_intern returns an ID, but you are comparing it to the VALUE directly. You first have to "unwrap" the VALUE, retrieving the ID it is associated with. Replacing your if statement by this should solve your problem:

if (rb_intern("test") == SYM2ID(symbol)) {
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top