Ruby C API string and character?

Having written the C extension for the Ruby gem, I need to check the parameter value for equality with a known character and string.

I understand you can put a string with

char *foo = "foo";
VALUE foo_string_value = rb_intern(foo);

and then convert it to a symbol:

VALUE foo_sym_value = ID2SYM(foo_string_value);

My question is whether I can now reliably check a parameter, which can be a character or a string, :fooor 'foo', moreover, they use C pointer equality:

if (param == foo_string_value || param == foo_sym_value) {
  ...
}

In other words, does interning guarantee that equality of the pointer is also equal to equality of values ​​for characters and strings?

If this is the wrong approach, then what is the right choice for this comparison?

+4
source share
1 answer

. , ,

VALUE square_sym = ID2SYM(rb_intern("square"));
VALUE kind_as_sym = rb_funcall(kind_value, rb_intern("to_sym"), 0);
if (square_sym == kind_as_sym) {
  ...
}

. . , ID, , , .

+3

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


All Articles