-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the Hashes and Arrays Resource #362
Conversation
There are now 3 main sections: Arrays, Hashes, and In Combination. Each section has the same basic layout: - Creation - Accessing elements - Iteration which I think gives it a really nice flow and emphasizes the pattern. In between arrays / hashes and in combination is a comparison section, where I moved the table I made in the previous commit. I've also reworked the examples away from Pokemon, since we got so many freaking complaints last time. We may want to assign the first 3 sections (arrays, hashes, comparison) as homework the night before, depending on time.
- The list can be empty | ||
- Since an array is an object, we can store it in a variable | ||
- You can get the length of the array with `Array#length` | ||
- The array will keep track of the order elements were added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better, it maintains the original order of the elements given.
- The list can be empty | ||
- Since a hash is an object, we can store it in a variable | ||
- You can get the number of key/value pairs with `Hash#length` | ||
- The hash does **not** keep track of the order of its key/value pairs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think technically ruby does now maintain the order of the key/value pairs.
http://ruby-doc.org/core-2.5.0/Hash.html
I think it makes more sense to talk about why you shouldn't worry about order in a Hash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it's not mandated by the spec but it does happen in practice. I've struggled with this because ordering is not a property of hashes in general, only in this particular implementation of ruby, and I don't want to set them up with bogus expectations.
There's probably a better way to address this, and I think you're right that it involves talking about the "why" of it.
#### Iteration | ||
|
||
- Like an array, you can iterate through the key/value pairs in a hash | ||
- Unlike an array, the order is not guaranteed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again I think it is in the order they were inserted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
||
This loop does the same thing with slightly different syntax. | ||
- Iteration is the process of doing something to each element in the array, like printing it to the command line or adding 7 to it | ||
- To iterate manually, you can use a `times` loop on the length of the array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "to iterate manually"?
You've dealt with variables holding a single value and you've been introduced to the concept of collections as well, but here we'll go into greater depth. | ||
# Intro to Data Structures: Arrays and Hashes | ||
|
||
<!-- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a great note for the TET
- Arrays are created by putting square brackets `[]` around a list of things | ||
- The list can be empty | ||
- Since an array is an object, we can store it in a variable | ||
- You can get the length of the array with `Array#length` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this method declaration syntax is accurate, it is often confusing for the students, especially early on. Do we need to use this or could we just show them the dot methods?
|
||
# Accessing the first element | ||
puts my_list[0] | ||
puts "#{students}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use string interpolation instead of puts-ing the array directly? (as a question i would want to know why)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For arrays and hashes, puts with interpolation will put it on one line with brackets, making it much more visually obvious (IMO) what's going on. Puts with no interpolation will split it across many lines with no brackets.
myarr = [1, 2, 3]
puts myarr
# Output:
# 1
# 2
# 3
puts "#{myarr}"
# Output:
# [1, 2, 3]
- Each element in the array is assigned a number called its _index_ | ||
- Indices always start from 0 | ||
- This means the last element is at `arr.length - 1` | ||
- Negative indices count backwards from the end, starting at `-1` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add that this would be the last element in the array just to clarify
- You can add a new element to the array with the `Array#push` method or with `<<` | ||
- You can read elements using square brackets `[]` and the index | ||
- Changing the value of an element is done in the same way | ||
- Trying to read an element that doesn't exist will result in `nil` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"read an element at an index that is not in the array"
|
||
This loop does the same thing with slightly different syntax. | ||
- Iteration is the process of doing something to each element in the array, like printing it to the command line or adding 7 to it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might leave off the 'like printing it' section
|
||
This loop does the same thing with slightly different syntax. | ||
- Iteration is the process of doing something to each element in the array, like printing it to the command line or adding 7 to it | ||
- To iterate manually, you can use a `times` loop on the length of the array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe describe what this does differently (using the index) rather than using "manually"
The Array has the following index/value pairs. | ||
fruit_prices = { | ||
apple: 2.15, | ||
pear: 3.02, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we prefer the trailing comma? I feel like do in JS but not in Ruby
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The community style guide suggests avoiding trailing commas.
https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. This is why we need a style guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rubocop says no by default, but you can make it OK for multiline literals
rubocop/rubocop#713
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crap chris had already answered this when I hit send. I guess our conclusions are the same - I'll remove the trailing commas.
{ | ||
name: 'quiche', | ||
price: 10.00, | ||
allergens: ['gluten', 'dairy',], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question re trailing commas
|
||
|
||
|
||
|
||
Look at the Ruby docs for [Array](http://www.ruby-doc.org/core-2.4.0/Array.html) and [Hash](http://www.ruby-doc.org/core-2.4.0/Hash.html) for more details about collections. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update these links to not reference the specific version but reference the latest by just using /core/
instead
Description
Restructure the Arrays and Hashes lecture
There are now 3 main sections: Arrays, Hashes, and In Combination. Each section has the same basic layout:
which I think gives it a really nice flow and emphasizes the pattern.
In between arrays / hashes and in combination is a comparison section, where I moved the table I made in the previous commit.
I've also reworked the examples away from Pokemon, since we got so many freaking complaints last time.
We may want to assign the first 3 sections (arrays, hashes, comparison) as homework the night before, depending on time.
Todos
Feedback Requested
Find my dopey typos!