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 > false > c.object_id == d.object_id // All symbols are same instance > true
Destructuring (assignment) in JavaScript
Destructuring assignment is a syntax in JavaScript (ES6) that enables the unpacking of values from arrays and objects into separate variables.
Regular assignment |
Destructured assignment |
const array = [1, 2, 3, 4]; const one = array[0]; const two = array[1]; const three = array[2]; const obj = { one: 1, two: 2, three: 3 }; const one = obj.one; const two = obj.two; const three = obj.three;
|
const array = [1, 2, 3, 4]; const [one, two, three] = array; const obj = { one: 1, two: 2, three: 3 }; const { one, two, three } = obj;
|
This Tech Bite was brought to you by Kenan Klisura, Lead Software Engineer at Atlantbh.
Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.