Skip to content

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

Blog/refinements in ruby #236

wants to merge 19 commits into from

Conversation

hulksyed07
Copy link
Contributor

What did you do?

Please include a summary of the changes.

  • Blog on using refinements in ruby
  • Updated that

Why did you do it?

Why were these changes made?

  • This was missing
  • that needed changes

Screenshots (Please include if anything visual)

Include any relevant screenshots that may help explain the change.

Copy link
Collaborator

@2KAbhishek 2KAbhishek left a 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

@hulksyed07
Copy link
Contributor Author

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

@hulksyed07 hulksyed07 requested a review from 2KAbhishek December 8, 2024 16:07
Copy link
Collaborator

@2KAbhishek 2KAbhishek Dec 12, 2024

Choose a reason for hiding this comment

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

We can expand the width of the image, since its plain white

image

this is what it looks like now

+++
title = "Refinements in Ruby: A Flexible Approach to Modifying Core Classes"
slug = "refinements-in-ruby"
date = 2024-09-13T11:48:18+05:30
Copy link
Collaborator

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

Suggested change
date = 2024-09-13T11:48:18+05:30
date = 2024-12-12T11:48:18+05:30

@hulksyed07 hulksyed07 self-assigned this Dec 12, 2024
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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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?

Comment on lines +37 to +43
module StringExtensions
refine String do
def reverse_words
self.split(' ').reverse.join(' ')
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit:

Suggested change
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.

Comment on lines +143 to +147
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.
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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`.

@2KAbhishek
Copy link
Collaborator

lets squash commits before merging

hulksyed07 and others added 5 commits January 13, 2025 12:13
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>
Co-authored-by: Abhishek Keshri <iam2kabhishek@gmail.com>
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.

3 participants