Skip to content

Commit

Permalink
ad rb_intern spec
Browse files Browse the repository at this point in the history
  • Loading branch information
timfel committed Sep 11, 2010
1 parent da754a5 commit be04c89
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions optional/capi/ext/rubyspec.h
Expand Up @@ -269,6 +269,7 @@
#define HAVE_RB_STRUCT_DEFINE 1

/* Symbol */
#define HAVE_RB_INTERN 1
#define HAVE_RB_IS_CLASS_ID 1
#define HAVE_RB_IS_CONST_ID 1
#define HAVE_RB_IS_INSTANCE_ID 1
Expand Down
16 changes: 16 additions & 0 deletions optional/capi/ext/symbol_spec.c
Expand Up @@ -5,6 +5,17 @@
extern "C" {
#endif

#ifdef HAVE_RB_INTERN
VALUE symbol_spec_rb_intern(VALUE self, VALUE string) {
return ID2SYM(rb_intern(RSTRING_PTR(string)));
}

VALUE symbol_spec_rb_intern_c_compare(VALUE self, VALUE string, VALUE sym) {
ID symbol = rb_intern(RSTRING_PTR(string));
return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
}
#endif

#ifdef HAVE_RB_IS_CLASS_ID
VALUE symbol_spec_rb_is_class_id(VALUE self, VALUE sym) {
return rb_is_class_id(SYM2ID(sym)) ? Qtrue : Qfalse;
Expand All @@ -27,6 +38,11 @@ void Init_symbol_spec() {
VALUE cls;
cls = rb_define_class("CApiSymbolSpecs", rb_cObject);

#ifdef HAVE_RB_INTERN
rb_define_method(cls, "rb_intern", symbol_spec_rb_intern, 1);
rb_define_method(cls, "rb_intern_c_compare", symbol_spec_rb_intern_c_compare, 2);
#endif

#ifdef HAVE_RB_IS_CLASS_ID
rb_define_method(cls, "rb_is_class_id", symbol_spec_rb_is_class_id, 1);
#endif
Expand Down
7 changes: 7 additions & 0 deletions optional/capi/symbol_spec.rb
Expand Up @@ -7,6 +7,13 @@
@s = CApiSymbolSpecs.new
end

describe "rb_intern" do
it "converts a string to a symbol, uniquely" do
@s.rb_intern("test_symbol").should === :test_symbol
@s.rb_intern_c_compare("test_symbol", :test_symbol).should == true
end
end

describe "rb_is_const_id" do
it "returns true given a const-like symbol" do
@s.rb_is_const_id(:Foo).should == true
Expand Down

0 comments on commit be04c89

Please sign in to comment.