Class: Ruby2CExtension::Tools::SymbolManager
- Defined in:
- lib/ruby2cext/tools.rb
Overview
keeps track of used symbols and provides global variables that hold the corresponding ID
Instance Method Summary collapse
-
#get(sym) ⇒ Object
returns the var name for sym.
-
#initialize ⇒ SymbolManager
constructor
A new instance of SymbolManager.
- #to_c_code(init_fun_name = "init_syms") ⇒ Object
Constructor Details
#initialize ⇒ SymbolManager
Returns a new instance of SymbolManager.
6 7 8 |
# File 'lib/ruby2cext/tools.rb', line 6 def initialize @syms = {} end |
Instance Method Details
#get(sym) ⇒ Object
returns the var name for sym
11 12 13 14 |
# File 'lib/ruby2cext/tools.rb', line 11 def get(sym) idx = (@syms[sym] ||= @syms.size) "sym[#{idx}] /* #{sym.to_s.gsub(/[^\w<=>+\-*\/!?@$|&~.]/, "_").gsub("*/", "__")} */" end |
#to_c_code(init_fun_name = "init_syms") ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby2cext/tools.rb', line 16 def to_c_code(init_fun_name = "init_syms") res = [] res << "static ID sym[#{@syms.size}];" unless @syms.empty? res << "static void #{init_fun_name}() {" @syms.sort_by { |sym, idx| idx }.each { |sym, idx| res << "sym[#{idx}] = rb_intern(#{sym.to_s.to_c_strlit});" } res << "}" res.join("\n") end |