January 2019
Intermediate to advanced
246 pages
5h 23m
English
The strings you’ve been using in this chapter are flexible, but that flexibility imposes performance costs. Suppose you need to sift through a huge file with millions of mineral objects and give each their mineral name, essentially assigning an identifier. You’d be better off using Symbols here, because every instance of a given Symbol is exactly the same object:
| | class Mineral |
| | property name |
| | |
| | def initialize(@name : Symbol) |
| | end |
| | end |
| | |
| | mineral1 = Mineral.new(:talc) |
| | mineral8547 = Mineral.new(:talc) |
| | |
| | p mineral1.name |
| | # => :talc |
| | p mineral8547.name # => :talc |
| | p :talc # => :talc |
| | |
| | mineral8547.name = :gold |
| | :gold.class # => Symbol |
Because ...
Read now
Unlock full access