-
Notifications
You must be signed in to change notification settings - Fork 38
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
Unleash::Context#to_s displays all properties #175
Conversation
- Refactored the `to_s` method in the `Context` class to dynamically include all instance variables. - This change ensures that any new instance variables added in the future will automatically be included in the string representation without needing to modify the `to_s` method. - This makes the method more flexible and maintainable.
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.
I would have preferred a simpler approach, without meta-programming.
Also, as in all other classes in the entire project, we do not use meta programming when writing the to_s
method, and I would like to stick to that standard, instead of having different implementations in different classes.
I would however be positive to having meta programming in a test to verify that all instance variables are present in the to_s
method, if you feel like doing that.
Or if using meta programming, leverage the built-in to_h
built in method (as in the suggestion below).
lib/unleash/context.rb
Outdated
formatted_attributes = instance_variables.map do |var| | ||
"#{var.to_s.delete('@')}=#{instance_variable_get(var)}" | ||
end | ||
|
||
"<Context: #{formatted_attributes.join(', ')}>" |
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.
If one were to use meta-programming, i'd have preferred if the built in to_h
method was used instead:
formatted_attributes = instance_variables.map do |var| | |
"#{var.to_s.delete('@')}=#{instance_variable_get(var)}" | |
end | |
"<Context: #{formatted_attributes.join(', ')}>" | |
"<Context: " + self.to_h.map{|k,v| "#{k}=#{v}"}.join(',') + ">" |
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.
I had no idea to use the to_h
method. It is a very nice and simple implementation!
Pull Request Test Coverage Report for Build 9600614573Details
💛 - Coveralls |
@rarruda Thank you for your review! I learned that I should refer to implementations in other classes. I also felt that a simpler implementation without meta-programming would be better. |
Like the other classes, it is simple to implement and easy to understand. Co-authored-by: Renato Arruda <[email protected]>
I have confirmed once again that the RSpec and RuboCop checks pass. |
Pull Request Test Coverage Report for Build 9624141809Details
💛 - Coveralls |
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.
Thanks for your contribution and for making the unleash gem better!
About the changes
to_s
method in theContext
class to dynamically include all instance variables.to_s
method.RSpec and RuboCop were executed, and no issues were found related to this change.
Closes #172
Important files
lib/unleash/context.rb
Discussion points