Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions concepts/namespaces/.meta/config.json
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": []
}
61 changes: 61 additions & 0 deletions concepts/namespaces/about.md
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
```
61 changes: 61 additions & 0 deletions concepts/namespaces/introduction.md
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
```
1 change: 1 addition & 0 deletions concepts/namespaces/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
17 changes: 17 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@
"ostruct"
]
},
{
"slug": "last-will",
"name": "Last Will",
"uuid": "",
"concepts": [
"namespaces"
],
"prerequisites": [
"basics",
"modules"
]
},
{
"slug": "moviegoer",
"name": "Moviegoer",
Expand Down Expand Up @@ -1624,6 +1636,11 @@
"slug": "multiple-assignment-and-decomposition",
"name": "Multiple Assignment and Decomposition"
},
{
"uuid": "",
"slug": "namespaces",
"name": "Namespaces"
},
{
"uuid": "fa482f4a-19d6-4d13-ad9e-5121c536dc76",
"slug": "ostruct",
Expand Down
20 changes: 20 additions & 0 deletions exercises/concept/last-will/.docs/hints.md
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.
47 changes: 47 additions & 0 deletions exercises/concept/last-will/.docs/instructions.md
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)
61 changes: 61 additions & 0 deletions exercises/concept/last-will/.docs/introduction.md
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
Comment thread
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
Comment thread
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
```
18 changes: 18 additions & 0 deletions exercises/concept/last-will/.meta/config.json
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."
}
40 changes: 40 additions & 0 deletions exercises/concept/last-will/.meta/design.md
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
Loading
Loading