I've been experimenting and find that I like redefining Object's to_s
methods.
我一直在试验,发现我喜欢重新定义对象的to_s方法。
Is this a bad idea or is it good practice?
这是坏主意还是好做法?
4 个解决方案
#1
56
No, you should feel free to override to_s
- there are no ill side-effects. As long as your new to_s
is more informative than the built-in (not exactly a high standard there), you're in the clear.
不,你可以随意重写to_s——没有副作用。只要您的新to_s比内置的to_s提供更多的信息(并不一定是很高的标准),您就很清楚了。
And they help make your test failures read better - sometimes by a lot - which is never a bad thing. Go for it!
他们帮助你让你的测试失败读得更好——有时候是很多——这不是一件坏事。就去做吧!
#2
10
I override to_s
all the time in my Rails project:
在我的Rails项目中,我总是重写to_s:
def to_s first_name + " " + last_name end
so that it's easier to show objects in the view:
这样更容易在视图中显示对象:
<%= @person %>
#3
7
It might be tricky to do that because sometimes, inspect
method just calls to_s
, and if that is altered, you might have trouble debugging. If you think altering to_s
may confuse you when you need to see the results by methods that rely on inspect
, such as p
, then maybe you need to redefine inspect
for that class at the same time. As long as you are sure what you are doing, you might want to do it.
这样做可能有些困难,因为有时候,检查方法只调用to_s,如果修改了,可能会遇到调试困难。如果您认为更改to_s可能会在需要通过依赖于inspect(如p)的方法查看结果时造成混淆,那么您可能需要同时重新定义该类的inspect。只要你确定自己在做什么,你就可能想去做。
#4
0
It's not "bad" per se, but it isn't "good" either. It really depends on the context.
它本身并不是“坏”的,但也不是“好”的。这取决于环境。
If you are doing this for a one-shot place (for example inside your rails app's /lib/
folder, for a particular app) it is probably alright (make sure to give the file a descriptive name, such as object_to_s_patch.rb
or similar, and that all patches are on the same place)
如果你这样做是为了一个一次性的地方(例如,在你的rails应用程序的/lib/文件夹内,对于一个特定的应用程序),它可能是对的(一定要给文件一个描述性的名称,比如object_to_s_patch。rb或类似的,所有的补丁都在同一个地方)
If you are doing a gem or a lib, on the other hand, I would not override it. Instead I'd add another method - Object.to_special_s
or something. But I'd also try not to touch Object if possible - If you can get by with using YourModule::to_s(object)
that'd be probably even better.
另一方面,如果您正在执行gem或lib,我不会重写它。相反,我要添加另一个方法——对象。to_special_s什么的。但是,如果可能的话,我也会试着不去触摸对象——如果你能通过使用你的模块来获得的话::to_s(对象),那可能会更好。
The reasoning behind this is that other people might be using Object.to_s
for other stuff, maybe in other libs. Monkeypatching it will produce *es with those other libs.
这背后的原因是其他人可能在使用对象。为了其他的东西,也许在其他的地方。修补它将会与其他地方发生冲突。
The only exception when doing a gem that I can think of is when the main point (or one of the main points) of that library is actually overriding the method; in other words, you are literally making a lib that overrides Object.to_s
, and little else. I'd put some big warning on the documentation on that case. This way people using it will not get surprised.
在执行gem时,我能想到的唯一例外是当库的主要点(或主要点之一)实际上覆盖方法时;换句话说,您实际上是在创建一个重写对象的库。to_s,。我要对那个案子的文件提出警告。这样人们就不会感到惊讶了。
#1
56
No, you should feel free to override to_s
- there are no ill side-effects. As long as your new to_s
is more informative than the built-in (not exactly a high standard there), you're in the clear.
不,你可以随意重写to_s——没有副作用。只要您的新to_s比内置的to_s提供更多的信息(并不一定是很高的标准),您就很清楚了。
And they help make your test failures read better - sometimes by a lot - which is never a bad thing. Go for it!
他们帮助你让你的测试失败读得更好——有时候是很多——这不是一件坏事。就去做吧!
#2
10
I override to_s
all the time in my Rails project:
在我的Rails项目中,我总是重写to_s:
def to_s first_name + " " + last_name end
so that it's easier to show objects in the view:
这样更容易在视图中显示对象:
<%= @person %>
#3
7
It might be tricky to do that because sometimes, inspect
method just calls to_s
, and if that is altered, you might have trouble debugging. If you think altering to_s
may confuse you when you need to see the results by methods that rely on inspect
, such as p
, then maybe you need to redefine inspect
for that class at the same time. As long as you are sure what you are doing, you might want to do it.
这样做可能有些困难,因为有时候,检查方法只调用to_s,如果修改了,可能会遇到调试困难。如果您认为更改to_s可能会在需要通过依赖于inspect(如p)的方法查看结果时造成混淆,那么您可能需要同时重新定义该类的inspect。只要你确定自己在做什么,你就可能想去做。
#4
0
It's not "bad" per se, but it isn't "good" either. It really depends on the context.
它本身并不是“坏”的,但也不是“好”的。这取决于环境。
If you are doing this for a one-shot place (for example inside your rails app's /lib/
folder, for a particular app) it is probably alright (make sure to give the file a descriptive name, such as object_to_s_patch.rb
or similar, and that all patches are on the same place)
如果你这样做是为了一个一次性的地方(例如,在你的rails应用程序的/lib/文件夹内,对于一个特定的应用程序),它可能是对的(一定要给文件一个描述性的名称,比如object_to_s_patch。rb或类似的,所有的补丁都在同一个地方)
If you are doing a gem or a lib, on the other hand, I would not override it. Instead I'd add another method - Object.to_special_s
or something. But I'd also try not to touch Object if possible - If you can get by with using YourModule::to_s(object)
that'd be probably even better.
另一方面,如果您正在执行gem或lib,我不会重写它。相反,我要添加另一个方法——对象。to_special_s什么的。但是,如果可能的话,我也会试着不去触摸对象——如果你能通过使用你的模块来获得的话::to_s(对象),那可能会更好。
The reasoning behind this is that other people might be using Object.to_s
for other stuff, maybe in other libs. Monkeypatching it will produce *es with those other libs.
这背后的原因是其他人可能在使用对象。为了其他的东西,也许在其他的地方。修补它将会与其他地方发生冲突。
The only exception when doing a gem that I can think of is when the main point (or one of the main points) of that library is actually overriding the method; in other words, you are literally making a lib that overrides Object.to_s
, and little else. I'd put some big warning on the documentation on that case. This way people using it will not get surprised.
在执行gem时,我能想到的唯一例外是当库的主要点(或主要点之一)实际上覆盖方法时;换句话说,您实际上是在创建一个重写对象的库。to_s,。我要对那个案子的文件提出警告。这样人们就不会感到惊讶了。