-
-
Notifications
You must be signed in to change notification settings - Fork 531
Last Will exercise + namespaces #1832
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?
Changes from all commits
e8e1c0c
fbd33a4
7c956c1
3ed741b
cead53f
f26fae3
8f8d992
9d1762d
2758b8c
25703e6
4955188
3d9a791
1912991
3f781b0
a905daa
0d46244
28e3a4d
f546027
875d4e6
6b7667f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "blurb": "Classes and modules provide namespaces. Namespaces can be nested, which might help to structure big code bases. Access to the namespaces is done via the scope-resolution operator.", | ||
| "authors": ["vaeng","vaiapatta1985"], | ||
| "contributors": [] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Introduction | ||
|
|
||
| # Namespaces | ||
|
|
||
| An important method for code organization is the use of namespaces. | ||
| Classes and modules provide namespaces. | ||
| Two methods might have a naming collision, which can be resolved by putting them in different namespaces. | ||
| Namespaces can be nested, which might help to structure big code bases. | ||
| Access to the namespaces is done via the scope-resolution operator `::`. | ||
|
|
||
| The example below shows the use of two different `foo` methods. | ||
| They are used together by prefixing their respective namespaces. | ||
|
|
||
| ```ruby | ||
| class MyNamespace | ||
| def self.foo | ||
| 44 | ||
| end | ||
|
|
||
| class MyInnerNamespace | ||
| def self.baz | ||
| 90 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| class MyOtherNamespace | ||
| def self.foo | ||
| -2 | ||
| end | ||
| end | ||
|
|
||
| p MyNamespace::foo + MyOtherNamespace::foo * MyNamespace::MyInnerNamespace::baz # => -136 | ||
| ``` | ||
|
|
||
| Note that namespaces are interpreted based on where the executing code is. | ||
| To refer to something in the *root* namespace, (ie outside all defined namespaces), start with `::`. | ||
|
|
||
| For example: | ||
|
|
||
| ```ruby | ||
| class Example | ||
| def self.example | ||
| 5 | ||
| end | ||
| end | ||
|
|
||
| class MyNamespace | ||
|
|
||
| class Example | ||
| def self.example | ||
| 10 | ||
| end | ||
| end | ||
|
|
||
| def self.call_example | ||
| p Example::example # => 10 | ||
| p ::Example::example # => 5 | ||
| end | ||
| end | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Introduction | ||
|
|
||
| # Namespaces | ||
|
|
||
| An important method for code organization is the use of namespaces. | ||
| Classes and modules provide namespaces. | ||
| Two methods might have a naming collision, which can be resolved by putting them in different namespaces. | ||
| Namespaces can be nested, which might help to structure big code bases. | ||
| Access to the namespaces is done via the scope-resolution operator `::`. | ||
|
|
||
| The example below shows the use of two different `foo` methods. | ||
| They are used together by prefixing their respective namespaces. | ||
|
|
||
| ```ruby | ||
| class MyNamespace | ||
| def self.foo | ||
| 44 | ||
| end | ||
|
|
||
| class MyInnerNamespace | ||
| def self.baz | ||
| 90 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| class MyOtherNamespace | ||
| def self.foo | ||
| -2 | ||
| end | ||
| end | ||
|
|
||
| p MyNamespace::foo + MyOtherNamespace::foo * MyNamespace::MyInnerNamespace::baz # => -136 | ||
| ``` | ||
|
|
||
| Note that namespaces are interpreted based on where the executing code is. | ||
| To refer to something in the *root* namespace, (ie outside all defined namespaces), start with `::`. | ||
|
|
||
| For example: | ||
|
|
||
| ```ruby | ||
| class Example | ||
| def self.example | ||
| 5 | ||
| end | ||
| end | ||
|
|
||
| class MyNamespace | ||
|
|
||
| class Example | ||
| def self.example | ||
| 10 | ||
| end | ||
| end | ||
|
|
||
| def self.call_example | ||
| p Example::example # => 10 | ||
| p ::Example::example # => 5 | ||
| end | ||
| end | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [ | ||
| { | ||
| "url": "https://docs.ruby-lang.org/en/4.0/syntax/modules_and_classes_rdoc.html", | ||
| "description": "Syntax for Ruby Modules and Classes" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Hints | ||
|
|
||
| ## General | ||
|
|
||
| - Do not change the code in the families' namespaces. | ||
| - Do not copy the values into your code, call the functions. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no functions in Ruby, so this can be confusing. Perhaps we should say "use the methods provided in..." instead, and avoid the word "function" or "functions" in this way to reference methods. |
||
|
|
||
| ## 1. Take your seat in front of the families and lay out your papers | ||
|
|
||
| - The namespace has to be called `EstateExecutor` for the tests to work. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests will work as intended even if this does not exist. We can state that the tests will report a failure or error if the namespace is not called The tests will work, as it is written to work with errors and mistakes and report them. |
||
|
|
||
| ## 2. Find the secret account number | ||
|
|
||
| Each `bank_number_part` has to be called with the `secret_modifier` from the parameter list. | ||
|
|
||
| ## 3. Last step: Enter the secret code | ||
|
|
||
| - You can call functions from nested namespaces like this `OuterNamespace::InnerNamespace::my_function`. | ||
|
|
||
| - Take care to add the blue and the red fragments separately before multiplicating both parts. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Instructions | ||
|
|
||
| You work for a prestigious law firm that is specialized in handling unique testament requests. | ||
|
|
||
| In this exercise, you are going to open a mysterious vault. | ||
| You are the executor of the estate and will assemble the long-kept secret codes of three families to get an account number and the matching code. | ||
|
|
||
| To prevent any family from opening the vault alone, it can only be opened by combining their knowledge with a secret modifier that you know from the last will. | ||
|
|
||
| You have three tasks, all related to helping the families to open the vault. | ||
|
|
||
| # 1. Take your seat in front of the families and lay out your papers | ||
|
|
||
| Define a namespace called `EstateExecutor`. | ||
| The code from the next tasks should be defined in the body of the `EstateExecutor` namespace. | ||
|
|
||
| ```ruby | ||
| class SomeName | ||
| # The space between the class/module name | ||
| # and the `end` keyword | ||
| # is called body of the namespace. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two spaces for indentation... |
||
| end | ||
| ``` | ||
|
|
||
| # 2. Find the secret account number | ||
|
|
||
| This is your big moment. | ||
| Only you have the secret modifier key to reveal the secret account number. | ||
|
|
||
| Define the `assemble_account_number(secret_modifier)` method that takes the `Integer` secret modifier as an argument and returns the `Integer` assembled account number. | ||
|
|
||
| To get the correct number, you have to sum up the `bank_number_part` from each of the three families. | ||
|
|
||
| # 3. Last step: Enter the secret code | ||
|
|
||
| The instructions in the testament ask you to add all the blue and then all the red fragments. | ||
| The resulting code is obtained by multiplying both sums. | ||
|
|
||
| Define the `assemble_code` method that returns the resulting code by combining the fragments from the three families to a single `Integer` result. | ||
| The method does not have any arguments and relies solely on the information in the relevant namespaces from the families. | ||
|
|
||
| # Source | ||
|
|
||
| ## Created by | ||
|
|
||
| - @vaeng (original C++ exercise) | ||
| - @vaiapatta1985 (transcription to Ruby) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Introduction | ||
|
|
||
| # Namespaces | ||
|
|
||
| An important method for code organization is the use of namespaces. | ||
| Classes and modules provide namespaces. | ||
| Two methods might have a naming collision, which can be resolved by putting them in different namespaces. | ||
| Namespaces can be nested, which might help to structure big code bases. | ||
| Access to the namespaces is done via the scope-resolution operator `::`. | ||
|
|
||
| The example below shows the use of two different `foo` methods. | ||
| They are used together by prefixing their respective namespaces. | ||
|
|
||
| ```ruby | ||
| class MyNamespace | ||
| def self.foo | ||
| 44 | ||
| end | ||
|
|
||
| class MyInnerNamespace | ||
| def self.baz | ||
| 90 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| class MyOtherNamespace | ||
| def self.foo | ||
| -2 | ||
| end | ||
| end | ||
|
VaiaPatta1985 marked this conversation as resolved.
|
||
|
|
||
| p MyNamespace::foo + MyOtherNamespace::foo * MyNamespace::MyInnerNamespace::baz # => -136 | ||
| ``` | ||
|
|
||
| Note that namespaces are interpreted based on where the executing code is. | ||
| To refer to something in the *root* namespace, (ie outside all defined namespaces), start with `::`. | ||
|
|
||
| For example: | ||
|
|
||
| ```ruby | ||
| class Example | ||
| def self.example | ||
| 5 | ||
| end | ||
| end | ||
|
|
||
| class MyNamespace | ||
|
VaiaPatta1985 marked this conversation as resolved.
|
||
|
|
||
| class Example | ||
| def self.example | ||
| 10 | ||
| end | ||
| end | ||
|
|
||
| def self.call_example | ||
| p Example::example # => 10 | ||
| p ::Example::example # => 5 | ||
| end | ||
| end | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "authors": [ | ||
| "vaeng", | ||
| "vaiapatta1985" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "last_will.rb" | ||
| ], | ||
| "test": [ | ||
| "last_will_test.rb" | ||
| ], | ||
| "exemplar": [ | ||
| ".meta/exemplar.rb" | ||
| ] | ||
| }, | ||
| "blurb": "Classes and modules provide namespaces. Namespaces can be nested, which might help to structure big code bases. Access to the namespaces is done via the scope-resolution operator." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Design | ||
|
|
||
| ## Goal | ||
|
|
||
| The goal of this exercise is to teach the student how to use namespaces in Ruby. | ||
|
|
||
| ## Learning objectives | ||
|
|
||
| - Know how to call a method defined in a namespace. | ||
| - Know how to organize methods in namespaces. | ||
|
|
||
| ## Out of scope | ||
|
|
||
| - Memory and performance characteristics. | ||
| - Method overloads. | ||
| - Lambdas. | ||
| - Optional parameters. | ||
| - Visibility. | ||
|
|
||
| ## Concepts | ||
|
|
||
| The Concepts this exercise unlocks are: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is a single Concept being taught, then we should use the singular "concept" rather than the plural. |
||
|
|
||
| - `namespaces`: know how to call a method defined in a namespace; know how to organize methods in namespaces. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - `basics` | ||
| - `modules` | ||
|
|
||
| ## Representer | ||
|
|
||
| This exercise does not require any specific representation logic to be added to the [representer][representer]. | ||
|
|
||
| ## Analyzer | ||
|
|
||
| This exercise does not require any specific logic to be added to the [analyzer][analyzer]. | ||
|
|
||
| [analyzer]: https://github.com/exercism/ruby-analyzer | ||
| [representer]: https://github.com/exercism/ruby-representer | ||
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 code provided in the family namespaces should be considered as a "given" and not modified.