Skip to content

Commit

Permalink
Added rb_sym2str()
Browse files Browse the repository at this point in the history
This just converts a Symbol/ID (we treat both as the same) to a String.
  • Loading branch information
Yorick Peterse committed May 8, 2015
1 parent 3871c2b commit b7f4f94
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/ruby/optional/capi/ext/rubyspec.h
Expand Up @@ -606,6 +606,7 @@
#define HAVE_RB_IS_CLASS_ID 1
#define HAVE_RB_IS_CONST_ID 1
#define HAVE_RB_IS_INSTANCE_ID 1
#define HAVE_RB_SYM2STR 1

/* Thread */
#define HAVE_RB_THREAD_ALONE 1
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/optional/capi/ext/symbol_spec.c
Expand Up @@ -81,6 +81,12 @@ VALUE symbol_spec_rb_is_instance_id(VALUE self, VALUE sym) {
}
#endif

#ifdef HAVE_RB_SYM2STR
VALUE symbol_spec_rb_sym2str(VALUE self, VALUE sym) {
return rb_sym2str(sym);
}
#endif

void Init_symbol_spec() {
VALUE cls;
cls = rb_define_class("CApiSymbolSpecs", rb_cObject);
Expand Down Expand Up @@ -121,6 +127,10 @@ void Init_symbol_spec() {
#ifdef HAVE_RB_IS_INSTANCE_ID
rb_define_method(cls, "rb_is_instance_id", symbol_spec_rb_is_instance_id, 1);
#endif

#ifdef HAVE_RB_SYM2STR
rb_define_method(cls, "rb_sym2str", symbol_spec_rb_sym2str, 1);
#endif
}

#ifdef __cplusplus
Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/optional/capi/symbol_spec.rb
Expand Up @@ -104,4 +104,10 @@
@s.rb_is_class_id(:foo).should == false
end
end

describe "rb_sym2str" do
it "converts a Symbol to a String" do
@s.rb_sym2str(:bacon).should == "bacon"
end
end
end
4 changes: 4 additions & 0 deletions vm/capi/symbol.cpp
Expand Up @@ -40,4 +40,8 @@ extern "C" {
Object* p = reinterpret_cast<Symbol*>(sym)->is_cvar_p(env->state());
return CBOOL(p) ? Qtrue : Qfalse;
}

VALUE rb_sym2str(VALUE sym) {
return rb_id2str(SYM2ID(sym));
}
}
2 changes: 2 additions & 0 deletions vm/include/capi/ruby/ruby.h
Expand Up @@ -1585,6 +1585,8 @@ struct RTypedData {
ID rb_intern_str(VALUE string);
#define HAVE_RB_INTERN_STR 1

VALUE rb_sym2str(VALUE sym);

/** Coerce x and y and perform 'x func y' */
VALUE rb_num_coerce_bin(VALUE x, VALUE y, ID func);

Expand Down

0 comments on commit b7f4f94

Please sign in to comment.