Ruby学习笔记(20)_符号Symbol
2017-11-09 16:48
197 查看
Symbol的一篇文章结合ruby基础教程综合 博客原文
:myname
符号能实现的功能,大部分字符串也能实现。但像散列键这样只是单纯判断“是否相等”的处理中,使用符号会比字符串比较更加有效率,因此在实际编程中我们也会时常用到符号。
另外,符号与字符串可以互相任意转换。对符号使用 to_s 方法,则可以得到对应的字符串。反之,对字符串使用 to_sym 方法,则可以得到对应的符号。
There are elements of truth in each of the preceding assertions, and yet in my opinion they are not valuable, partially because they depend on a deep knowledge of Ruby to understand their significance. I prefer to answer the question “what are symbols” in a language independent manner:
The preceding code prints four lines. The first line prints the string representation because that’s how the puts() method is set up. The second line is an explicit conversion to string. The third is an explicit conversion to integer. The fourth prints the type of the symbol. The preceding code results in the following output:
Based on what’s been presented in this section, we can add to the language independent answer to the question “what is a symbol”:
1.A Ruby symbol is a thing that has both a number (integer) representation and a string representation.
2.The string representation is much more important and used much more often.
3.The value of a Ruby symbol’s string part is the name of the symbol, minus the leading colon.
4.A Ruby symbol cannot be changed at runtime.
5.Multiple uses of the same symbol have the same object ID and are the same object.
What do symbols look like?
This is the one area where everyone agrees. Most symbols looks like a colon followed by a non-quoted string::myname
符号能实现的功能,大部分字符串也能实现。但像散列键这样只是单纯判断“是否相等”的处理中,使用符号会比字符串比较更加有效率,因此在实际编程中我们也会时常用到符号。
另外,符号与字符串可以互相任意转换。对符号使用 to_s 方法,则可以得到对应的字符串。反之,对字符串使用 to_sym 方法,则可以得到对应的符号。
sym = :foo #=>:foo sym.to_s #=> "foo"
Symbol是不变的
不可以像其他变量一样对它进行赋值运算。比如这样的写法是错误的:myname = “Tom”,这么写会报错。 相反Symbol可以作为值赋给其他变量比如mystring = :mynameWhat are symbols ?
It’s a string. No it’s an object. No it’s a name.There are elements of truth in each of the preceding assertions, and yet in my opinion they are not valuable, partially because they depend on a deep knowledge of Ruby to understand their significance. I prefer to answer the question “what are symbols” in a language independent manner:
A Ruby symbol is a thing that has both a number (integer) representation and a string representation.
Let’s explore further using code:puts :steve puts :steve.to_s puts :steve.to_i puts :steve.class
The preceding code prints four lines. The first line prints the string representation because that’s how the puts() method is set up. The second line is an explicit conversion to string. The third is an explicit conversion to integer. The fourth prints the type of the symbol. The preceding code results in the following output:
steve steve 10257 Symbol
Based on what’s been presented in this section, we can add to the language independent answer to the question “what is a symbol”:
1.A Ruby symbol is a thing that has both a number (integer) representation and a string representation.
2.The string representation is much more important and used much more often.
3.The value of a Ruby symbol’s string part is the name of the symbol, minus the leading colon.
4.A Ruby symbol cannot be changed at runtime.
5.Multiple uses of the same symbol have the same object ID and are the same object.
相关文章推荐
- ArcGIS 添加 MarkerSymbol 弹出“图形符号无法序列化为 JSON”错误
- 使用POI在Excel单元格插入符号(Symbol)
- [转]Windbg Symbol符号文件设置
- 分支-20. 计算符号函数的值(10)
- 符号表,nm命令,EXPORT_SYMBOL 与 EXPORT_SYMBOL_GPL
- ADS2014中的电路符号(symbol)设置及使用
- 符号变换引擎(Symbol Transform Engine - STE)
- 使用 Microsoft Symbol Server 获取调试符号文件
- GCC学习笔记(三)——关于GCC属性中的弱符号(weak symbol)
- 使用 Microsoft Symbol Server 获取调试符号文件
- 分支-20. 计算符号函数的值(10)
- 使用 Microsoft Symbol Server 获取调试符号文件
- font-family特殊字体(Webdings,Wingdings,Symbol)符号及代码对照表
- 符号化Symbol(符号)体系
- 说说编译链接系统中的符号(symbol)、重定位(relocation)、字串表(string-table)和节(section)
- 符号表实现(Symbol Table Implementations)
- 符号化Symbol(符号)体系
- WinDBG 技巧:列出模块(DLL/EXE)里面所有的符号(symbol)
- 关于符号Symbol第一篇
- 关于符号Symbol第二篇