Skip to content
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

Merged
merged 6 commits into from
Feb 2, 2018
Merged

Conversation

droberts-sea
Copy link

@droberts-sea droberts-sea commented Jan 22, 2018

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:

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

Todos

  • Build the pre-reading into the calendar

Feedback Requested

Find my dopey typos!

Dan Roberts added 3 commits January 16, 2018 13:00
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.
@droberts-sea droberts-sea self-assigned this Jan 22, 2018
@droberts-sea
Copy link
Author

- 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
Copy link
Collaborator

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
Copy link
Collaborator

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.

Copy link
Author

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
Copy link
Collaborator

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.

Copy link
Contributor

@tildeee tildeee left a 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
Copy link
Contributor

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

<!--
Copy link
Collaborator

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`
Copy link
Collaborator

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}"
Copy link
Collaborator

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)

Copy link
Author

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`
Copy link
Collaborator

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`
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Collaborator

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,
Copy link
Collaborator

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

Copy link
Collaborator

@CheezItMan CheezItMan Jan 25, 2018

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

Copy link
Author

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.

Copy link
Author

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

Copy link
Author

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',],
Copy link
Collaborator

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.
Copy link
Collaborator

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

@droberts-sea droberts-sea merged commit b639b91 into master Feb 2, 2018
@droberts-sea droberts-sea deleted the dpr/c9-hashes-arrays branch February 2, 2018 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants