修改gem中的核心类 [英] Modifying core classes in a gem

查看:103
本文介绍了修改gem中的核心类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种宝石,我认为它对于更多的人而不仅仅是我而言是有用的。我面临的一个问题是我需要合并嵌套散列。我发现这个有用的Gist 来实现这一点,但现在我想知道是否可以修改哈希#这样的宝石?



我确定有一个社区标准或最佳实践,要么接受或拒绝这种类型的代码,所以我转身为了指导。



谢谢。

解决方案

怀疑,只是子类 Hash ,并在你的子类中包含Module。你应该特别这样做,当你重写方法或显着改变行为。



但是我不明白为什么你不应该修改哈希类。例如,Rails严格地扩展了Core类,并且我从未听到有人抱怨过。您可以看看Rails的 activesupport 如何扩展核心类:

https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext



只要确保不要破坏现有的行为,那么您的宝石用户就不会遇到不必要的副作用。


I'm developing a Gem which I think will be useful for more people than just me. One issue I'm facing is that I need to merge nested hashes. I found this useful Gist to accomplish that, but now I'm wondering if it's okay to modify Hash# like this in a Gem?

I'm sure there's a community "standard" or best practices that either accepts or declines this sort of code, so I'm turning to SO for guidance.

Thank you.

解决方案

When in doubt, just subclass Hash and include the Module in your subclass. You should particularly do this, when you override methods or change the behavior dramatically.

But i don't see why you shouldn't just modify the Hash class. Rails, for example, extends Core classes rigorously and i've never heard someone complain. You might take a look at how Rails' activesupport extends core classes:

https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext

Just make sure not to break existing behavior, so the users of your gem will not experience unwanted side-effects.

这篇关于修改gem中的核心类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆