-
-
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
Open
VaiaPatta1985
wants to merge
17
commits into
exercism:main
Choose a base branch
from
VaiaPatta1985:last_will
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e8e1c0c
Moviegoer and SimpleCalculator accept exceptions from both scopes
VaiaPatta1985 fbd33a4
Moviegoer and SimpleCalculator accept exceptions from both scopes
VaiaPatta1985 7c956c1
Merge remote-tracking branch 'ruby_fork/moviegoer_calculator_fix' int…
VaiaPatta1985 3ed741b
whitespace fixes
VaiaPatta1985 cead53f
Files for last will exercise.
VaiaPatta1985 f26fae3
More files for the last will exercise.
VaiaPatta1985 8f8d992
Corrections.
VaiaPatta1985 9d1762d
Corrections.
VaiaPatta1985 2758b8c
Corrections.
VaiaPatta1985 25703e6
Corrections.
VaiaPatta1985 4955188
Corrections.
VaiaPatta1985 3d9a791
Corrections.
VaiaPatta1985 1912991
Corrections.
VaiaPatta1985 3f781b0
Corrections.
VaiaPatta1985 a905daa
Corrections.
VaiaPatta1985 0d46244
Corrections.
VaiaPatta1985 28e3a4d
Corrections.
VaiaPatta1985 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "blurb": "Classes and modules can be 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": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 can be 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 can be 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| ## 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. | ||
|
|
||
| ## 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 can be 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 | ||
|
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 | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 can be namespaces. Namespaces can be nested, which might help to structure big code bases. Access to the namespaces is done via the scope-resolution operator." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
|
||
| - `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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.