tildeio / libcruby-sys

8 stars 2 forks source link

`SPRINTF_TO_S` #47

Open wagenet opened 6 years ago

wagenet commented 6 years ago

In Helix, this is const char* HELIX_SPRINTF_TO_S = "%" PRIsVALUE;

PRIsVALUE is a special value used in Ruby's sprintf that calls #to_s on the object. (See https://silverhammermba.github.io/emberb/c/#string.)

We're using this in Helix with rb_raise which takes an sprintf formatted string with a list of values that are applied to it. In our case, we want the exception message to the #to_s of a Ruby value.

A more straight-forward solution might just be to do rb_funcall with to_s on the value. However, the benefit to the SPRINTF_TO_S approach is that it allows Ruby to handle more of the dirty work internally.

wagenet commented 6 years ago

Actually, we should just be able to expose PRIsVALUE and build the format string ourselves. See #52.