【发布时间】:2010-09-19 07:51:26
【问题描述】:
我对 Ruby 还是很陌生(阅读 Pickaxe 并且大部分时间都在 irb),现在我知道可以在 Ruby 中修补类,我想知道什么时候可以这样做,特别是修补 Ruby 的基类是否可以接受。例如:我回答了另一个 Ruby 问题 here,发帖人想知道如何从 DateTime 中减去小时数。由于DateTime 类似乎不提供此功能,因此我发布了一个答案,将DateTime 和Fixnum 类作为可能的解决方案进行修补。这是我提交的代码:
require 'date'
# A placeholder class for holding a set number of hours.
# Used so we can know when to change the behavior
# of DateTime#-() by recognizing when hours are explicitly passed in.
class Hours
attr_reader :value
def initialize(value)
@value = value
end
end
# Patch the #-() method to handle subtracting hours
# in addition to what it normally does
class DateTime
alias old_subtract -
def -(x)
case x
when Hours; return DateTime.new(year, month, day, hour-x.value, min, sec)
else; return self.old_subtract(x)
end
end
end
# Add an #hours attribute to Fixnum that returns an Hours object.
# This is for syntactic sugar, allowing you to write "someDate - 4.hours" for example
class Fixnum
def hours
Hours.new(self)
end
end
我修补了这些类,因为我认为在这种情况下,它会产生一种清晰、简洁的语法,用于从 DateTime 中减去固定的小时数。具体来说,您可以根据上述代码执行以下操作:
five_hours_ago = DateTime.now - 5.hours
这看起来很好看,也很容易理解;但是,我不确定弄乱DateTime 的- 运算符的功能是否是个好主意。
对于这种情况,我能想到的唯一选择是:
1.只需即时创建一个新的 DateTime 对象,在对 new 的调用中计算新的小时值
new_date = DateTime.new(old_date.year, old_date.year, old_date.month, old_date.year.day, old_date.hour - hours_to_subtract, date.min, date.sec)
2.编写一个实用方法,接受 DateTime 和从中减去的小时数
基本上,只是方法 (1) 的包装:
def subtract_hours(date, hours)
return DateTime.new(date.year, date.month, date.day, date.hour - hours, date.min, date.sec)
end
3.向DateTime 添加新方法,而不是更改#-() 的现有行为
也许一个新的DateTime#less 方法可以与Fixnum#hours 补丁一起使用,以允许这样的语法:
date.less(5.hours)
但是,正如我已经提到的,我采用了修补方法,因为我认为它会产生更具表现力的语法。
我的方法有什么问题吗,或者我应该使用 3 种替代方法中的一种(或我没有想到的另一种)来做到这一点?我感觉打补丁正在成为我解决 Ruby 问题的新“锤子”,所以我想就我是否以“Ruby 方式”做事得到一些反馈。
【问题讨论】:
标签: ruby monkeypatching