`
peryt
  • 浏览: 54979 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

the difference between string and symbol in ruby

阅读更多
Do you know why rails like using symbol instead of string as key for hash?

the answer is,
when comparing symbol, it find the symbol object_id, then find the symbol,
Because there is only one copy of the same symbol in the memory in ruby.

when comparing string, it has to compare the characters in the string one by one,
Each string has its own copy, even if they are the same string.
you know this is much expensive than comparing symbol!!



1. they can be converted between each other:

"hello".to_sym  => :hello
"hello".intern   => :hello
:hello.to_s  => "hello"

"hello world" == :"hello world"

2. symbol is a constant, can't be changed.
string is a variable, can be changed.

puts "hello" << " world"
=> "hello world"

puts :hello << :"   world"
=> error, undefined method << for symbol.

sometimes, your program will rely on a string as a flag, and this flag should never change, if you use a common, string, it may be changed by mistake, for example:

status = "peace"
buggy_logger = status

print buggy_logger << "\n"

in the above code, the string status is changed by mistake.

to avoid this mistake, there are some methods:
a. use clone:

status = "peace"
buggy_logger = status.clone

by this way, if you change buggy_logger, status will not be affected.

b. use freeze:
status = "peace"
status.freeze

then it will report error if some code want to change the content of status.

c. use symbol.


3. overhead, the resource by symbol is much less.

puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id
puts "hello world".object_id

# => 3102960
# => 3098410
# => 3093860
# => 3089330
# => 3084800

you can see the object.id is different for the same string.

but for symbol:

puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id
puts :"hello world".object_id

# => 239518
# => 239518
# => 239518
# => 239518
# => 239518

They are the same.

4. there is a variable Symbol that hold all existing symbol:

puts Symbol.all_symbols.inspect

5. how to test the performance of a block of code:

require 'benchmark'

str_time = Benchmark.measure do
    10000000.times do
          "test"
     end
end.total

symbol_time = Benchmark.measure do
    10000000.times do
          :test
     end
end.total

puts "String: " + str.to_s
puts "Symbol: " + sym.to_s



分享到:
评论

相关推荐

    difference between marketing and sales

    市场营销与销售是商业活动中两个密切相关的但又有所区别的领域。它们之间的差异可以从多个角度进行阐述: 1. 目标与期限:市场营销关注长期的竞争优势和未来市场趋势,旨在解决问题并创造需求;...

    Difference between HBase and RDBMS

    在IT领域,数据库的选择对于系统设计至关重要。本文将深入探讨两种截然不同的数据库类型:HBase和RDBMS(关系型数据库管理系统)之间的差异,以及为什么在某些情况下选择HBase可能更为合适。 HBase,全称为Hadoop ...

    the difference between java and C++(part1)

    java and C++ difference

    What's the difference between TAF and FCF

    透明应用故障切换(TAF)和快速连接故障切换(FCF)是两种在高可用性环境中用于处理数据库连接故障的机制。它们都是Oracle数据库提供的功能,以确保在系统出现故障时,应用程序能够继续运行,而不会中断服务。...

    SAP Notes 176337: On the difference between TYPE and LIKE

    ### SAP Notes 176337: 关于 TYPE 和 LIKE 的区别 #### 概述 在 SAP 的 ABAP 编程环境中,了解数据类型 (`Datatypes`) 和数据对象 (`Data Objects`) 之间的区别至关重要。这不仅关系到编程时如何正确声明变量,还...

    Difference between FX2 and FX2LP

    - 零长度IN包与ISO传输:FX2LP允许在无需固件干预的情况下处理零长度的IN包,并改进了ISO传输的数据PID顺序。 - ECC(Error-Correcting Code)生成:在FX2LP中,GPIF(通用可编程接口)数据现在支持ECC生成,增强...

    The Difference between eBay Buyers and Sellers when Facing Conflict

    易趣网买家和卖家解决在线争端的不同方式,董宝田,李玮,有关在线争端解决的研究开始于二十世纪末期,现如今,随着电子商务爆发式的发展,怎样解决在线争端变得尤为重要。作为全球最大的网上

    Fragmentation difference between the protonated and sodiated phosphonamidate peptide in multistage mass spectrometry

    N-磷酰化多肽的质子加合峰与钠离子加合峰的多级质谱裂解规律的差异性研究,刘艳,陈培燕,利用三种不同的磷酰化试剂,分别对胸腺肽的肌动蛋白结合域进行N-磷酰化修饰,并通过ESI-MS/MS对其进行序列测定。...

    difference between IE and Firefox

    在压缩包"develop in IE and Firefox"中,可能包含的是针对这两种浏览器开发的示例代码、测试用例或解决兼容性问题的策略,可以帮助开发者更好地理解并处理IE和Firefox之间的差异。通过深入研究这些文件,开发者可以...

    通信网基本概念与主体结构(第二版)答案

    通信网是信息技术领域中的核心部分,它连接了世界各地的人们,使得信息的传输变得迅速而高效。《通信网基本概念及其主体结构》第二版是一本深入探讨这一主题的专业书籍,其课后练习答案则为学习者提供了理解通信网...

    Mastering the C++17 .pdf

    The C++ language has a long history, dating back to the 1980s. Recently it has undergone a renaissance, ...class and function templates, the difference between lvalue and rvalue references, and so on.

    Z_08年阿尔卡特笔试面试题(无答案)

    1-1 In UMTS,what’s the difference between AAL2 and AAL5? 1-2 In UMTS,what do FDD and TDD mean? 1-3 Describe the processor of hardover and soft handover? 1-4 Describe the spread mechanism used in UMTS...

    金融模型的行为分析

    The reliance investors put on such quantitative analysis was catastrophic for the economy, setting off the ongoing string of financial crises that began with the mortgage market in 2007 and continues...

    VMWARE:Difference Between VMWare ESX and VMWare ESXi

    在虚拟化领域,VMware 是一个非常知名的提供商,其产品线包括了 ESX 和 ESXi 服务器。这两款产品都是为了帮助企业构建、管理和扩展虚拟化环境而设计的,但它们之间存在显著的区别。以下是 VMware ESX 服务器和 ESXi ...

    Art and Science of Analog Circuit Design

    A final difference is the heavy capitalistic and marketeering influence in many of the chapters. This unplanned emphasis is at center stage in sections by Grant, Williams, Brown, and others, and ...

Global site tag (gtag.js) - Google Analytics