What is it?
Programming on the Toilet is a document which contains tips, tricks, snippets or explanations about various programming technologies and paradigms, which could help developers in their everyday job. This episode was brought to you by Kenan Klisura, Lead Software Engineer at Atlantbh.
Symbols and variables in Ruby
Symbols and variables are very different things in Ruby. Symbols are like strings, they’re immutable and behave like constants. All equal symbols point to the same memory location. Variables, on the other hand, are references to some objects and can reference symbols.
a = “test” // a, b variable that points to “test” string
a = “test”
c = :test // c, d variable that points to :test symbol
d = :test
> a.object_id == b.object_id // Strings with different instance
> c.object_id == d.object_id // All symbols are same instance
Want to share these tips in your workplace or at home? Download this episode of Programming on the Toilet here.