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

Don't monkey patch String class #91

Merged
merged 2 commits into from
Apr 22, 2023
Merged

Don't monkey patch String class #91

merged 2 commits into from
Apr 22, 2023

Conversation

n-rodriguez
Copy link
Contributor

@n-rodriguez n-rodriguez commented Apr 21, 2023

As explained here awesome-print/awesome_print#419, monkey patch on String class was pretty unexpected.

The key is in def colorize(str, type) method : instead of monkey patching String + calling 'foo'.red, let's define colors methods on AmazingPrint::Colors module and call AmazingPrint::Colors.red('foo') instead.

def colorize(str, type)
str = CGI.escapeHTML(str) if options[:html]
if options[:plain] || !options[:color][type] || !inspector.colorize?
str
#
# Check if the string color method is defined by amazing_print and accepts
# html parameter or it has been overriden by some gem such as colorize.
#
elsif str.method(options[:color][type]).arity == -1 # Accepts html parameter.
str.send(options[:color][type], options[:html])
else
str = %(<kbd style="color:#{options[:color][type]}">#{str}</kbd>) if options[:html]
str.send(options[:color][type])
end

This way we don't pollute String class with new methods and we avoid clashes with colorize gem which I don't use due to license issues (gpl2 is contaminating) and we do not have to add a new dependency like Rainbow gem.

Thank you!

@ddarbyson
Copy link

nice to see some movement on this

paddor
paddor previously approved these changes Apr 21, 2023
Copy link
Contributor

@paddor paddor left a comment

Choose a reason for hiding this comment

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

Yeah this is long overdue. Thank you. At first glance, this looks good to me.

Copy link
Member

@HarlemSquirrel HarlemSquirrel left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! This is very much needed.

Could you please check out the lints and make Robocop happy? 😁

@n-rodriguez
Copy link
Contributor Author

Could you please check out the lints and make Robocop happy? 😁

Done :)

Copy link
Member

@HarlemSquirrel HarlemSquirrel left a comment

Choose a reason for hiding this comment

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

@HarlemSquirrel
Copy link
Member

Those other checks appear to be failing on master so we can fix those separately.

@HarlemSquirrel HarlemSquirrel merged commit 26c8bf3 into amazing-print:master Apr 22, 2023
@n-rodriguez
Copy link
Contributor Author

Thank you!

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.

4 participants