类 Symbol
Symbol
对象表示 Ruby 解释器中的一个命名标识符。
您可以使用以下方式显式创建 Symbol
对象:
对于给定的名称或字符串,在程序执行期间,将创建相同的 Symbol
对象,而与该名称的上下文或含义无关。因此,如果 Fred
在一个上下文中是常量,在另一个上下文中是方法,在第三个上下文中是类,则 Symbol
:Fred
在所有三个上下文中将是相同的对象。
module One class Fred end $f1 = :Fred end module Two Fred = 1 $f2 = :Fred end def Fred() end $f3 = :Fred $f1.object_id #=> 2514190 $f2.object_id #=> 2514190 $f3.object_id #=> 2514190
常量、方法和变量名将作为符号返回
module One Two = 2 def three; 3 end @four = 4 @@five = 5 $six = 6 end seven = 7 One.constants # => [:Two] One.instance_methods(true) # => [:three] One.instance_variables # => [:@four] One.class_variables # => [:@@five] global_variables.grep(/six/) # => [:$six] local_variables # => [:seven]
Symbol
对象与 String
对象不同之处在于,Symbol
对象表示标识符,而 String
对象表示文本或数据。
这里有什么¶ ↑
首先,看看其他地方的内容。类 Symbol
-
继承自 类 Object。
-
包含 模块 Comparable。
在这里,类 Symbol
提供了对以下方面有用的方法:
用于查询的方法¶ ↑
-
::all_symbols
:返回 Ruby 符号表中当前所有符号的数组。 -
empty?
:如果self.length
为零,则返回true
;否则返回false
。 -
end_with?
:如果符号以任何给定的字符串结尾,则返回true
。 -
start_with?
:如果符号以任何给定的字符串开头,则返回true
。
用于比较的方法¶ ↑
-
<=>
:如果给定的符号小于、等于或大于符号,则返回 -1、0 或 1。 -
casecmp
:忽略大小写,如果给定的符号小于、等于或大于符号,则返回 -1、0 或 1。 -
casecmp?
:如果符号在 Unicode 大小写折叠后等于给定的符号,则返回true
;否则返回false
。
用于转换的方法¶ ↑
公共类方法
源代码
static VALUE sym_all_symbols(VALUE _) { return rb_sym_all_symbols(); }
返回 Ruby 符号表中当前所有符号的数组
Symbol.all_symbols.size # => 9334 Symbol.all_symbols.take(3) # => [:!, :"\"", :"#"]
源代码
# File ext/json/lib/json/add/symbol.rb, line 44 def self.json_create(o) o['s'].to_sym end
请参阅 as_json
。
公共实例方法
源代码
static VALUE sym_cmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return rb_str_cmp_m(rb_sym2str(sym), rb_sym2str(other)); }
如果 object
是一个符号,则返回等效于 symbol.to_s <=> object.to_s
的值
:bar <=> :foo # => -1 :foo <=> :foo # => 0 :foo <=> :bar # => 1
否则,返回 nil
:foo <=> 'bar' # => nil
相关:String#<=>
。
源代码
static VALUE sym_match(VALUE sym, VALUE other) { return rb_str_match(rb_sym2str(sym), other); }
等效于 symbol.to_s =~ object
,包括对全局变量的可能更新;请参阅 String#=~
。
源代码
static VALUE sym_aref(int argc, VALUE *argv, VALUE sym) { return rb_str_aref_m(argc, argv, rb_sym2str(sym)); }
等效于 symbol.to_s[]
;请参阅 String#[]
。
源代码
# File ext/json/lib/json/add/symbol.rb, line 23 def as_json(*) { JSON.create_id => self.class.name, 's' => to_s, } end
方法 Symbol#as_json
和 Symbol.json_create
可用于序列化和反序列化 Symbol 对象;请参阅 Marshal
。
方法 Symbol#as_json
序列化 self
,返回一个表示 self
的 2 元素哈希
require 'json/add/symbol' x = :foo.as_json # => {"json_class"=>"Symbol", "s"=>"foo"}
方法 JSON.create
反序列化此类哈希,返回一个 Symbol 对象
Symbol.json_create(x) # => :foo
源代码
static VALUE sym_capitalize(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_capitalize(argc, argv, rb_sym2str(sym))); }
等效于 sym.to_s.capitalize.to_sym
。
请参阅 String#capitalize
。
源代码
static VALUE sym_casecmp(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return str_casecmp(rb_sym2str(sym), rb_sym2str(other)); }
类似于 Symbol#<=>
,但不区分大小写;等效于 self.to_s.casecmp(object.to_s)
lower = :abc upper = :ABC upper.casecmp(lower) # => 0 lower.casecmp(lower) # => 0 lower.casecmp(upper) # => 0
如果 self
和 object
具有不兼容的编码,或者如果 object
不是符号,则返回 nil
sym = 'äöü'.encode("ISO-8859-1").to_sym other_sym = 'ÄÖÜ' sym.casecmp(other_sym) # => nil :foo.casecmp(2) # => nil
与 Symbol#casecmp?
不同,不区分大小写不适用于 ‘A’..‘Z’ 和 ‘a’..‘z’ 之外的字符
lower = :äöü upper = :ÄÖÜ upper.casecmp(lower) # => -1 lower.casecmp(lower) # => 0 lower.casecmp(upper) # => 1
源代码
static VALUE sym_casecmp_p(VALUE sym, VALUE other) { if (!SYMBOL_P(other)) { return Qnil; } return str_casecmp_p(rb_sym2str(sym), rb_sym2str(other)); }
如果 self
和 object
在 Unicode 大小写折叠后相等,则返回 true
,否则返回 false
lower = :abc upper = :ABC upper.casecmp?(lower) # => true lower.casecmp?(lower) # => true lower.casecmp?(upper) # => true
如果 self
和 object
具有不兼容的编码,或者如果 object
不是符号,则返回 nil
sym = 'äöü'.encode("ISO-8859-1").to_sym other_sym = 'ÄÖÜ' sym.casecmp?(other_sym) # => nil :foo.casecmp?(2) # => nil
与 Symbol#casecmp
不同,适用于 ‘A’..‘Z’ 和 ‘a’..‘z’ 之外的字符
lower = :äöü upper = :ÄÖÜ upper.casecmp?(lower) # => true lower.casecmp?(lower) # => true lower.casecmp?(upper) # => true
源代码
static VALUE sym_downcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_downcase(argc, argv, rb_sym2str(sym))); }
等效于 sym.to_s.downcase.to_sym
。
请参阅 String#downcase
。
相关:Symbol#upcase
。
源代码
static VALUE sym_empty(VALUE sym) { return rb_str_empty(rb_sym2str(sym)); }
如果 self
为 :''
,则返回 true
,否则返回 false
。
源代码
static VALUE sym_encoding(VALUE sym) { return rb_obj_encoding(rb_sym2str(sym)); }
等效于 self.to_s.encoding
;请参阅 String#encoding
。
源代码
static VALUE sym_end_with(int argc, VALUE *argv, VALUE sym) { return rb_str_end_with(argc, argv, rb_sym2str(sym)); }
等效于 self.to_s.end_with?
;请参阅 String#end_with?
。
源代码
static VALUE sym_inspect(VALUE sym) { VALUE str = rb_sym2str(sym); const char *ptr; long len; char *dest; if (!rb_str_symname_p(str)) { str = rb_str_inspect(str); len = RSTRING_LEN(str); rb_str_resize(str, len + 1); dest = RSTRING_PTR(str); memmove(dest + 1, dest, len); } else { rb_encoding *enc = STR_ENC_GET(str); VALUE orig_str = str; len = RSTRING_LEN(orig_str); str = rb_enc_str_new(0, len + 1, enc); // Get data pointer after allocation ptr = RSTRING_PTR(orig_str); dest = RSTRING_PTR(str); memcpy(dest + 1, ptr, len); RB_GC_GUARD(orig_str); } dest[0] = ':'; RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING); return str; }
返回 self
的字符串表示形式(包括前导冒号)
:foo.inspect # => ":foo"
源代码
static VALUE sym_length(VALUE sym) { return rb_str_length(rb_sym2str(sym)); }
等效于 self.to_s.length
;请参阅 String#length
。
源代码
static VALUE sym_match_m(int argc, VALUE *argv, VALUE sym) { return rb_str_match_m(argc, argv, rb_sym2str(sym)); }
等效于 self.to_s.match
,包括对全局变量的可能更新;请参阅 String#match
。
源代码
static VALUE sym_match_m_p(int argc, VALUE *argv, VALUE sym) { return rb_str_match_m_p(argc, argv, sym); }
等效于 sym.to_s.match?
;请参阅 String#match
。
源代码
# File symbol.rb, line 26 def name Primitive.attr! :leaf Primitive.cexpr! 'rb_sym2str(self)' end
返回 self
的冻结字符串表示形式(不包括前导冒号)
:foo.name # => "foo" :foo.name.frozen? # => true
源代码
static VALUE sym_start_with(int argc, VALUE *argv, VALUE sym) { return rb_str_start_with(argc, argv, rb_sym2str(sym)); }
等效于 self.to_s.start_with?
;请参阅 String#start_with?
。
源代码
static VALUE sym_succ(VALUE sym) { return rb_str_intern(rb_str_succ(rb_sym2str(sym))); }
等效于 self.to_s.succ.to_sym
:foo.succ # => :fop
相关:String#succ
。
源代码
static VALUE sym_swapcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_swapcase(argc, argv, rb_sym2str(sym))); }
等效于 sym.to_s.swapcase.to_sym
。
请参阅 String#swapcase
。
源代码
# File ext/json/lib/json/add/symbol.rb, line 39 def to_json(*a) as_json.to_json(*a) end
返回表示 self
的 JSON
字符串
require 'json/add/symbol' puts :foo.to_json
输出
# {"json_class":"Symbol","s":"foo"}
源代码
VALUE rb_sym_to_proc(VALUE sym) { static VALUE sym_proc_cache = Qfalse; enum {SYM_PROC_CACHE_SIZE = 67}; VALUE proc; long index; ID id; if (!sym_proc_cache) { sym_proc_cache = rb_ary_hidden_new(SYM_PROC_CACHE_SIZE * 2); rb_vm_register_global_object(sym_proc_cache); rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil); } id = SYM2ID(sym); index = (id % SYM_PROC_CACHE_SIZE) << 1; if (RARRAY_AREF(sym_proc_cache, index) == sym) { return RARRAY_AREF(sym_proc_cache, index + 1); } else { proc = sym_proc_new(rb_cProc, ID2SYM(id)); RARRAY_ASET(sym_proc_cache, index, sym); RARRAY_ASET(sym_proc_cache, index + 1, proc); return proc; } }
返回一个 Proc
对象,该对象在第一个参数上调用名称为 self
的方法,并将剩余的参数传递给该方法。
proc = :to_s.to_proc # => #<Proc:0x000001afe0e48680(&:to_s) (lambda)> proc.call(1000) # => "1000" proc.call(1000, 16) # => "3e8" (1..3).collect(&:to_s) # => ["1", "2", "3"]
源代码
# File symbol.rb, line 10 def to_s Primitive.attr! :leaf Primitive.cexpr! 'rb_sym_to_s(self)' end
返回 self
的字符串表示形式(不包括前导冒号)
:foo.to_s # => "foo"
源代码
源代码
static VALUE sym_upcase(int argc, VALUE *argv, VALUE sym) { return rb_str_intern(rb_str_upcase(argc, argv, rb_sym2str(sym))); }
等效于 sym.to_s.upcase.to_sym
。
请参阅 String#upcase
。