-
-
Notifications
You must be signed in to change notification settings - Fork 531
Add hash concept and exercise #1809
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
base: main
Are you sure you want to change the base?
Conversation
88c383c to
c4876ae
Compare
c4876ae to
d89a432
Compare
|
I would say we are a bit in a limbo, though, the array and enumeration concepts are connected to one exercise, and personally, I think it makes sense to introduce arrays before hashes. But the enumeration concept mentions hashes. So either I think enumeration should be stripped out of hashes, which might not make sense. Instead it might make sense to make a separate exercise for arrays so we can have. |
|
An option could be to temporairly place hashes over arrays |
I do tend to teach "collections" rather than independent data structures, since other than the data structure itself, the idea of Enumerable being the thing that gives a lot of power. What do you think about taking that approach for a concept, rather than the concept of "map/dictionary" specifically? I have not looked at the changes in this PR yet, though. Just got back to my computer after a two week break (travelling). |
|
|
||
| Even though hashes are unordered collections, Ruby maintains the insertion order of key-value pairs. | ||
| This means that when you iterate over a hash, the pairs will be returned in the order they were added. | ||
| However, deleting elements may affect the order of remaining elements. |
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.
Expand on this, is the order at that point of operation undeterminate? Citation would be helpful for further exploration, and historically in Ruby, this is an interesting point as well.
| my_hash = { 1 => "one", :two => 2, "three" => [3, "three"] } | ||
| ``` | ||
|
|
||
| Alternatively if the keys are symbols, you can use a more JSON-style syntax: |
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.
Perhaps avoid JSON reference. But maybe mention the "newer syntax as of Ruby 1.9" form as shown.
| my_hash = { name: "Alice", age: 30, city: "New York" } | ||
| ``` | ||
|
|
||
| You can create an empty hash using the `Hash.new` method: |
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.
Capitalize "Hash" when speaking of the type specifically or the object itself, since hash is also a method and an idea. (Even if it weren't this situation, I would likely suggest capitalization as well as code syntax for the specific thing.)
|
|
||
| ## Accessing values | ||
|
|
||
| You can access values in a hash using their corresponding keys, the syntax reminds of array indexing, but using the key instead of an index: |
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 can access values in a hash using their corresponding keys, the syntax reminds of array indexing, but using the key instead of an index: | |
| You can access values in a `Hash` instance using its corresponding keys, the syntax reminds of array indexing, but using the key instead of an index: |
| You can access values in a hash using their corresponding keys, the syntax reminds of array indexing, but using the key instead of an index: | ||
|
|
||
| ```ruby | ||
| my_hash = { "name" => "Alice", "age" => 30, "city" => "New York" } |
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.
No space inside Hash delimiters, similar to Array and Range, save the space inside {} delimiters for blocks.
| my_hash = { "name" => "Alice", "age" => 30, "city" => "New York" } | |
| my_hash = {"name" => "Alice", "age" => 30, "city" => "New York"} |
Alternatively, present it as:
| my_hash = { "name" => "Alice", "age" => 30, "city" => "New York" } | |
| my_hash = { | |
| "name" => "Alice", | |
| "age" => 30, | |
| "city" => "New York" | |
| } |
Indentation adjusted for readability (this is how I style my key/value pairs reducing eye jitter), and not necessary, but might be nice to have.
| @@ -0,0 +1,38 @@ | |||
| class GrossStore | |||
| UNITS = {'quarter_of_a_dozen' => 3, 'half_of_a_dozen' => 6, 'dozen' => 12, 'small_gross' => 120, 'gross' => 144, 'great_gross' => 1728}.freeze | |||
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.
Nice that there is no space inside the delimiter, but why the freeze? We advertise this as a mutable object, but then make it immutable here?
Also blank line after class definition line is a signal of "topic change" and creates a paragraph of sorts.
This is an exemplar, not just an example, so we should apply opinions here to be that exemplar.
But I will not critique explicitly here. Welcome to discuss it on Discord, though, if you want to (even pair program through it if you want).
Or even as a product of a mentor request! ;)
| @@ -0,0 +1,24 @@ | |||
| class GrossStore | |||
| UNITS = { 'quarter_of_a_dozen' => 3, 'half_of_a_dozen' => 6, 'dozen' => 12, 'small_gross' => 120, 'gross' => 144, | |||
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.
| UNITS = { 'quarter_of_a_dozen' => 3, 'half_of_a_dozen' => 6, 'dozen' => 12, 'small_gross' => 120, 'gross' => 144, | |
| UNITS = {'quarter_of_a_dozen' => 3, 'half_of_a_dozen' => 6, 'dozen' => 12, 'small_gross' => 120, 'gross' => 144, |
As a minimum, no space inside delimiters.
Perhaps a columnar presentation of the Hash as well, easier to see patterns, etc., than having a horizontal showing of this.
| UNITS = { 'quarter_of_a_dozen' => 3, 'half_of_a_dozen' => 6, 'dozen' => 12, 'small_gross' => 120, 'gross' => 144, | ||
| 'great_gross' => 1728 }.freeze | ||
|
|
||
| attr_reader :bill |
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.
public method definition above the private method initialize is something that I personally detest, it suggests something that is untrue (that initialize is perhaps not private, since in this organization it is in the midst of public method definitions.
| ], | ||
| "prerequisites": [ | ||
| "advanced-enumeration" | ||
| "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 should be capitalized, as hashes are created by using hash method calls, and is something different.
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 is the slug
| ], | ||
| "prerequisites": [ | ||
| "ostruct" | ||
| "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.
... same here.
I think in a way that is a good idea, I think however that the concept might end up being too big. Since this concept is around 200 lines say array is the same and then what about other collections (such as sets). It might make sense to have these after each other but don't know about having a single big one. |
This is apart of making ostruct an optional concept: https://forum.exercism.org/t/openstruct-is-now-officially-discouraged/17490