重载 ActiveSupport 的默认 to_sentence 行为

发布于 2024-08-03 04:18:44 字数 406 浏览 5 评论 0原文

ActiveSupport 提供了很好的方法to_sentence。因此,

require 'active_support'
[1,2,3].to_sentence  # gives "1, 2, and 3"
[1,2,3].to_sentence(:last_word_connector => ' and ')  # gives "1, 2 and 3"

最好您可以更改最后一个单词连接器,因为我不喜欢有多余的逗号。但它需要太多额外的文本:44 个字符而不是 11 个!

问题:将 :last_word_connector 的默认值更改为 ' 和 ' 的最像 ruby​​ 的方法是什么?

ActiveSupport offers the nice method to_sentence. Thus,

require 'active_support'
[1,2,3].to_sentence  # gives "1, 2, and 3"
[1,2,3].to_sentence(:last_word_connector => ' and ')  # gives "1, 2 and 3"

it's good that you can change the last word connector, because I prefer not to have the extra comma. but it takes so much extra text: 44 characters instead of 11!

the question: what's the most ruby-like way to change the default value of :last_word_connector to ' and '?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮华 2024-08-10 04:18:44

嗯,它是可本地化的,因此您只需指定默认 'en' 值 ' 和 ' < code>support.array.last_word_connector

请参阅:

来自:conversion.rb

def to_sentence(options = {})
...
   default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
...
end

分步指南:

首先,创建一个 Rails 项目

rails i18n

接下来,编辑 en.yml 文件: vim config/locales/en.yml

en:
  support:
    array:
      last_word_connector: " and "

最后,它可以工作:

 
Loading development environment (Rails 2.3.3)
>> [1,2,3].to_sentence
=> "1, 2 and 3"

Well, it's localizable so you could just specify a default 'en' value of ' and ' for support.array.last_word_connector

See:

from: conversion.rb

def to_sentence(options = {})
...
   default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
...
end

Step by step guide:

First, Create a rails project

rails i18n

Next, edit your en.yml file: vim config/locales/en.yml

en:
  support:
    array:
      last_word_connector: " and "

Finally, it works:

 
Loading development environment (Rails 2.3.3)
>> [1,2,3].to_sentence
=> "1, 2 and 3"
凉月流沐 2024-08-10 04:18:44

作为一般如何重写方法的答案,此处< /a> 提供了一个很好的方法。它不会遇到与别名技术相同的问题,因为没有剩余的“旧”方法。

在这里,您可以如何使用该技术来解决您的原始问题(使用 ruby​​ 1.9 进行测试)

class Array
  old_to_sentence = instance_method(:to_sentence)
  define_method(:to_sentence) { |options = {}|

    options[:last_word_connector] ||= " and "
    old_to_sentence.bind(self).call(options)
  }
end

您可能还需要阅读 UnboundMethod 如果上面的代码令人困惑。请注意,old_to_sentence 在 end 语句之后超出了范围,因此对于将来使用 Array 来说这不是问题。

As an answer to how to override a method in general, a post here gives a nice way of doing it. It doesn't suffer from the same problems as the alias technique, as there isn't a leftover "old" method.

Here how you could use that technique with your original problem (tested with ruby 1.9)

class Array
  old_to_sentence = instance_method(:to_sentence)
  define_method(:to_sentence) { |options = {}|

    options[:last_word_connector] ||= " and "
    old_to_sentence.bind(self).call(options)
  }
end

You might also want read up on UnboundMethod if the above code is confusing. Note that old_to_sentence goes out of scope after the end statement, so it isn't a problem for future uses of Array.

闻呓 2024-08-10 04:18:44
 class Array
   alias_method :old_to_sentence, :to_sentence
   def to_sentence(args={})
     a = {:last_word_connector => ' and '}
     a.update(args) if args
     old_to_sentence(a)
   end
 end
 class Array
   alias_method :old_to_sentence, :to_sentence
   def to_sentence(args={})
     a = {:last_word_connector => ' and '}
     a.update(args) if args
     old_to_sentence(a)
   end
 end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文