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.
In Helix, this is
const char* HELIX_SPRINTF_TO_S = "%" PRIsVALUE;
PRIsVALUE
is a special value used in Ruby'ssprintf
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 ansprintf
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
withto_s
on the value. However, the benefit to theSPRINTF_TO_S
approach is that it allows Ruby to handle more of the dirty work internally.