Skip to content

Instantly share code, notes, and snippets.

@ryansobol
Last active August 6, 2024 14:51
Show Gist options
  • Save ryansobol/9b0b6995a7ae806cd008 to your computer and use it in GitHub Desktop.
Save ryansobol/9b0b6995a7ae806cd008 to your computer and use it in GitHub Desktop.

Revisions

  1. ryansobol revised this gist Oct 23, 2014. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions symbols.md
    Original 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.

    ### In practice, how do you remember which one to use?

    If identity is important, **use a Symbol**.

    If content is important, **use a String**.
  2. ryansobol revised this gist Oct 23, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions symbols.md
    Original 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.
    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 we're created.
    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?

  3. ryansobol revised this gist Oct 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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.
    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:

  4. ryansobol revised this gist Oct 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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.
    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?

  5. ryansobol revised this gist Oct 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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 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:

  6. ryansobol revised this gist Oct 22, 2014. No changes.
  7. ryansobol revised this gist Oct 21, 2014. 1 changed file with 88 additions and 225 deletions.
    313 changes: 88 additions & 225 deletions symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,349 +1,212 @@
    ### What's a Hash and why is it important?
    ### What's a Symbol and why is it imporant?

    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.
    In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters.

    While an Array is indexed by Integers only, a Hash is keyed by any object -- Strings, Integers, etc.
    ### What does a Symbol look like?

    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 `=>`:
    Most commonly, a Symbol is a single word prefixed by a colon:

    ```ruby
    { 'name' => 'john' }
    :hello
    ```

    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.
    **NOTE:** You'll see an example of a multi-word Symbol later.

    Multiple key-value pairs are further separated by a comma and a space:
    When calling `puts` on a Symbol:

    ```ruby
    { 'name' => 'john', 'age' => 40 }
    puts :hello
    ```

    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 })
    hello
    ```

    **NOTE:** Again, Ruby will fail with a `SyntaxError` without the parenthesis.

    It also displays the following:
    But when calling `p` on this Symbol:

    ```ruby
    {"name"=>"john", "age"=>40}
    p :hello
    ```

    ### 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:
    It displays the following:

    ```ruby
    person = ['john', 40]
    ```

    Similarly, a Hash can be assigned to a variable too:

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    :hello
    ```

    An Array can receive a variety of methods:
    ### How does a Symbol compare to a String?

    A String can be assigned to a variable:

    ```ruby
    person = ['john', 40]
    person.size #=> 2
    name = 'john'
    ```

    In this example, an Array with two **elements** is assigned to the `person` variable.

    Similarly, a Hash can receive a variety of methods too:
    Similarly, a Symbol can be assigned to a variable too:

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    person.size #=> 2
    name = :john
    ```

    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**:
    Also, a String can receive a variety of methods:

    ```ruby
    person = ['john', 40]
    person[0] #=> 'john'
    name = 'john'
    name = name.upcase #=> 'JOHN'
    ```

    Likewise, a single **value** of a Hash can be fetched by it's **key**:
    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
    person = { 'name' => 'john', 'age' => 40 }
    person['name'] #=> 'john'
    name = :john
    name = name.upcase #=> :JOHN
    ```

    Fetching an **element** from an Array with a non-existent **index** returns `nil`:
    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.

    ```ruby
    person = ['john', 40]
    person[2] #=> nil
    ```
    ### How does a Symbol contrast to a String?

    Likewise, fetching a **value** from a Hash with a non-existent **key** also returns `nil`:
    A String is **mutable**. That means its value can change while a program is running:

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    person['location'] #=> nil
    name = 'john'
    name.upcase! #=> 'JOHN'
    ```

    #### Modifying a container
    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.

    Given an Array **index**, it's **element** can be reassigned:
    Unlike a String, a Symbol is **immutable**. That means its value remains constant during the entirety of the program:

    ```ruby
    person = ['john', 40]
    person[0] = 'paul'
    name = :john
    name.upcase! # NoMethodError: undefined method `upcase!' for :john:Symbol
    ```

    Likewise, given a Hash **key**, it's **value** can be reassigned:
    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.

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    person['name'] = 'paul'
    ```
    ### When do you use a Symbol?

    #### Deleting from a container

    Given an Array **index**, it's **element** can be deleted from the Array:
    Strings with the same content refer to a different object:

    ```ruby
    person = ['john', 40]
    person.delete_at(0) #=> 'john'
    person #=> [40]
    'john'.object_id #=> 927900
    'john'.object_id #=> 928360
    ```

    **NOTE:** The element `40` is now at index `0`.

    Likewise, given a Hash **key**, it's **key-value pair** can be deleted:
    In contrast, Symbols with the same content refer to the same object:

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    person.delete('name') #=> 'john'
    person #=> { 'age' => 40 }
    :john.object_id #=> 539688
    :john.object_id #=> 539688
    ```

    **NOTE:** The value `40` is still at key `'age'`.
    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**.

    #### Iterating over a container
    Every time a program creates an object, it takes time and memory. This means **Symbols are more efficient than Strings**.

    Each **element** and **index** combination of an Array can be iterated over:
    For this reason, Symbols make great Hash keys:

    ```ruby
    person = ['john', 40]
    person = { :name => 'john' }

    person.each_with_index do |element, index|
    puts "#{element} at #{index}"
    end
    person[:name] #=> 'john'
    ```

    Which displays the following:

    ```ruby
    john at 0
    40 at 1
    ```
    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.

    Similarly, each **key-value pair** of a Hash can be iterated over:
    In fact, this is so common, there's a shorthand way of writing the above example:

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }

    person.each do |key, value|
    puts "#{key}: #{value}"
    end
    ```
    person = { name: 'john' }

    Which displays the following:

    ```
    name: john
    age: 40
    person[:name] #=> 'john'
    ```

    ### How does a Hash contrast to an Array?
    In this example, the colon moved to the right of the word `name` and the hash rocket `=>` disappeared.

    Even with some minor quirks, Hashes and Arrays are rather similar. However, there are a few major differences.
    **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 `{}`.

    #### Index Type vs Key Type
    ### When do you use a String?

    An Array is indexed by Integers only:
    If Symbols are more efficient than Strings, why bother with Strings at all?

    ```ruby
    person = ['john', 40]
    person[0] #=> 'john'
    person[1] #=> 40
    ```
    One reason is that Strings have a richer set of methods.

    While a Hash is keyed by any object. For example, by Strings:
    For example, two Strings can be added together, but two Symbols cannot.

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    person['name'] #=> 'john'
    person['age'] #=> 40
    'jo' + 'hn' #=> 'john'
    :jo + :hn # NoMethodError: undefined method `+' for :jo:Symbol
    ```

    Or by Integers:
    A String can be centered, while a Symbol cannot.

    ```ruby
    person = { 1 => 'john', 2 => 40 }
    person[1] #=> 'john'
    person[2] #=> 40
    'john'.center(6) #=> ' john '
    :john.center(6) # NoMethodError: undefined method `center' for :john:Symbol
    ```

    Or by a mixture of both Strings and Integers:
    A String can be queried about its content, but a Symbol cannot.

    ```ruby
    person = { 'name' => 'john', 2 => 40 }
    person['name'] #=> 'john'
    person[2] #=> 40
    'john'.include?('jo') #=> true
    :john.include?(:jo) # NoMethodError: undefined method `center' for :john:Symbol
    ```

    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.
    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.

    #### Index Order vs Value Order
    ### How do you convert a String to a Symbol and vice versa?

    An Array is ordered by the **numerical value** of each index, which always starts at `0`:
    Quite easily, actually:

    ```ruby
    person = ['john', 40]

    person.each_with_index do |element, index|
    puts "#{element} at #{index}"
    end
    'john'.to_sym #=> :john
    :john.to_s #=> 'john'
    ```

    Which displays the following:
    In both cases, a **copy** is made of the content and either a new Symbol or String is returned.

    ```ruby
    john at 0
    40 at 1
    ```
    ### What does a multi-word Symbol look like?

    On the other hand, a Hash is ordered by the **insertion order** of each the key-value pair:
    Less commonly, a Symbol is also multiple words, wrapped inside quotation marks and prefixed by a colon:

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }

    person.each do |key, value|
    puts "#{key}: #{value}"
    end
    :'john lennon'
    ```

    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:
    When calling `puts` on a multi-word Symbol:

    ```ruby
    person = ['john', 40]
    person.push('paul') #=> ['john', 40, 'paul']
    person << 72 #=> ['john', 40, 'paul', 72]
    puts :'john lennon'
    ```

    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:
    It displays the following:

    ```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' }
    john lennon
    ```

    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:
    But when calling `p` on this Symbol:

    ```ruby
    person = ['john'] + [40]
    person #=> ['john', 40]
    p :'john lennon'
    ```

    However, two Hashes cannot be concatenated together:
    It displays the following:

    ```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 }
    :"john lennon"
    ```

    **NOTE:** A Hash used as a method argument does not need the curly braces `{}`.
    **NOTE:** Ruby always uses double quotation marks when displaying multi-word Symbols.

    At first glace, merging looks just like concatenation. However, when both Hashes have the same key, the Hash inside the `merge` method takes precedence:
    ### In practice, how do you remember which one to use?

    ```ruby
    person = { 'name' => 'john', 'age' => 40 }.merge('age' => 35)
    person #=> { 'name' => 'john', 'age' => 35 }
    ```
    If identity is important, **use a Symbol**.

    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.
    If content is important, **use a String**.
  8. ryansobol revised this gist Oct 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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
    #### Index Order vs Value Order

    An Array is ordered by the **numerical value** of each index, which always starts at `0`:

  9. ryansobol revised this gist Oct 21, 2014. 1 changed file with 225 additions and 88 deletions.
    313 changes: 225 additions & 88 deletions symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,212 +1,349 @@
    ### What's a Symbol and why is it imporant?
    ### What's a Hash and why is it important?

    In Ruby, a Symbol is the most efficient way, in terms of time and memory, to represent a set of characters.
    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.

    ### What does a Symbol look like?
    While an Array is indexed by Integers only, a Hash is keyed by any object -- Strings, Integers, etc.

    Most commonly, a Symbol is a single word prefixed by a colon:
    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
    :hello
    {}
    ```

    **NOTE:** You'll see an example of a multi-word Symbol later.

    When calling `puts` on a Symbol:
    To create a Hash with a single key-value pair, place the pair inside the curly braces, separated by a hash-rocket `=>`:

    ```ruby
    puts :hello
    { 'name' => 'john' }
    ```

    It displays the following:
    In this example, `'name'` is the **key** and `'john'` is the corresponding **value**.

    ```
    hello
    **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 }
    ```

    But when calling `p` on this Symbol:
    In this example, `'name'` and `'age'` are the **keys**. And `'john'` and `40` are their corresponding **values**.

    When calling `puts` on a Hash:

    ```ruby
    p :hello
    puts({ 'name' => 'john', 'age' => 40 })
    ```

    **NOTE:** Ruby will fail with a `SyntaxError` without the parenthesis.

    It displays the following:

    ```ruby
    {"name"=>"john", "age"=>40}
    ```
    :hello

    **NOTE:** Ruby always uses double quotation marks when displaying Strings.

    Similarly, when calling `p` on this Hash:

    ```ruby
    p({ 'name' => 'john', 'age' => 40 })
    ```

    ### How does a Symbol compare to a String?
    **NOTE:** Again, Ruby will fail with a `SyntaxError` without the parenthesis.

    A String can be assigned to a variable:
    It also displays the following:

    ```ruby
    name = 'john'
    {"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 Symbol can be assigned to a variable too:
    Similarly, a Hash can be assigned to a variable too:

    ```ruby
    name = :john
    person = { 'name' => 'john', 'age' => 40 }
    ```

    Also, a String can receive a variety of methods:
    An Array can receive a variety of methods:

    ```ruby
    name = 'john'
    name = name.upcase #=> 'JOHN'
    person = ['john', 40]
    person.size #=> 2
    ```

    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.
    In this example, an Array with two **elements** is assigned to the `person` variable.

    Similarly, a Symbol can receive a variety of methods too:
    Similarly, a Hash can receive a variety of methods too:

    ```ruby
    name = :john
    name = name.upcase #=> :JOHN
    person = { 'name' => 'john', 'age' => 40 }
    person.size #=> 2
    ```

    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.
    In this example, a Hash with two **key-value pairs** is assigned to the `person` variable.

    ### How does a Symbol contrast to a String?
    #### Fetching from a container

    A String is **mutable**. That means its value can change while a program is running:
    A single **element** of an Array can be fetched by it's **index**:

    ```ruby
    name = 'john'
    name.upcase! #=> 'JOHN'
    person = ['john', 40]
    person[0] #=> '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.
    Likewise, a single **value** of a Hash can be fetched by it's **key**:

    Unlike a String, a Symbol is **immutable**. That means its value remains constant during the entirety of the program:
    ```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
    name = :john
    name.upcase! # NoMethodError: undefined method `upcase!' for :john:Symbol
    person = ['john', 40]
    person[0] = 'paul'
    ```

    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.
    Likewise, given a Hash **key**, it's **value** can be reassigned:

    ### When do you use a Symbol?
    ```ruby
    person = { 'name' => 'john', 'age' => 40 }
    person['name'] = 'paul'
    ```

    Strings with the same content refer to a different object:
    #### Deleting from a container

    Given an Array **index**, it's **element** can be deleted from the Array:

    ```ruby
    'john'.object_id #=> 927900
    'john'.object_id #=> 928360
    person = ['john', 40]
    person.delete_at(0) #=> 'john'
    person #=> [40]
    ```

    In contrast, Symbols with the same content refer to the same object:
    **NOTE:** The element `40` is now at index `0`.

    Likewise, given a Hash **key**, it's **key-value pair** can be deleted:

    ```ruby
    :john.object_id #=> 539688
    :john.object_id #=> 539688
    person = { 'name' => 'john', 'age' => 40 }
    person.delete('name') #=> 'john'
    person #=> { 'age' => 40 }
    ```

    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**.
    **NOTE:** The value `40` is still at key `'age'`.

    Every time a program creates an object, it takes time and memory. This means **Symbols are more efficient than Strings**.
    #### Iterating over a container

    For this reason, Symbols make great Hash keys:
    Each **element** and **index** combination of an Array can be iterated over:

    ```ruby
    person = { :name => 'john' }
    person = ['john', 40]

    person[:name] #=> 'john'
    person.each_with_index do |element, index|
    puts "#{element} at #{index}"
    end
    ```

    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.
    Which displays the following:

    ```ruby
    john at 0
    40 at 1
    ```

    In fact, this is so common, there's a shorthand way of writing the above example:
    Similarly, each **key-value pair** of a Hash can be iterated over:

    ```ruby
    person = { name: 'john' }
    person = { 'name' => 'john', 'age' => 40 }

    person.each do |key, value|
    puts "#{key}: #{value}"
    end
    ```

    person[:name] #=> 'john'
    Which displays the following:

    ```
    name: john
    age: 40
    ```

    In this example, the colon moved to the right of the word `name` and the hash rocket `=>` disappeared.
    ### How does a Hash contrast to an Array?

    **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 `{}`.
    Even with some minor quirks, Hashes and Arrays are rather similar. However, there are a few major differences.

    ### When do you use a String?
    #### Index Type vs Key Type

    If Symbols are more efficient than Strings, why bother with Strings at all?
    An Array is indexed by Integers only:

    One reason is that Strings have a richer set of methods.
    ```ruby
    person = ['john', 40]
    person[0] #=> 'john'
    person[1] #=> 40
    ```

    For example, two Strings can be added together, but two Symbols cannot.
    While a Hash is keyed by any object. For example, by Strings:

    ```ruby
    'jo' + 'hn' #=> 'john'
    :jo + :hn # NoMethodError: undefined method `+' for :jo:Symbol
    person = { 'name' => 'john', 'age' => 40 }
    person['name'] #=> 'john'
    person['age'] #=> 40
    ```

    A String can be centered, while a Symbol cannot.
    Or by Integers:

    ```ruby
    'john'.center(6) #=> ' john '
    :john.center(6) # NoMethodError: undefined method `center' for :john:Symbol
    person = { 1 => 'john', 2 => 40 }
    person[1] #=> 'john'
    person[2] #=> 40
    ```

    A String can be queried about its content, but a Symbol cannot.
    Or by a mixture of both Strings and Integers:

    ```ruby
    'john'.include?('jo') #=> true
    :john.include?(:jo) # NoMethodError: undefined method `center' for :john:Symbol
    person = { 'name' => 'john', 2 => 40 }
    person['name'] #=> 'john'
    person[2] #=> 40
    ```

    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.
    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.

    ### How do you convert a String to a Symbol and vice versa?
    ### Index Order vs Value Order

    Quite easily, actually:
    An Array is ordered by the **numerical value** of each index, which always starts at `0`:

    ```ruby
    'john'.to_sym #=> :john
    :john.to_s #=> 'john'
    person = ['john', 40]

    person.each_with_index do |element, index|
    puts "#{element} at #{index}"
    end
    ```

    In both cases, a **copy** is made of the content and either a new Symbol or String is returned.
    Which displays the following:

    ### What does a multi-word Symbol look like?
    ```ruby
    john at 0
    40 at 1
    ```

    Less commonly, a Symbol is also multiple words, wrapped inside quotation marks and prefixed by a colon:
    On the other hand, a Hash is ordered by the **insertion order** of each the key-value pair:

    ```ruby
    :'john lennon'
    person = { 'name' => 'john', 'age' => 40 }

    person.each do |key, value|
    puts "#{key}: #{value}"
    end
    ```

    When calling `puts` on a multi-word Symbol:
    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
    puts :'john lennon'
    person = ['john', 40]
    person.push('paul') #=> ['john', 40, 'paul']
    person << 72 #=> ['john', 40, 'paul', 72]
    ```

    It displays the following:
    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]
    ```
    john lennon

    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]
    ```

    But when calling `p` on this Symbol:
    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
    p :'john lennon'
    person = { 'name' => 'john', 'age' => 40 }
    person['friend'] = 'paul'
    person #=> { 'name' => 'john', 'age' => 40, 'friend' => 'paul' }
    ```

    It displays the following:
    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
    ```
    :"john lennon"

    Instead, two Hashes can be merged together:

    ```ruby
    person = { 'name' => 'john' }.merge('age' => 40)
    person #=> { 'name' => 'john', 'age' => 40 }
    ```

    **NOTE:** Ruby always uses double quotation marks when displaying multi-word Symbols.
    **NOTE:** A Hash used as a method argument does not need the curly braces `{}`.

    ### In practice, how do you remember which one to use?
    At first glace, merging looks just like concatenation. However, when both Hashes have the same key, the Hash inside the `merge` method takes precedence:

    If identity is important, **use a Symbol**.
    ```ruby
    person = { 'name' => 'john', 'age' => 40 }.merge('age' => 35)
    person #=> { 'name' => 'john', 'age' => 35 }
    ```

    If content is important, **use a String**.
    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.
  10. ryansobol revised this gist Oct 21, 2014. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    #### What's a Symbol and why is it imporant?
    ### 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?
    ### 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?
    ### 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?
    ### 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?
    ### 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?
    ### 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?
    ### 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?
    ### 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?
    ### In practice, how do you remember which one to use?

    If identity is important, **use a Symbol**.

  11. ryansobol revised this gist Oct 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #### What's a Symbol and why are they imporant?
    #### 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.

  12. ryansobol revised this gist Oct 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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:
    Less commonly, a Symbol is also multiple words, wrapped inside quotation marks and prefixed by a colon:

    ```ruby
    :'john lennon'
  13. ryansobol revised this gist Oct 21, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions symbols.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ It displays the following:
    hello
    ```

    But when calling `p` on a Symbol:
    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 a Symbol:
    But when calling `p` on this Symbol:

    ```ruby
    p :'john lennon'
  14. ryansobol revised this gist Oct 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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?

    A Symbol can also contain multiple words, wrapped inside quotations and prefixed by a colon:
    Less commonly, a Symbol is also multiple words, wrapped inside quotations and prefixed by a colon:

    ```ruby
    :'john lennon'
  15. ryansobol revised this gist Oct 21, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions symbols.md
    Original 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 represented by a single word prefixed by a colon:
    Most commonly, a Symbol is a single word prefixed by a colon:

    ```ruby
    :hello
    ```

    **NOTE:** You'll see how a Symbol can represent multiple words later.
    **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 represent multiple words, wrapped inside quotations and prefixed by a colon:
    A Symbol can also contain multiple words, wrapped inside quotations and prefixed by a colon:

    ```ruby
    :'john lennon'
  16. ryansobol revised this gist Oct 21, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions symbols.md
    Original 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**.
  17. ryansobol revised this gist Oct 21, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions symbols.md
    Original 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.

    #### How can a Symbol represent multiple words?
    #### What does a multi-word Symbol look like?

    A Symbol can also be represented by a multiple word wrapped inside quotations and prefixed by a colon:
    A Symbol can also represent multiple words, wrapped inside quotations and prefixed by a colon:

    ```ruby
    :'john lennon'
  18. ryansobol revised this gist Oct 21, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions symbols.md
    Original 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
    ```

    **NOTE:** You'll see how a Symbol can represent multiple words later.

    When calling `puts` on a Symbol:

    ```ruby
  19. ryansobol revised this gist Oct 21, 2014. 1 changed file with 62 additions and 30 deletions.
    92 changes: 62 additions & 30 deletions symbols.md
    Original 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 = 'josh'
    name = 'john'
    ```


    Similarly, a Symbol can be assigned to a variable too:

    ```ruby
    name = :josh
    name = :john
    ```

    Also, a String can receive a variety of methods:

    ```ruby
    name = 'josh'
    name = name.upcase #=> 'JOSH'
    name = 'john'
    name = name.upcase #=> 'JOHN'
    ```

    In this example, a **copy** of the `'josh'` String was created where each letter was capitalized to form `'JOSH'`. Afterwards, the `'JOSH'` String was reassigned to the `name` variable, replacing `'josh'`. In other words, two **unique** Strings we're created.
    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 = :josh
    name = name.upcase #=> :JOSH
    name = :john
    name = name.upcase #=> :JOHN
    ```

    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.
    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 = 'josh'
    name.upcase! #=> 'JOSH'
    name = 'john'
    name.upcase! #=> 'JOHN'
    ```

    In this example, the `'josh'` String itself was capitalized to `'JOSH'`. **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.
    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 = :josh
    name.upcase! # NoMethodError: undefined method `upcase!' for :josh:Symbol
    name = :john
    name.upcase! # NoMethodError: undefined method `upcase!' for :john: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.
    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
    'josh'.object_id #=> 927900
    'josh'.object_id #=> 928360
    'john'.object_id #=> 927900
    'john'.object_id #=> 928360
    ```

    In contrast, Symbols with the same content refer to the same object:

    ```ruby
    :josh.object_id #=> 539688
    :josh.object_id #=> 539688
    :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 => 'josh' }
    person = { :name => 'john' }

    person[:name] #=> 'josh'
    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: 'josh' }
    person = { name: 'john' }

    person[:name] #=> 'josh'
    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' + 'sh' #=> 'josh'
    :jo + :sh # NoMethodError: undefined method `+' for :jo:Symbol
    'jo' + 'hn' #=> 'john'
    :jo + :hn # NoMethodError: undefined method `+' for :jo:Symbol
    ```

    A String can be centered, while a Symbol cannot.

    ```ruby
    'josh'.center(6) #=> ' josh '
    :josh.center(6) # NoMethodError: undefined method `center' for :josh:Symbol
    '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
    'josh'.include?('jo') #=> true
    :josh.include?(:jo) # NoMethodError: undefined method `center' for :josh:Symbol
    '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
    'josh'.to_sym #=> :josh
    :josh.to_s #=> 'josh'
    '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**.
  20. ryansobol revised this gist Oct 21, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion symbols.md
    Original 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?

    A Symbol is represented by a word prefixed by a colon:
    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
  21. ryansobol revised this gist Oct 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #### What's a Symbol and why should I care?
    #### 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.

  22. ryansobol revised this gist Oct 21, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion symbols.md
    Original 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 a word prefixed by a colon:
    A Symbol is represented by a word prefixed by a colon:

    ```ruby
    :hello
  23. ryansobol revised this gist Oct 20, 2014. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions symbols.md
    Original 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:
  24. ryansobol revised this gist Oct 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #### What does a Symbol look like?

    A Symbol is a colon followed by a single word:
    A Symbol is a word prefixed by a colon:

    ```ruby
    :hello
  25. ryansobol revised this gist Oct 20, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions symbols.md
    Original 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?

    Every String with the same content refers to a different object:
    Strings with the same content refer 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:
    In contrast, Symbols with the same content refer to the same object:

    ```ruby
    :josh.object_id #=> 539688
  26. ryansobol revised this gist Oct 20, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions symbols.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ### What does a Symbol look like?
    #### 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?
    #### 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?
    #### 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?
    #### 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?
    #### 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?
    #### 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?
    #### In practice, how do you remember which one to use?

    If identity is important, **use a Symbol**.

  27. ryansobol revised this gist Oct 20, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions symbols.md
    Original 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**.
  28. ryansobol revised this gist Oct 20, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions symbols.md
    Original 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 remember this in practice?

    If identity is important, **use a Symbol**.

    If content is important, **use a String**.

    ### 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**.
  29. ryansobol revised this gist Oct 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion symbols.md
    Original 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 `=>` dissapears.
    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 `{}`.

  30. ryansobol revised this gist Oct 20, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions symbols.md
    Original 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?

    A Symbol is useful because it refers to the same object throughout the running program:
    Every String with the same content refers to a different object:

    ```ruby
    :josh.object_id #=> 539688
    :josh.object_id #=> 539688
    'josh'.object_id #=> 927900
    'josh'.object_id #=> 928360
    ```

    In contrast, each String refers to a different object:
    In contrast, every Symbol with the same content refers to the same object:

    ```ruby
    'josh'.object_id #=> 927900
    'josh'.object_id #=> 928360
    :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**.