- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我对 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 方式”做事获得一些反馈。
最佳答案
我的个人答案,简而言之:the core-class patching hammer should be at the bottom of your toolbox .您还可以使用许多其他技术,而且在几乎所有情况下,它们都足够、更干净,而且更多sustainable .
不过,这实际上取决于您编写代码的环境。如果这是个人项目 - 当然,随心所欲地打补丁!当您与一大群程序员长期在大型代码库上工作时,问题就开始出现了。在我工作的组织中,有超过 100KLOC 的 Ruby 代码库和 20 名左右的开发人员,我们已经开始严厉打击猴子补丁,因为我们已经看到它会导致令人头疼、浪费工时的行为太频繁了。在这一点上,我们几乎只能容忍它临时修补尚未合并或不会合并我们的源补丁的第三方代码。
关于ruby - 修补 Ruby 的基类(例如 Fixnum)是否可以接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/238878/
我目前正在尝试将包含数字 82,000 的散列 counts["email"] 除以包含值 130 万的变量 total。 当我运行 puts counts["email"]/total 时,我得到
好的,这是怎么回事? irb(main):001:0> 4/3 => 1 irb(main):002:0> 7/8 => 0 irb(main):003:0> 5/2 => 2 我知道 Ruby 在这
使用以下函数,我转到一个站点,抓取一些信息,返回一些 JSON,并将其放入 @price 实例变量中。 返回给我的 JSON 是一个数字,但是如果数字大于 1000,那么数字将包含一个逗号,所以我将其
这个问题在这里已经有了答案: TypeError (String can't be coerced into Fixnum)? (1 个回答) 关闭 8 年前。 我创建了一个程序,它接受输入学生姓名
我在这一行有这个错误(if change_needed - coins[i] >= 0 then ) String can't be coerced into Fixnum 从我的代码来看,我不明白为
我正在查看用 Ruby 1.8 为 RubyQuiz 编写的一些代码,当我在 1.9.2 中运行它时,它现在会抛出一个错误。这个方法 def encrypt(s) return process(s
我想在 Ruby 中获取当前时间的字符串: "Time is " + Time.new.month + "/" + Time.new.day + "/" + Time.new.year 但是它说“无法
当我尝试 fixnum.new 时出现未定义方法错误。 Fixnum.new # undefined method `new' for Fixnum:Class (NoMethodError) 为什
我写了下面的基本代码 puts ' Hi there , what is your favorite number ? ' number = gets.chomp puts number + ' is
我试图通过使用优化和 fixnums 从一个小的二次求解器中获得更高的速度。这是我的代码: 1: (defun solve-x (d) 2: (declare (optimize (speed
为什么数字返回输出的 ruby size 函数是这样的? 1000000.size # => 8 99999999999999999999.size # => 9 最佳答
我正在尝试编写一个从整数中提取最低阶数的函数。例如: > 24689.lowest_order => 9 到目前为止我有: class Integer def lowest_order I
例如,如果我在 irb 中键入以下内容,它会返回 Fixnum。 20.class => Fixnum 此外, 20.between?(10, 30) => true 但是,当我查看 Ruby 文档时
我可以像这样在一个实例上定义一个方法: object = Object.new def object.foo puts "5" end 用 Fixnum 尝试类似的东西是行不通的: def 3.f
我需要通过划分两个单独的计数来计算一些百分比值。我得到的计数值是 class 是 fixnum。但是当我要划分它们时,无论计数值是多少,它都会显示 0, 这里最后的结果显示为 0。 最佳答案 您
我认为 Ruby 会自动转换为 Bignum。我找到了确认 here 然而,这并没有发生: ruby 1.8.7 (358) [universal-darwin12.0] >> 2 ** 62
如何确定负 FixNum 的无符号解释? # unexpected, true (~0b01111011).to_s(2) == ("-" + (~0b01111011).abs.to_s(2)) #
该方法返回任意对象的特征类: class Object def eigenclass class #> 数组: [1, 2].eigenclass # => #> 但是对于 Fixnum
我已经写了一个基本的计算程序。该程序对某些输入运行良好,而对其他输入则给出 TypeError。我无法弄清楚这种不可预测的行为背后的原因。这是我的代码- class Conversion I = 1,
试图解决 Chris Pine 书中的一个问题。要求用户输入他们的名字和姓氏,然后它应该显示两者的字符总数。 这是我提出的众多解决方案之一: puts "First name?" first_name
我是一名优秀的程序员,十分优秀!