Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* restructured readme * added readme to assignments * restructered * cleaned up * added zeros to single digit numbers * cleaned up * restructured 01-learning style * Create readme.md * cleaned up * added numbers to headings * fixed links * cleaned up * cleaned up * added folder for jump start live * Update and rename README.md to readme.md * Update readme.md * Update readme.md * Update readme.md * Create pretty-code.md * Create conditionals.md * Create loops.md * Update loops.md * Create more-loops.md * Create arrays.md * Create hashes.md * Create readme.md * Update readme.md * Update readme.md * Update readme.md * removed jump-start-live; has its own place * restructured and cleaned up * restructured and cleaned up * fixed titles * fixed assignment links * fixed article linking structure * fixed link structure * clean up * fixed link structure * removed term bold styles * major updates to reduce to a single page * formatting fixes * formatting fixes * major changes to reduce to a single page * small formatting change * major updates to reduce to a single page * some small content fixes * fixed optional readings section * major updates; unfinished * removed unnecessary files * reordered * removed unneccessary files * reordered content * fixed names * cleaned up; renamed * renamed * fixed table headings * switched to activies, from assignments * added note about not having a mac * formatting fix * formatting fix * fixed names * content fixes * fixed imgs * content fixes * reorg * Fixed links * Fixed links * adjusted grouping * fixed intros * massive re-structring and content fixes * fixed links * uggggh * content fixes * major updates to content and structure * formatting fix * formatting fix * formatting fix * formatting fix * formatting fix * removed img of truth table * content fixes * content fixes * major restructuring of content * updated learning goals * moved from lessons to section folders * updates * added blank doc for applyign to ada * added link for applying to ada * fixed overviews and links * fixed heading * fixed img link * fixed order of content * fixed img link * fixed headings * fixed img links * fixed links
- Loading branch information
Showing
68 changed files
with
506 additions
and
862 deletions.
There are no files selected for viewing
This file contains 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 characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Hashes | ||
_Jump start: Lesson 13_ | ||
|
||
## Learning Goals | ||
- Vocabulary: data structure, array, index, value, hash, key-value pair, default value | ||
- To understand what a data structure is and how it can be used | ||
- To be able to create a new array and hash data structure | ||
- To be able to access data in an array and hash | ||
- To be able to add data to an array and hash | ||
- To understand the difference between an array and a hash | ||
- To know the syntax needed to create a new array and hash | ||
|
||
## Activities | ||
* Review the notes in this section | ||
* Complete [Hash Worksheet](assignments/hash-worksheet.md) | ||
* Complete [Account Generator Continued](assignments/account-generator-cont.md) | ||
* Complete [Walk-a-thon](assignments/walkathon.md) | ||
|
||
## Notes | ||
### Overview | ||
A `Hash` is another very popular collection type in Ruby. It is similar to an Array, except that indexing is done via unordered _keys_ of any object type, and _not an ordered integer index_. | ||
|
||
```ruby | ||
{ "key1" => "key1sAssociatedValue", "key2" => "key2sAssociatedValue" ...} | ||
``` | ||
|
||
### Creating Hashes | ||
We initialize an empty `Hash` by using `{}` (pronounced _brace_). A hash can be assigned to a variable in the same or a similar way that other types of data are assigned to a variable: | ||
|
||
### Empty | ||
|
||
```ruby | ||
my_hash = {} | ||
``` | ||
|
||
```ruby | ||
my_hash = Hash.new | ||
``` | ||
|
||
We know that this hash is empty because the hash definition starts with the `{`(left brace) and ends with the `}`(right brace), and there is nothing between those two symbols. | ||
|
||
### With Data | ||
|
||
By using the `new` syntax and specifying a default value, _all_ keys retrieved with no corresponding value will return the default value. Without the default value, accessing an undefined key will return `nil`. | ||
|
||
```ruby | ||
my_hash = Hash.new | ||
my_hash["gibberish"] # => nil | ||
|
||
default_data = Hash.new("def") | ||
|
||
# By creating a hash with a default value, | ||
# all data retrievals will return the default value if not specified otherwise | ||
default_data["gibberish"] # => "def" | ||
``` | ||
|
||
You can also create a new hash with key/value pairs populated. | ||
|
||
```ruby | ||
my_dog = { | ||
"name" => "barkly", | ||
"breed" => "beagle", | ||
"age" => 2 | ||
} | ||
``` | ||
|
||
### Accessing Data | ||
To access data from within a hash, we use a syntax that is similar to accessing data in an Array. The difference is that we use the strings that correspond to the key to retrieve the associated value. | ||
|
||
```ruby | ||
my_dog["age"] # => 2 | ||
my_dog["breed"] # => "spaniel" | ||
my_dog["name"] # => "barkly" | ||
|
||
# by default, will return nil for keys that don't exist | ||
my_dog["color"] # => nil | ||
``` | ||
|
||
### Assigning Data | ||
We can utilize our understanding of reassigning values in Arrays to assign and reassign values in a Hash. | ||
|
||
Assuming that we want to add a **new** key value pair to an **existing** hash, we can choose the key that we want to set the value for. In the case below, we have decided that we want to set the value `"blenheim"` for a new key `"color"`. | ||
|
||
```ruby | ||
# Add a new key value pair for color | ||
my_dog["color"] = "blenheim" | ||
``` | ||
|
||
In addition, we can reset the value associated with an existing key using the same syntax. In the case below, we would like to set the `"age"` value to `3` from it's original `2`. | ||
```ruby | ||
# Retrieve the value currently set | ||
my_dog["age"] # => 2 | ||
|
||
# Reassign the value associated with the age key | ||
my_dog["age"] = 3 | ||
|
||
# Retrieve the updated value | ||
my_dog["age"] # => 3 | ||
``` | ||
|
||
### Using Built-In Methods | ||
|
||
#### length | ||
This method returns the number of items in the hash. An item corresponds to a key/value pair. | ||
|
||
```ruby | ||
my_dog = { | ||
"name" => "barkly", | ||
"breed" => "beagle", | ||
"age" => 2 | ||
} | ||
|
||
my_dog.length # => 3 | ||
``` | ||
|
||
#### keys | ||
This method returns an _array_ of all the defined keys for the hash. | ||
|
||
```ruby | ||
my_dog.keys #=> ["name", "breed", "age"] | ||
``` | ||
|
||
#### values | ||
This method returns an _array_ of all the values of all defined keys for the hash. | ||
|
||
```ruby | ||
my_dog.values #=> ["barkly", "beagle", 3] | ||
``` | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains 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 characters
Oops, something went wrong.