开发者

Why does Ruby expose symbols?

Why does Ruby expose symbols for explicit use? Isn't that the sort of optimisation that's usually handled by the开发者_如何学Python interpreter/compiler?


Part of the issue is that Ruby strings are mutable. Since every string Ruby allocates must be independent (it can't cache short/common ones), it's convenient to have a Symbol type to let the programmer have what are essentially immutable, memory-efficient strings.

Also, they share many characteristics with enum's, but with less pain for the programmer.


Ruby symbols are used in lieu of string constants in other similar languages. Besides the performance benefit, they can be used to semantically distinguish between string data and a more abstract symbol. Being syntactically different, they can clearly be distinguished in code.


Have a look at Ruby symbols post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜