Created
February 8, 2018 02:16
-
-
Save profh/536bd0850d1e0969b84d95bdbb82acdf to your computer and use it in GitHub Desktop.
Understanding Ruby Symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple method format purposes | |
def brk | |
puts "============================" | |
end | |
# Finding all symbols and symbol methods that Ruby already knows: | |
symbols = Symbol.all_symbols.sort{|x,y| x.to_s <=> y.to_s } | |
symbols.each { |x| print x.to_s + "\t" } | |
brk | |
# Strings and Symbols (see these symbols added to the symbol table above) | |
puts "duck" # this is a string | |
puts :"duck" # the : converts the string to a symbol | |
puts :duck # since it is so common, Ruby let's us drop the quotation marks | |
puts "duck".to_sym # this is a string converted to a symbol with .to_sym method | |
puts %s[goose] # %s[goose].class # => Symbol | |
brk | |
# | |
# | |
# Adding more symbols to the symbols table | |
:mallard | |
:ring_neck | |
# Adding methods also creates symbols | |
def quack | |
puts "quack" | |
end | |
# Adding classes (and their associated methods) adds more symbols | |
class Duck | |
attr_accessor :breed, :gender | |
def walk | |
puts "waddle" | |
end | |
end | |
# Difference between strings and symbols is that | |
# same string repeated creates separate objects (and | |
# uses more memory) while a symbol is created once and | |
# linked to many times (saving memory). | |
puts "favorite".object_id.to_s | |
puts "favorite".object_id.to_s | |
puts "favorite".object_id.to_s | |
brk | |
puts :favorite.object_id.to_s | |
puts :favorite.object_id.to_s | |
puts :favorite.object_id.to_s | |
brk | |
# Symbols are much faster than strings | |
require 'benchmark' | |
str_time = Benchmark.measure { for i in 1..10_000_000; duck = "mallard" end} | |
sym_time = Benchmark.measure { for i in 1..10_000_000; duck = :mallard end} | |
puts str_time.format("[String] Total CPU time: %t") | |
puts sym_time.format("[Symbol] Total CPU time: %t") | |
puts "Ratio str/sym: #{str_time.total.to_f/sym_time.total.to_f}" | |
brk | |
### BOTTOM LINE: Symbols are immutable strings that take less memory | |
### and are faster to implement than strings. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment