-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Add getters and setters concept and exercise #687
Conversation
@@ -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.", |
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.
"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.", |
concepts/getters-setters/about.md
Outdated
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. |
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 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. |
concepts/getters-setters/about.md
Outdated
|
||
## Setters | ||
|
||
Setters is a macro that defines a method that sets the value of an instance variable. |
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.
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 } |
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.
p my_method { |x, y| x * y } | |
p my_method { |x, y| x * y } | |
1 + yield(2, 3) | ||
end |
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.
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. |
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.
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`. |
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 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. |
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.
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 |
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.
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 |
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.
it "should not allowed to be get" do | |
it "should not allow getting" do |
This will be the last concept for a while to be added