본문 바로가기
조회 수 1494 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

What is the difference between a Symbol and String?


- Symbols and string are used interchangeably by various developers and their usage within gems can be confusing at times.
- You can think of Symbols as faster & immutable strings. 
- Once a string is used up it is marked for cleaning by the garbage collector but it is not cleaned up immediately and it cannot be reused. 
- Symbols live for the duration of the session. You might say that this leads to increased memory usage however by keeping the symbol alive a bit longer it can be reused again.

Here’s a terminal irb session that will provide more insight.

puts :"I am a symbol".object_id
457908

puts :"I am a symbol".object_id
457908

puts :"I am a symbol".object_id
457908

puts "I am a string".object_id
70343000106700

puts "I am a string".object_id
70343000094220

Notice that the object_id stays the same when symbols are created. This happens because the ruby interpreter uses the same heap memory location each time. The symbol was never completely released. However, in the case of strings the memory is marked for cleanup each time and a new memory is allocated.

Another big difference is that strings can be modified. They are mutable.
So, this would work:

puts "I am a string" << " for you"
I am a string for you

However, symbols are immutable.
So, this would throw an error:

puts :"I am a symbol" << :" for you"
NoMethodError: undefined method `<<' for :"I am a symbol":Symbol


Title
List of Articles
번호 제목 글쓴이 날짜 조회 수
31 Different Ways to Set Attributes in ActiveRecord (attribute, create, update, validation) Hojung 2015.05.24 1271
30 Ruby Style Guide Hojung 2015.02.05 1610
29 Refresh content automatically after some period time on Rails Hojung 2015.01.22 2214
28 Install ruby-filemagic on Mac (brew, libmagic) Hojung 2014.12.03 1476
27 Byte manipulation in Ruby Hojung 2014.12.03 1436
26 How to use Github for your project file Hojung 2014.09.24 1053
25 Ruby map, each, collect, inject, reject, select quick reference Hojung 2014.06.10 1815
24 Difference between collect, select, map and each in ruby Hojung 2014.06.10 1298
23 remove nil from array (compact, compact!) Hojung 2014.06.10 1605
22 remove child association model when parent removed (rails-dependent-destroy) Hojung 2014.06.03 1663
21 deploying cron jobs with whenever gem Hojung 2014.06.01 1703
20 Get list of a class' instance methods Hojung 2014.06.01 1366
19 Optional method parameters in Ruby Hojung 2014.05.29 1487
18 Best Ruby On Rails Content Management Systems (CMS) Hojung 2014.04.26 2683
17 The simple way to print exceptions in Ruby Hojung 2014.04.07 1518
16 Ruby Symbols Hojung 2014.04.05 6291
» What is the difference between a Symbol and String? Hojung 2014.04.05 1494
14 install Rbenv, ruby and rails Hojung 2014.03.22 1672
13 HTTP Posts in Ruby Hojung 2014.03.14 1666
12 optparse: mandatory and optional arguments example Hojung 2014.03.14 2353
Board Pagination ‹ Prev 1 2 Next ›
/ 2

Designed by sketchbooks.co.kr / sketchbook5 board skin

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5