Permalink
Browse files

Finalized notes for ruby-enumerable-builtins and added notes for

ruby-object-inheritance.
  • Loading branch information...
deconstructionalism
deconstructionalism committed Jul 6, 2018
1 parent 3630ccd commit 8752b072afc6f719c938b70c6b5cea24b70ad412
@@ -27,7 +27,7 @@ By the end of this, developers should be able to:

**Array**: A list of objects who's values are defined at the time of instantiation using either `Array.new` or the `[<value_1>, <value_2>, etc]` syntax.

**Range**: A list of objects from some start value to some end value, of the form `(<start value>..<end value>)`, `(<start value>...<end value>)`, or `Range.new(<start value>, <end value>)`. A `Range` object does not actually calculate the values in the list until you run the `.to_a` method on it, convering the `Range` to an `Array`, which can help save memory such that values are only calcualted right when the `Range` is about to be used, as opposed to being calculated when they are declared as in an `Array`. *NOTE: ranges are `start value` inclusive and `end value` exclusive if you use `...`, but is `start value` inclusive and `end value` inclusive if you use `..`*
**Range**: A list of objects from some start value to some end value, of the form `(<start value>..<end value>)`, `(<start value>...<end value>)`, or `Range.new(<start value>, <end value>)`. A `Range` object does not actually calculate the values in the list until you run the `.to_a` method on it, convering the `Range` to an `Array`, which can help save memory such that values are only calcualted right when the `Range` is about to be used, as opposed to being calculated when they are declared as in an `Array`. *NOTE: ranges are `start value` inclusive and `end value` exclusive if you use `...`, but is `start value` inclusive and `end value` inclusive if you use `..`*

**Hash**: A list of key and value pairs where the keys are all unique. Also called associative arrays. You can use any type of value as the key but `symbol` types are reccomended as they allow for much faster lookup of associated values than using other types such as `String` type keys. `Hash` objects are declared using either `Hash.new` or `{ :<symbol name 1> => <value 1>, :<symbol name 2> => <value 2>, etc }` notation.

@@ -54,19 +54,97 @@ By the end of this, developers should be able to:
@thing2 = thing2
end
# you can acces the `RANDOM_VAL` value
# note that the value in the name space that you
# want to access has to be in all caps
# you can access the `RANDOM_VAL` value
puts MyClass::RANDOM_VAL
# RETURNS:
# 300
# => nil
```
- note than only constants (non-method variables) can be accessed in the namespace and constants must be declared in all caps, ie `RANDOM_VAL`

#### Lab Code
#### `Enumerable::each_with_object`

- will be inserted after lesson
- [`each_with_object`](https://ruby-doc.org/core-2.1.0/Enumerator.html#method-i-each_with_object) allows you to iterate through an enumerable variable and for each value, accumulate som change in an object of your choosing. Let's break down what is happening in each step of this method:

- ```js
// code goes here
```
- ```ruby
range = 5..115
altogether = range.each_with_object(Hash.new(0)) do |el, hash|
hash[:even] += el if el.even?
hash[:odd] += el if el.odd?
hash[:div3] += el if (el % 3).zero?
hash[:all] += el
end
```
1. `each_with_object` takes a single argument, the object you want to accumulate values within (called `memo` short for **memory** in **Ruby**). In this case, it's a `Hash` which will provide a default value of `0` for new keys

2. We also pass a block to `each_with_object` using the `do` keyword, and the block recieves to arguments from `each_with_object`:
- the current element in the utterable - we called it `el`
- the object / `memo` we are accumulating on - we called it `hash`

3. Inside the block, we have 4 conditions for adding values to keys in `hash`. It is important to note that each key does not exist at first in our empty `hash`, and when they are first added, they have a value of `0`.

- if `el` is even, add it's value to `hash[:even]`
- if `el` is odd, add it's value to `hash[:odd]`
- if `el` is divisible by 3, add it's value to `hash[:div3]`
- add every single value to `hash[:all]`
4. Finally, the `hash` is returned from the method with keys `even`, `odd`, `div3`, and `all` where values for `range` have accumulated sums according to the rules above.


#### `Enumerable::reduce
- `reduce` allows you to also accumulate values in an object while iterating through an enumerable type. The syntax is very similar to `each_with_object` with some key differences. Let's break down what is happening in each step of this method:
- ```ruby
alphabet = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
j: 10,
k: 11,
l: 12,
m: 13,
n: 14,
o: 15,
p: 16,
q: 17,
r: 18,
s: 19,
t: 20,
u: 21,
v: 22,
w: 23,
x: 24,
y: 25,
z: 26
}
reduced_hash = alphabet.reduce({keys: [], values: []}) do |memory, (key, value)|
memory[:keys] << key
memory[:values] << value
memory
end
```

1. `reduce` takes an initial value that will be accumulated in, in this case a hash with two keys that have empty arrays as values: `keys`, `values`
2. the block for `reduce` gets passed two positional arguments: the `memory` object which is being accumulated on, and the current value of the enumerable object that is being iterated. In a `Hash`, iteration returns `key` `value` pairs, which can be directly broken into two variables in the arguments of the block using `(keys, value)`
3. we then push the values of the current `key` and `value` to the approriate `memory` hash keys
4. **REMEMBER that you must return the accumulator hash `memory` at the end of the block or else it's changed values won't actually be saved for the next iteration**

- let's compare the `reduce` implementation to using `each_with_object` for the same task:

- ```ruby
reduced_hash_two = alphabet.each_with_object({keys: [], values: []}) do |(key, value), memory|
memory[:keys] << key
memory[:values] << value
end
```

- notice how the input arguments are in reverse order in `each_with_object`
- also, you don't need to return `memory` at the end of the block for it to be saved!
Binary file not shown.
@@ -0,0 +1,36 @@
## LESSON: [ruby-object-inheritance](https://git.generalassemb.ly/ga-wdi-boston/ruby-object-inheritance)

---

**2018-07-06**

### OBJECTIVES

By the end of this, developers should be able to:

- Diagram the Ruby method lookup chain
- Write a class which inherits from another class.

### VOCABULARY

**Inheritance**: They way objects inherit methods and properties from their parent classes.

**Superclass**: Classes themselves can inherit from other classes, and these parents classes of classes are called superclasses.

**Method Lookup Chain**: When a method is called on an object, it will look within it's own instance for methods with that name, and if not found, will go to the parent class and try to find the method, and if not found, go to that class' parent and try to find the method etc until either the method is found, and then run, or it is not found, returning `nil` / `undefined` .

**Overriding:** When a class inherits from a superclass and the superclass has a method that is also named in the child class, the child class is the method that gets called on objects, ie it overrides access to the superclass method and uses it's own method of the same name instead.

### NOTES

- note about lesson

### EXAMPLES

#### Code Topic

- description of code block:
- ```js
// code goes here
```

Binary file not shown.

0 comments on commit 8752b07

Please sign in to comment.