Skip to content

Instantly share code, notes, and snippets.

@tracyloisel
Created February 2, 2017 13:02
Show Gist options
  • Save tracyloisel/714d68f17f99860e31532d97ce84dd5e to your computer and use it in GitHub Desktop.
Save tracyloisel/714d68f17f99860e31532d97ce84dd5e to your computer and use it in GitHub Desktop.
Add to_i feature to TrueClass and FalseClass object
/*
* call-seq:
* false.to_i -> 0
*
* The integer representation of <code>false</code> is 0.
*/
static VALUE
false_to_i(VALUE obj)
{
return INT2FIX(0);
}
/*
* call-seq:
* true.to_i -> 1
*
* The integer representation of <code>true</code> is 1.
*/
static VALUE
true_to_i(VALUE obj)
{
return INT2FIX(1);
}
/*
* Document-class: Data
*
* This is a recommended base class for C extensions using Data_Make_Struct
* or Data_Wrap_Struct, see doc/extension.rdoc for details.
*/
rb_define_method(rb_cTrueClass, "to_i", true_to_i, 0);
rb_define_method(rb_cFalseClass, "to_i", false_to_i, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment