Last active
August 6, 2024 14:51
-
-
Save ryansobol/9b0b6995a7ae806cd008 to your computer and use it in GitHub Desktop.
Revisions
-
ryansobol revised this gist
Oct 23, 2014 . 1 changed file with 0 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -204,9 +204,3 @@ It displays the following: ``` **NOTE:** Ruby always uses double quotation marks when displaying multi-word Symbols. -
ryansobol revised this gist
Oct 23, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -58,7 +58,7 @@ name = 'john' name = name.upcase #=> 'JOHN' ``` In this example, a **copy** of the `'john'` String was created where each letter was capitalized to form `'JOHN'`. Afterwards, the `'JOHN'` String was reassigned to the `name` variable, replacing `'john'`. In other words, two **unique** Strings were created. Similarly, a Symbol can receive a variety of methods too: @@ -67,7 +67,7 @@ name = :john name = name.upcase #=> :JOHN ``` In this example, a **copy** of the `:john` Symbol was created where each letter was capitalized to form `:JOHN`. Afterwards, the `:JOHN` Symbol was reassigned to the `name` variable, replacing `:john`. In other words, two **unique** Symbols were created. ### How does a Symbol contrast to a String? -
ryansobol revised this gist
Oct 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -58,7 +58,7 @@ name = 'john' name = name.upcase #=> 'JOHN' ``` In this example, a **copy** of the `'john'` String was created where each letter was capitalized to form `'JOHN'`. Afterwards, the `'JOHN'` String was reassigned to the `name` variable, replacing `'john'`. In other words, two **unique** Strings we're created. Similarly, a Symbol can receive a variety of methods too: -
ryansobol revised this gist
Oct 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -67,7 +67,7 @@ name = :john name = name.upcase #=> :JOHN ``` In this example, a **copy** of the `:john` Symbol was created where each letter was capitalized to form `:JOHN`. Afterwards, the `:JOHN` Symbol was reassigned to the `name` variable, replacing `:john`. In other words, two **unique** Symbols we're created. ### How does a Symbol contrast to a String? -
ryansobol revised this gist
Oct 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -117,7 +117,7 @@ person = { :name => 'john' } person[:name] #=> 'john' ``` In this example, the first time the Ruby interpreter sees `:name`, it creates a Symbol. The next time it encounters `:name`, it reuses the same Symbol saving precious time and memory. In fact, this is so common, there's a shorthand way of writing the above example: -
ryansobol revised this gist
Oct 22, 2014 . No changes.There are no files selected for viewing
-
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 88 additions and 225 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,349 +1,212 @@ ### What's a Symbol and why is it imporant? In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters. ### What does a Symbol look like? Most commonly, a Symbol is a single word prefixed by a colon: ```ruby :hello ``` **NOTE:** You'll see an example of a multi-word Symbol later. When calling `puts` on a Symbol: ```ruby puts :hello ``` It displays the following: ``` hello ``` But when calling `p` on this Symbol: ```ruby p :hello ``` It displays the following: ``` :hello ``` ### How does a Symbol compare to a String? A String can be assigned to a variable: ```ruby name = 'john' ``` Similarly, a Symbol can be assigned to a variable too: ```ruby name = :john ``` Also, a String can receive a variety of methods: ```ruby name = 'john' name = name.upcase #=> 'JOHN' ``` In this example, a **copy** of the `'john'` String was created where each letter was capitalized to form `'JOHN'`. Afterwards, the `'JOHN'` String was reassigned to the `name` variable, replacing `'john'`. In other words, two **unique** Strings we're created. Similarly, a Symbol can receive a variety of methods too: ```ruby name = :john name = name.upcase #=> :JOHN ``` In this example, a **copy** of the `:john` Symbol was created where each letter was capitalized to form `:JOHN`. Afterwards, the `:JOHN` Symbol was reassigned to the `name` variable, replacing `:john`. In other words, two **unique** Symbols we're created. ### How does a Symbol contrast to a String? A String is **mutable**. That means its value can change while a program is running: ```ruby name = 'john' name.upcase! #=> 'JOHN' ``` In this example, the `'john'` String itself was capitalized to `'JOHN'`. **No copies** were made. And because only the value of the String has changed, the `name` variable doesn't need to be reassigned. In other words, one String was created and altered. Unlike a String, a Symbol is **immutable**. That means its value remains constant during the entirety of the program: ```ruby name = :john name.upcase! # NoMethodError: undefined method `upcase!' for :john:Symbol ``` In this example, there is no `upcase!` method for the `:john` Symbol. It's not possible to change the value of a Symbol like you can a String. In other words, once created, a Symbol cannot be altered. ### When do you use a Symbol? Strings with the same content refer to a different object: ```ruby 'john'.object_id #=> 927900 'john'.object_id #=> 928360 ``` In contrast, Symbols with the same content refer to the same object: ```ruby :john.object_id #=> 539688 :john.object_id #=> 539688 ``` In other words, two Strings with the exact same content are **two different objects**. However, two Symbols with the exact same content are **only one object**. Every time a program creates an object, it takes time and memory. This means **Symbols are more efficient than Strings**. For this reason, Symbols make great Hash keys: ```ruby person = { :name => 'john' } person[:name] #=> 'john' ``` In this example, the first time the Ruby interpreter sees `:name`, it creates a Symbol. The next time it encounters `:name`, it reuses the same Symbol saving precious time and memory. In fact, this is so common, there's a shorthand way of writing the above example: ```ruby person = { name: 'john' } person[:name] #=> 'john' ``` In this example, the colon moved to the right of the word `name` and the hash rocket `=>` disappeared. **TIP:** This syntax for Symbols, where the colon is to the right of the word, is only available when defining a Hash using curly braces `{}`. ### When do you use a String? If Symbols are more efficient than Strings, why bother with Strings at all? One reason is that Strings have a richer set of methods. For example, two Strings can be added together, but two Symbols cannot. ```ruby 'jo' + 'hn' #=> 'john' :jo + :hn # NoMethodError: undefined method `+' for :jo:Symbol ``` A String can be centered, while a Symbol cannot. ```ruby 'john'.center(6) #=> ' john ' :john.center(6) # NoMethodError: undefined method `center' for :john:Symbol ``` A String can be queried about its content, but a Symbol cannot. ```ruby 'john'.include?('jo') #=> true :john.include?(:jo) # NoMethodError: undefined method `center' for :john:Symbol ``` Strings have [many methods](http://www.ruby-doc.org/core-2.1.3/String.html), like the ones above, that make working with textual content much easier. ### How do you convert a String to a Symbol and vice versa? Quite easily, actually: ```ruby 'john'.to_sym #=> :john :john.to_s #=> 'john' ``` In both cases, a **copy** is made of the content and either a new Symbol or String is returned. ### What does a multi-word Symbol look like? Less commonly, a Symbol is also multiple words, wrapped inside quotation marks and prefixed by a colon: ```ruby :'john lennon' ``` When calling `puts` on a multi-word Symbol: ```ruby puts :'john lennon' ``` It displays the following: ``` john lennon ``` But when calling `p` on this Symbol: ```ruby p :'john lennon' ``` It displays the following: ``` :"john lennon" ``` **NOTE:** Ruby always uses double quotation marks when displaying multi-word Symbols. ### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. If content is important, **use a String**. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -240,7 +240,7 @@ Though it's rarely done, a Hash can even be keyed by an Array or a Hash. **NOTE:** It's extremely common for a Hash to be keyed by a Symbol -- another object type you'll see shortly. #### Index Order vs Value Order An Array is ordered by the **numerical value** of each index, which always starts at `0`: -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 225 additions and 88 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,212 +1,349 @@ ### What's a Hash and why is it important? A Hash is a collection of key-value pairs. To add, fetch, modify, and delete a value from a Hash, you refer to it with a unique key. While an Array is indexed by Integers only, a Hash is keyed by any object -- Strings, Integers, etc. In other programming languages, a Hash might be known as an 'associative array', 'dictionary', or 'HashMap'. ### What does a Hash look like? The empty Hash is just two curly braces: ```ruby {} ``` To create a Hash with a single key-value pair, place the pair inside the curly braces, separated by a hash-rocket `=>`: ```ruby { 'name' => 'john' } ``` In this example, `'name'` is the **key** and `'john'` is the corresponding **value**. **NOTE:** One space after the opening curly brace `{`, before the closing curly brace `}`, and around the hash-rocket `=>` is considered good style. Multiple key-value pairs are further separated by a comma and a space: ```ruby { 'name' => 'john', 'age' => 40 } ``` In this example, `'name'` and `'age'` are the **keys**. And `'john'` and `40` are their corresponding **values**. When calling `puts` on a Hash: ```ruby puts({ 'name' => 'john', 'age' => 40 }) ``` **NOTE:** Ruby will fail with a `SyntaxError` without the parenthesis. It displays the following: ```ruby {"name"=>"john", "age"=>40} ``` **NOTE:** Ruby always uses double quotation marks when displaying Strings. Similarly, when calling `p` on this Hash: ```ruby p({ 'name' => 'john', 'age' => 40 }) ``` **NOTE:** Again, Ruby will fail with a `SyntaxError` without the parenthesis. It also displays the following: ```ruby {"name"=>"john", "age"=>40} ``` ### How does a Hash compare to an Array? A Hash and an Array have many commonalities. For example, an Array can be assigned to a variable: ```ruby person = ['john', 40] ``` Similarly, a Hash can be assigned to a variable too: ```ruby person = { 'name' => 'john', 'age' => 40 } ``` An Array can receive a variety of methods: ```ruby person = ['john', 40] person.size #=> 2 ``` In this example, an Array with two **elements** is assigned to the `person` variable. Similarly, a Hash can receive a variety of methods too: ```ruby person = { 'name' => 'john', 'age' => 40 } person.size #=> 2 ``` In this example, a Hash with two **key-value pairs** is assigned to the `person` variable. #### Fetching from a container A single **element** of an Array can be fetched by it's **index**: ```ruby person = ['john', 40] person[0] #=> 'john' ``` Likewise, a single **value** of a Hash can be fetched by it's **key**: ```ruby person = { 'name' => 'john', 'age' => 40 } person['name'] #=> 'john' ``` Fetching an **element** from an Array with a non-existent **index** returns `nil`: ```ruby person = ['john', 40] person[2] #=> nil ``` Likewise, fetching a **value** from a Hash with a non-existent **key** also returns `nil`: ```ruby person = { 'name' => 'john', 'age' => 40 } person['location'] #=> nil ``` #### Modifying a container Given an Array **index**, it's **element** can be reassigned: ```ruby person = ['john', 40] person[0] = 'paul' ``` Likewise, given a Hash **key**, it's **value** can be reassigned: ```ruby person = { 'name' => 'john', 'age' => 40 } person['name'] = 'paul' ``` #### Deleting from a container Given an Array **index**, it's **element** can be deleted from the Array: ```ruby person = ['john', 40] person.delete_at(0) #=> 'john' person #=> [40] ``` **NOTE:** The element `40` is now at index `0`. Likewise, given a Hash **key**, it's **key-value pair** can be deleted: ```ruby person = { 'name' => 'john', 'age' => 40 } person.delete('name') #=> 'john' person #=> { 'age' => 40 } ``` **NOTE:** The value `40` is still at key `'age'`. #### Iterating over a container Each **element** and **index** combination of an Array can be iterated over: ```ruby person = ['john', 40] person.each_with_index do |element, index| puts "#{element} at #{index}" end ``` Which displays the following: ```ruby john at 0 40 at 1 ``` Similarly, each **key-value pair** of a Hash can be iterated over: ```ruby person = { 'name' => 'john', 'age' => 40 } person.each do |key, value| puts "#{key}: #{value}" end ``` Which displays the following: ``` name: john age: 40 ``` ### How does a Hash contrast to an Array? Even with some minor quirks, Hashes and Arrays are rather similar. However, there are a few major differences. #### Index Type vs Key Type An Array is indexed by Integers only: ```ruby person = ['john', 40] person[0] #=> 'john' person[1] #=> 40 ``` While a Hash is keyed by any object. For example, by Strings: ```ruby person = { 'name' => 'john', 'age' => 40 } person['name'] #=> 'john' person['age'] #=> 40 ``` Or by Integers: ```ruby person = { 1 => 'john', 2 => 40 } person[1] #=> 'john' person[2] #=> 40 ``` Or by a mixture of both Strings and Integers: ```ruby person = { 'name' => 'john', 2 => 40 } person['name'] #=> 'john' person[2] #=> 40 ``` Though it's rarely done, a Hash can even be keyed by an Array or a Hash. **NOTE:** It's extremely common for a Hash to be keyed by a Symbol -- another object type you'll see shortly. ### Index Order vs Value Order An Array is ordered by the **numerical value** of each index, which always starts at `0`: ```ruby person = ['john', 40] person.each_with_index do |element, index| puts "#{element} at #{index}" end ``` Which displays the following: ```ruby john at 0 40 at 1 ``` On the other hand, a Hash is ordered by the **insertion order** of each the key-value pair: ```ruby person = { 'name' => 'john', 'age' => 40 } person.each do |key, value| puts "#{key}: #{value}" end ``` Which displays the following: ``` name: john age: 40 ``` #### Adding an Element vs Adding a Key-Value Pair There are a variety of ways to add an element to an Array. It can be pushed to the end of an Array by using either the `push` method or shovel `<<` operator: ```ruby person = ['john', 40] person.push('paul') #=> ['john', 40, 'paul'] person << 72 #=> ['john', 40, 'paul', 72] ``` It can be unshifted to the beginning of an Array by using the `unshift` method: ```ruby person = ['john', 40] person.unshift('paul') #=> ['paul', 'john', 40] ``` And it can be inserted at a specific index of the Array with the `insert` method: ```ruby person = ['john', 40] person.insert(1, 'paul') #=> ['john', 'paul', 40] ``` All of these techniques make it easy for you to manage the order of the elements in an Array. On the other hand, there is only one way to add a key-value pair to a Hash. And that is with the assignment `=` operator: ```ruby person = { 'name' => 'john', 'age' => 40 } person['friend'] = 'paul' person #=> { 'name' => 'john', 'age' => 40, 'friend' => 'paul' } ``` In this example, the value `'paul'` was assigned to the `'friend'` key. Since the `'friend'` key didn't exist in the Hash, the key-value pair was then added to the end. #### Concatenation Arrays vs Merging Hashes Two Arrays can be concatenated together, forming a new Array containing all the elements of both Arrays: ```ruby person = ['john'] + [40] person #=> ['john', 40] ``` However, two Hashes cannot be concatenated together: ```ruby person = { 'name' => 'john' } + { 'age' => 40 } # NoMethodError: undefined method `+' for {"name"=>"john"}:Hash ``` Instead, two Hashes can be merged together: ```ruby person = { 'name' => 'john' }.merge('age' => 40) person #=> { 'name' => 'john', 'age' => 40 } ``` **NOTE:** A Hash used as a method argument does not need the curly braces `{}`. At first glace, merging looks just like concatenation. However, when both Hashes have the same key, the Hash inside the `merge` method takes precedence: ```ruby person = { 'name' => 'john', 'age' => 40 }.merge('age' => 35) person #=> { 'name' => 'john', 'age' => 35 } ``` In this example, both Hashes contain the `'age'` key. When merged together, the `'age' => 35` key-value pair overwrites the `'age' => 40` key-value pair. In other words, key-value pairs from the Hash inside the `merge` method overwrite key-value pairs from the Hash receiving the method call. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,8 @@ ### What's a Symbol and why is it imporant? In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters. ### What does a Symbol look like? Most commonly, a Symbol is a single word prefixed by a colon: @@ -36,7 +36,7 @@ It displays the following: :hello ``` ### How does a Symbol compare to a String? A String can be assigned to a variable: @@ -69,7 +69,7 @@ name = name.upcase #=> :JOHN In this example, a **copy** of the `:john` Symbol was created where each letter was capitalized to form `:JOHN`. Afterwards, the `:JOHN` Symbol was reassigned to the `name` variable, replacing `:john`. In other words, two **unique** Symbols we're created. ### How does a Symbol contrast to a String? A String is **mutable**. That means its value can change while a program is running: @@ -89,7 +89,7 @@ name.upcase! # NoMethodError: undefined method `upcase!' for :john:Symbol In this example, there is no `upcase!` method for the `:john` Symbol. It's not possible to change the value of a Symbol like you can a String. In other words, once created, a Symbol cannot be altered. ### When do you use a Symbol? Strings with the same content refer to a different object: @@ -131,7 +131,7 @@ In this example, the colon moved to the right of the word `name` and the hash ro **TIP:** This syntax for Symbols, where the colon is to the right of the word, is only available when defining a Hash using curly braces `{}`. ### When do you use a String? If Symbols are more efficient than Strings, why bother with Strings at all? @@ -160,7 +160,7 @@ A String can be queried about its content, but a Symbol cannot. Strings have [many methods](http://www.ruby-doc.org/core-2.1.3/String.html), like the ones above, that make working with textual content much easier. ### How do you convert a String to a Symbol and vice versa? Quite easily, actually: @@ -171,7 +171,7 @@ Quite easily, actually: In both cases, a **copy** is made of the content and either a new Symbol or String is returned. ### What does a multi-word Symbol look like? Less commonly, a Symbol is also multiple words, wrapped inside quotation marks and prefixed by a colon: @@ -205,7 +205,7 @@ It displays the following: **NOTE:** Ruby always uses double quotation marks when displaying multi-word Symbols. ### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #### What's a Symbol and why is it imporant? In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -173,7 +173,7 @@ In both cases, a **copy** is made of the content and either a new Symbol or Stri #### What does a multi-word Symbol look like? Less commonly, a Symbol is also multiple words, wrapped inside quotation marks and prefixed by a colon: ```ruby :'john lennon' -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ It displays the following: hello ``` But when calling `p` on this Symbol: ```ruby p :hello @@ -191,7 +191,7 @@ It displays the following: john lennon ``` But when calling `p` on this Symbol: ```ruby p :'john lennon' -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -173,7 +173,7 @@ In both cases, a **copy** is made of the content and either a new Symbol or Stri #### What does a multi-word Symbol look like? Less commonly, a Symbol is also multiple words, wrapped inside quotations and prefixed by a colon: ```ruby :'john lennon' -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,13 +4,13 @@ In Ruby, a Symbol is the most efficient way, in terms of time and memory, to rep #### What does a Symbol look like? Most commonly, a Symbol is a single word prefixed by a colon: ```ruby :hello ``` **NOTE:** You'll see an example of a multi-word Symbol later. When calling `puts` on a Symbol: @@ -173,7 +173,7 @@ In both cases, a **copy** is made of the content and either a new Symbol or Stri #### What does a multi-word Symbol look like? A Symbol can also contain multiple words, wrapped inside quotations and prefixed by a colon: ```ruby :'john lennon' -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -203,6 +203,8 @@ It displays the following: :"john lennon" ``` **NOTE:** Ruby always uses double quotation marks when displaying multi-word Symbols. #### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,8 @@ Most commonly, a Symbol is represented by a single word prefixed by a colon: :hello ``` **NOTE:** You'll see how a Symbol can represent multiple words later. When calling `puts` on a Symbol: ```ruby @@ -169,9 +171,9 @@ Quite easily, actually: In both cases, a **copy** is made of the content and either a new Symbol or String is returned. #### What does a multi-word Symbol look like? A Symbol can also represent multiple words, wrapped inside quotations and prefixed by a colon: ```ruby :'john lennon' -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,6 @@ Most commonly, a Symbol is represented by a single word prefixed by a colon: :hello ``` When calling `puts` on a Symbol: ```ruby -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 62 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -41,68 +41,68 @@ It displays the following: A String can be assigned to a variable: ```ruby name = 'john' ``` Similarly, a Symbol can be assigned to a variable too: ```ruby name = :john ``` Also, a String can receive a variety of methods: ```ruby name = 'john' name = name.upcase #=> 'JOHN' ``` In this example, a **copy** of the `'john'` String was created where each letter was capitalized to form `'JOHN'`. Afterwards, the `'JOHN'` String was reassigned to the `name` variable, replacing `'john'`. In other words, two **unique** Strings we're created. Similarly, a Symbol can receive a variety of methods too: ```ruby name = :john name = name.upcase #=> :JOHN ``` In this example, a **copy** of the `:john` Symbol was created where each letter was capitalized to form `:JOHN`. Afterwards, the `:JOHN` Symbol was reassigned to the `name` variable, replacing `:john`. In other words, two **unique** Symbols we're created. #### How does a Symbol contrast to a String? A String is **mutable**. That means its value can change while a program is running: ```ruby name = 'john' name.upcase! #=> 'JOHN' ``` In this example, the `'john'` String itself was capitalized to `'JOHN'`. **No copies** were made. And because only the value of the String has changed, the `name` variable doesn't need to be reassigned. In other words, one String was created and altered. Unlike a String, a Symbol is **immutable**. That means its value remains constant during the entirety of the program: ```ruby name = :john name.upcase! # NoMethodError: undefined method `upcase!' for :john:Symbol ``` In this example, there is no `upcase!` method for the `:john` Symbol. It's not possible to change the value of a Symbol like you can a String. In other words, once created, a Symbol cannot be altered. #### When do you use a Symbol? Strings with the same content refer to a different object: ```ruby 'john'.object_id #=> 927900 'john'.object_id #=> 928360 ``` In contrast, Symbols with the same content refer to the same object: ```ruby :john.object_id #=> 539688 :john.object_id #=> 539688 ``` In other words, two Strings with the exact same content are **two different objects**. However, two Symbols with the exact same content are **only one object**. @@ -112,19 +112,19 @@ Every time a program creates an object, it takes time and memory. This means **S For this reason, Symbols make great Hash keys: ```ruby person = { :name => 'john' } person[:name] #=> 'john' ``` In this example, the first time the Ruby interpreter sees `:name`, it creates a Symbol. The next time it encounters `:name`, it reuses the same Symbol saving precious time and memory. In fact, this is so common, there's a shorthand way of writing the above example: ```ruby person = { name: 'john' } person[:name] #=> 'john' ``` In this example, the colon moved to the right of the word `name` and the hash rocket `=>` disappeared. @@ -140,22 +140,22 @@ One reason is that Strings have a richer set of methods. For example, two Strings can be added together, but two Symbols cannot. ```ruby 'jo' + 'hn' #=> 'john' :jo + :hn # NoMethodError: undefined method `+' for :jo:Symbol ``` A String can be centered, while a Symbol cannot. ```ruby 'john'.center(6) #=> ' john ' :john.center(6) # NoMethodError: undefined method `center' for :john:Symbol ``` A String can be queried about its content, but a Symbol cannot. ```ruby 'john'.include?('jo') #=> true :john.include?(:jo) # NoMethodError: undefined method `center' for :john:Symbol ``` Strings have [many methods](http://www.ruby-doc.org/core-2.1.3/String.html), like the ones above, that make working with textual content much easier. @@ -165,12 +165,44 @@ Strings have [many methods](http://www.ruby-doc.org/core-2.1.3/String.html), lik Quite easily, actually: ```ruby 'john'.to_sym #=> :john :john.to_s #=> 'john' ``` In both cases, a **copy** is made of the content and either a new Symbol or String is returned. #### How can a Symbol represent multiple words? A Symbol can also be represented by a multiple word wrapped inside quotations and prefixed by a colon: ```ruby :'john lennon' ``` When calling `puts` on a multi-word Symbol: ```ruby puts :'john lennon' ``` It displays the following: ``` john lennon ``` But when calling `p` on a Symbol: ```ruby p :'john lennon' ``` It displays the following: ``` :"john lennon" ``` #### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,12 +4,14 @@ In Ruby, a Symbol is the most efficient way, in terms of time and memory, to rep #### What does a Symbol look like? Most commonly, a Symbol is represented by a single word prefixed by a colon: ```ruby :hello ``` **NOTE:** You'll see how a Symbol can represent multiple words later. When calling `puts` on a Symbol: ```ruby -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #### What's a Symbol and why are they imporant? In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters. -
ryansobol revised this gist
Oct 21, 2014 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,10 @@ #### What's a Symbol and why should I care? In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters. #### What does a Symbol look like? A Symbol is represented by a word prefixed by a colon: ```ruby :hello -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,18 @@ It displays the following: hello ``` But when calling `p` on a Symbol: ```ruby p :hello ``` It displays the following: ``` :hello ``` #### How does a Symbol compare to a String? A String can be assigned to a variable: -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ #### What does a Symbol look like? A Symbol is a word prefixed by a colon: ```ruby :hello -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -73,14 +73,14 @@ In this example, there is no `upcase!` method for the `:josh` Symbol. It's not p #### When do you use a Symbol? Strings with the same content refer to a different object: ```ruby 'josh'.object_id #=> 927900 'josh'.object_id #=> 928360 ``` In contrast, Symbols with the same content refer to the same object: ```ruby :josh.object_id #=> 539688 -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #### What does a Symbol look like? A Symbol is a colon followed by a single word: @@ -18,7 +18,7 @@ It displays the following: hello ``` #### How does a Symbol compare to a String? A String can be assigned to a variable: @@ -51,7 +51,7 @@ name = name.upcase #=> :JOSH In this example, a **copy** of the `:josh` Symbol was created where each letter was capitalized to form `:JOSH`. Afterwards, the `:JOSH` Symbol was reassigned to the `name` variable, replacing `:josh`. In other words, two **unique** Symbols we're created. #### How does a Symbol contrast to a String? A String is **mutable**. That means its value can change while a program is running: @@ -71,7 +71,7 @@ name.upcase! # NoMethodError: undefined method `upcase!' for :josh:Symbol In this example, there is no `upcase!` method for the `:josh` Symbol. It's not possible to change the value of a Symbol like you can a String. In other words, once created, a Symbol cannot be altered. #### When do you use a Symbol? Every String with the same content refers to a different object: @@ -113,7 +113,7 @@ In this example, the colon moved to the right of the word `name` and the hash ro **TIP:** This syntax for Symbols, where the colon is to the right of the word, is only available when defining a Hash using curly braces `{}`. #### When do you use a String? If Symbols are more efficient than Strings, why bother with Strings at all? @@ -142,7 +142,7 @@ A String can be queried about its content, but a Symbol cannot. Strings have [many methods](http://www.ruby-doc.org/core-2.1.3/String.html), like the ones above, that make working with textual content much easier. #### How do you convert a String to a Symbol and vice versa? Quite easily, actually: @@ -153,7 +153,7 @@ Quite easily, actually: In both cases, a **copy** is made of the content and either a new Symbol or String is returned. #### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -151,6 +151,8 @@ Quite easily, actually: :josh.to_s #=> 'josh' ``` In both cases, a **copy** is made of the content and either a new Symbol or String is returned. ### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -142,12 +142,6 @@ A String can be queried about its content, but a Symbol cannot. Strings have [many methods](http://www.ruby-doc.org/core-2.1.3/String.html), like the ones above, that make working with textual content much easier. ### How do you convert a String to a Symbol and vice versa? Quite easily, actually: @@ -156,3 +150,9 @@ Quite easily, actually: 'josh'.to_sym #=> :josh :josh.to_s #=> 'josh' ``` ### In practice, how do you remember which one to use? If identity is important, **use a Symbol**. If content is important, **use a String**. -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -109,7 +109,7 @@ person = { name: 'josh' } person[:name] #=> 'josh' ``` In this example, the colon moved to the right of the word `name` and the hash rocket `=>` disappeared. **TIP:** This syntax for Symbols, where the colon is to the right of the word, is only available when defining a Hash using curly braces `{}`. -
ryansobol revised this gist
Oct 20, 2014 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -73,18 +73,18 @@ In this example, there is no `upcase!` method for the `:josh` Symbol. It's not p ### When do you use a Symbol? Every String with the same content refers to a different object: ```ruby 'josh'.object_id #=> 927900 'josh'.object_id #=> 928360 ``` In contrast, every Symbol with the same content refers to the same object: ```ruby :josh.object_id #=> 539688 :josh.object_id #=> 539688 ``` In other words, two Strings with the exact same content are **two different objects**. However, two Symbols with the exact same content are **only one object**.
NewerOlder