在Ruby中重写BigDecimal至_s默认值

时间:2022-09-25 18:26:28

As I retrieve data from a database table an array is populated. Some of the fields are defined as decimal & money fields and within the array they are represented as BigDecimal.

当我从数据库表中检索数据时,会填充一个数组。有些字段被定义为decimal & money字段,在数组中它们被表示为BigDecimal。

I use these array values to populate a CSV file, but the problem is that all BigDecimal values are by default represented in Scientific format (which is the default behaviour of the BigDecimal to_s method). I can show the values by using to_s('F'), but how can I override the default?

我使用这些数组值来填充CSV文件,但问题是所有BigDecimal值默认都用科学格式表示(这是BigDecimal to_s方法的默认行为)。我可以使用to_s('F')来显示值,但是如何覆盖默认值呢?

3 个解决方案

#1


2  

This is built on @Farrel's answer, but without polluting the method namespace with a useless old_xyz method. Also, why not use default arguments directly?

这是建立在@Farrel的答案上的,但是没有使用无用的old_xyz方法污染方法名称空间。另外,为什么不直接使用默认参数呢?

class BigDecimal
  old_to_s = instance_method :to_s

  define_method :to_s do |param='F'|
    old_to_s.bind(self).(param)
  end
end

In Ruby 1.8, this gets slightly uglier:

在Ruby 1.8中,情况变得更糟:

class BigDecimal
  old_to_s = instance_method :to_s

  define_method :to_s do |param|
    old_to_s.bind(self).call(param || 'F')
  end
end

Or, if you don't like the warning you get with the above code:

或者,如果你不喜欢上面的警告:

class BigDecimal
  old_to_s = instance_method :to_s

  define_method :to_s do |*param|
    old_to_s.bind(self).call(param.first || 'F')
  end
end

#2


1  

class BigDecimal
  alias old_to_s to_s

  def to_s( param = nil )
      self.old_to_s( param || 'F' )
   end
end

#3


0  

Ruby makes this easy. Behold:

Ruby使这项任务变得很容易。见:

class BigDecimal
  def to_s
    return "Whatever weird format you want"
  end
end

# Now BigDecimal#to_s will do something new, for all BigDecimal objects everywhere.

This technique is called monkey patching. As you might guess from the name, it's something you should use cautiously. This use seems reasonable to me, though.

这种技术叫做猴补。顾名思义,您应该谨慎使用它。这种用法在我看来似乎是合理的。

#1


2  

This is built on @Farrel's answer, but without polluting the method namespace with a useless old_xyz method. Also, why not use default arguments directly?

这是建立在@Farrel的答案上的,但是没有使用无用的old_xyz方法污染方法名称空间。另外,为什么不直接使用默认参数呢?

class BigDecimal
  old_to_s = instance_method :to_s

  define_method :to_s do |param='F'|
    old_to_s.bind(self).(param)
  end
end

In Ruby 1.8, this gets slightly uglier:

在Ruby 1.8中,情况变得更糟:

class BigDecimal
  old_to_s = instance_method :to_s

  define_method :to_s do |param|
    old_to_s.bind(self).call(param || 'F')
  end
end

Or, if you don't like the warning you get with the above code:

或者,如果你不喜欢上面的警告:

class BigDecimal
  old_to_s = instance_method :to_s

  define_method :to_s do |*param|
    old_to_s.bind(self).call(param.first || 'F')
  end
end

#2


1  

class BigDecimal
  alias old_to_s to_s

  def to_s( param = nil )
      self.old_to_s( param || 'F' )
   end
end

#3


0  

Ruby makes this easy. Behold:

Ruby使这项任务变得很容易。见:

class BigDecimal
  def to_s
    return "Whatever weird format you want"
  end
end

# Now BigDecimal#to_s will do something new, for all BigDecimal objects everywhere.

This technique is called monkey patching. As you might guess from the name, it's something you should use cautiously. This use seems reasonable to me, though.

这种技术叫做猴补。顾名思义,您应该谨慎使用它。这种用法在我看来似乎是合理的。