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

Add getters and setters concept and exercise #687

Merged
merged 9 commits into from
Aug 17, 2024

Conversation

meatball133
Copy link
Member

This will be the last concept for a while to be added

@@ -0,0 +1,5 @@
{
"blurb": "Crystal has macros for defining getters and setters. Which is used to access and modify the instance variables of a class.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"blurb": "Crystal has macros for defining getters and setters. Which is used to access and modify the instance variables of a class.",
"blurb": "Crystal has macros for defining getters and setters. These are used to access and modify the instance variables of a class.",

Getter is a macro that defines a method that returns the value of an instance variable.
This means that you no longer have to write `@` in front of the instance variable when you want to access it (in methods, excluding initialize); instead, you can call the method that the getter macro has defined.

The getter macro can accept multiple instance variables by separating them with a comma.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The getter macro can accept multiple instance variables by separating them with a comma.
The getter macro can accept multiple instance variables by separating them with commas.


## Setters

Setters is a macro that defines a method that sets the value of an instance variable.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Setters is a macro that defines a method that sets the value of an instance variable.
Setter is a macro that defines a method that sets the value of an instance variable.

1 + yield(2, 3)
end

p my_method { |x, y| x * y }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p my_method { |x, y| x * y }
p my_method { |x, y| x * y }

Comment on lines 2 to 3
1 + yield(2, 3)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1 + yield(2, 3)
end
1 + yield(2, 3)
end

@@ -0,0 +1,63 @@
# Instructions

In this exercise you'll be modelling a weighing machine.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this exercise you'll be modelling a weighing machine.
In this exercise you'll be modeling a weighing machine.


Implement the `WeighingMachine#initialize` method which takes two arguments, `precision` which is an `Int32` and `metric` which is a `Bool`.
The `metric` argument when `true` means that the machine should use the metric system, otherwise it should use the imperial system.
The initizalized set should also set an instance variable, `@weight` to `0.0`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The initizalized set should also set an instance variable, `@weight` to `0.0`.
The initialize method set should also set the instance variable `@weight` to `0.0`.


## 4. Allow the machine to be switch between metric and imperial units

Implement the `WeighingMachine#metric=` property to allow the unit to be set, it should accept a boolean value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Implement the `WeighingMachine#metric=` property to allow the unit to be set, it should accept a boolean value.
Implement the `WeighingMachine#metric=` property to allow the unit to be set.
It should accept a boolean value.

weighing_machine.precision.should eq(5)
end

it "should not allowed to be modify" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "should not allowed to be modify" do
it "should not allow modification" do

weighing_machine.weigh.should eq("11.023")
end

it "should not allowed to be get" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "should not allowed to be get" do
it "should not allow getting" do

@ryanplusplus ryanplusplus merged commit 69e4a32 into exercism:main Aug 17, 2024
12 checks passed
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.

2 participants