Skip to content

Commit b7f4f94

Browse files
author
Yorick Peterse
committedMay 8, 2015
Added rb_sym2str()
This just converts a Symbol/ID (we treat both as the same) to a String.
1 parent 3871c2b commit b7f4f94

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed
 

Diff for: ‎spec/ruby/optional/capi/ext/rubyspec.h

+1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@
606606
#define HAVE_RB_IS_CLASS_ID 1
607607
#define HAVE_RB_IS_CONST_ID 1
608608
#define HAVE_RB_IS_INSTANCE_ID 1
609+
#define HAVE_RB_SYM2STR 1
609610

610611
/* Thread */
611612
#define HAVE_RB_THREAD_ALONE 1

Diff for: ‎spec/ruby/optional/capi/ext/symbol_spec.c

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ VALUE symbol_spec_rb_is_instance_id(VALUE self, VALUE sym) {
8181
}
8282
#endif
8383

84+
#ifdef HAVE_RB_SYM2STR
85+
VALUE symbol_spec_rb_sym2str(VALUE self, VALUE sym) {
86+
return rb_sym2str(sym);
87+
}
88+
#endif
89+
8490
void Init_symbol_spec() {
8591
VALUE cls;
8692
cls = rb_define_class("CApiSymbolSpecs", rb_cObject);
@@ -121,6 +127,10 @@ void Init_symbol_spec() {
121127
#ifdef HAVE_RB_IS_INSTANCE_ID
122128
rb_define_method(cls, "rb_is_instance_id", symbol_spec_rb_is_instance_id, 1);
123129
#endif
130+
131+
#ifdef HAVE_RB_SYM2STR
132+
rb_define_method(cls, "rb_sym2str", symbol_spec_rb_sym2str, 1);
133+
#endif
124134
}
125135

126136
#ifdef __cplusplus

Diff for: ‎spec/ruby/optional/capi/symbol_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,10 @@
104104
@s.rb_is_class_id(:foo).should == false
105105
end
106106
end
107+
108+
describe "rb_sym2str" do
109+
it "converts a Symbol to a String" do
110+
@s.rb_sym2str(:bacon).should == "bacon"
111+
end
112+
end
107113
end

Diff for: ‎vm/capi/symbol.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ extern "C" {
4040
Object* p = reinterpret_cast<Symbol*>(sym)->is_cvar_p(env->state());
4141
return CBOOL(p) ? Qtrue : Qfalse;
4242
}
43+
44+
VALUE rb_sym2str(VALUE sym) {
45+
return rb_id2str(SYM2ID(sym));
46+
}
4347
}

Diff for: ‎vm/include/capi/ruby/ruby.h

+2
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,8 @@ struct RTypedData {
15851585
ID rb_intern_str(VALUE string);
15861586
#define HAVE_RB_INTERN_STR 1
15871587

1588+
VALUE rb_sym2str(VALUE sym);
1589+
15881590
/** Coerce x and y and perform 'x func y' */
15891591
VALUE rb_num_coerce_bin(VALUE x, VALUE y, ID func);
15901592

0 commit comments

Comments
 (0)