ascii转字符或者字符串转ascii
"a".ord # => 97 "!".ord # => 33 "\n".ord # => 10
'a'[0] # => "a" 'bad sound'[1] # => "a" 'a'[0].ord # => 97 'bad sound'[1].ord # => 97
97.chr # => "a"
符号转字符串
:a_symbol.to_s # => "a_symbol" :AnotherSymbol.id2name # => "AnotherSymbol" :"Yet another symbol!".to_s # => "Yet another symbol!"
通过字符串得到相应的符号
:dodecahedron.object_id # => 516488 symbol_name = "dodecahedron" symbol_name.intern # => :dodecahedron symbol_name.intern.object_id # => 516488
符号由一个名字和一个internal id 组成,相同符号引用同一个对象,但是两个内容一样的字符串却是不同的对象
"string".object_id # =>70309575257960 "string".object_id # =>70309575257221 :symbol.object_id # => 87690 :symbol.object_id # => 87690
如果对象内容重要使用字符串,如果对象唯一性重要使用符号