-
Notifications
You must be signed in to change notification settings - Fork 6
Blog/refinements in ruby #236
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
base: main
Are you sure you want to change the base?
Conversation
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.
Left some formatting suggestions, content looked good
Something that we should address in the article:
If there are other classes defined within the module where we are doing the refinement, will the refined methods be available in other classes without using
?
E.g:
module StringExtensions
class Foo
def bar
'Hello World'.reverse_words
end
end
end
We should add this as one of the points
Done, Added example for this |
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.
+++ | ||
title = "Refinements in Ruby: A Flexible Approach to Modifying Core Classes" | ||
slug = "refinements-in-ruby" | ||
date = 2024-09-13T11:48:18+05:30 |
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.
Let's update the date to today, it will show up chronologically on the list
date = 2024-09-13T11:48:18+05:30 | |
date = 2024-12-12T11:48:18+05:30 |
type = "" | ||
+++ | ||
|
||
Ruby is a highly flexible programming language known for its dynamic capabilities and metaprogramming power. One such powerful feature in Ruby is refinements. Introduced in Ruby 2.0, refinements allow you to modify existing classes (even core classes like String, Array, etc.) in a more controlled and localized way. This is particularly useful when you want to change class behavior in specific contexts without affecting the global environment, which is a common issue with traditional monkey-patching. |
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.
Ruby is a highly flexible programming language known for its dynamic capabilities and metaprogramming power. One such powerful feature in Ruby is refinements. Introduced in Ruby 2.0, refinements allow you to modify existing classes (even core classes like String, Array, etc.) in a more controlled and localized way. This is particularly useful when you want to change class behavior in specific contexts without affecting the global environment, which is a common issue with traditional monkey-patching. | |
Ruby is a highly flexible programming language known for its dynamic capabilities and metaprogramming power. One such powerful feature in Ruby is **refinements**. Introduced in Ruby 2.0, refinements allow you to modify existing classes (even core classes like `String`, `Array`, etc.) in a more controlled and localised way. This is particularly useful when you want to change class behaviour in specific contexts without affecting the global environment, which is a common issue with traditional monkey-patching. |
Shouldn't we use Indian English instead of US English?
module StringExtensions | ||
refine String do | ||
def reverse_words | ||
self.split(' ').reverse.join(' ') | ||
end | ||
end | ||
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.
Nit:
module StringExtensions | |
refine String do | |
def reverse_words | |
self.split(' ').reverse.join(' ') | |
end | |
end | |
end | |
module StringExtensions | |
refine String do | |
def reverse_words | |
self.split(' ').reverse.join(' ') | |
end | |
end | |
end |
Personally, I use 2 spaces as it reads nicely without having too much whitespace. But to each their own I guess.
1. **Limited Scope**: Unlike monkey-patching, which alters a class’s behavior globally, refinements allow changes to be confined to specific contexts. This reduces the risk of breaking code in other parts of your application. | ||
|
||
1. **Better Code Isolation**: Refinements ensure that method modifications do not leak out and affect other classes or libraries unintentionally. With refinements, you can safely modify the behavior of third-party libraries without worrying about conflicts. | ||
|
||
1. **Safer Library Usage**: Libraries can define refinements to tweak core class behaviors for internal use without impacting their users’ code. This leads to more reliable and maintainable libraries. |
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.
Why not have them in a single list?
|
||
1. **Limited Usage in Some Libraries**: Some libraries and frameworks (such as Rails) may not fully support refinements or may have conflicts with them due to their reliance on method lookups. | ||
|
||
1. **Limited Method Visibility**: Refinements only affect methods directly called on objects. They do not apply to methods called indirectly via send, method, or define_method. |
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. **Limited Method Visibility**: Refinements only affect methods directly called on objects. They do not apply to methods called indirectly via send, method, or define_method. | |
1. **Limited Method Visibility**: Refinements only affect methods directly called on objects. They do not apply to methods called indirectly via send, method, or `define_method`. |
lets squash commits before merging |
Co-authored-by: Nipun Paradkar <nipun@incubyte.co>
Co-authored-by: Nipun Paradkar <nipun@incubyte.co>
Co-authored-by: Nipun Paradkar <nipun@incubyte.co>
Co-authored-by: Nipun Paradkar <nipun@incubyte.co>
What did you do?
Please include a summary of the changes.
Why did you do it?
Why were these changes made?
Screenshots (Please include if anything visual)
Include any relevant screenshots that may help explain the change.