【问题标题】:How to add delimiter to each_char function? [closed]如何为 each_char 函数添加分隔符? [关闭]
【发布时间】:2014-02-24 15:53:50
【问题描述】:

我有这个 Ruby 函数:

def project_total_counter
  total = Project.all.count
  spans = []
  total.to_s.each_char do |c|
    spans << content_tag(:span, c)
  end
  spans.join().html_safe
end

它会产生类似的东西:

<span>1</span>
<span>7</span>
<span>8</span>
<span>5</span>
<span>5</span>
<span>2</span>

我怎样才能为此添加分隔符,所以我得到了这个:

<span>1</span>
<span>7</span>
<span>8</span>
,
<span>5</span>
<span>5</span>
<span>2</span>

感谢您的帮助。

【问题讨论】:

  • 你也在使用 Rails 框架,对吧?
  • @maerics:是的,确实
  • 逗号是否恰好在 span 3 之后,因为它是千位分隔符?
  • 是的,它也应该出现在 span 6、9、12 等之后。
  • @Tintin81 您能否在问题中添加该信息?

标签: ruby-on-rails ruby view helper


【解决方案1】:

或者(假设 Rails):

spans = ""
number_with_delimiter(Project.all.count, :delimiter => ',').each_char do |c|
    if c == ','
        spans << c
    else
        spans << content_tag(:span, c)
    end
end
spans.html_safe

或者,更简洁:

number_with_delimiter(Project.all.count, :delimiter => ',').each_char.inject("") do |s, c|
    s << if c == ',' then c else content_tag(:span, c) end
end.html_safe

【讨论】:

  • 如果使用 Rails,这是最优雅的答案。
  • 这对我来说是完美的解决方案,因为我确实在这里使用 Rails。谢谢!
  • @CDub 问题被标记为ruby-on-rails,并且有一个表达式Project.all.count,所以我认为这是一个安全的选择。 :)
  • Touche... or threeche, @mbratch... :)
【解决方案2】:

我会使用Enumerable#each_slice 如下所示:

def project_total_counter
  total = Project.all.count
  spans  = total.to_s.chars.each_slice(3).flat_map do |ary|
    ary.map { |c| content_tag(:span, c) }.push(",")
  end
  spans[0..-2].join().html_safe
end

【讨论】:

  • 这将为“12345”生成“123,45”
  • @CDub 现在我修好了,谢谢.. :-)
  • 我猜块的span参数应该是spans
  • @toro2k 谢谢修复..
【解决方案3】:
total.to_s.each_char.map.with_index do |c, i|
  content_tag(:span, c) + (i == 2 ? ',' : '')
end.join().html_safe

【讨论】:

    【解决方案4】:

    我认为您需要先反转字符串,以确保您始终保留三个剩余数字,并且不会以“123,45”或“123,45”之类的结尾:

    如果您对正则表达式感到满意:

    def project_total_counter
      results = []
    
      Project.all.count.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse.each do |char|
        results << char == "," ? "," : content_tag(:span, char)
      end
    
      results
    end
    

    假设您使用的是 Rails,我更喜欢 @mbratch 的回答。上述正则表达式的好处是它也适用于纯 Ruby。

    【讨论】:

      【解决方案5】:
      def project_total_counter
        total = Project.count
        span_groups = []
        total.to_s.each_char.in_groups_of(3) do |chars|
          span_groups << chars.map{|c| content_tag(:span, c)}.join
        end
        span_groups.join("\n,\n").html_safe
      end
      

      或者更简洁的

      def project_total_counter
        Project.count.to_s.each_char.in_groups_of(3).map{|chars| chars.map{|c| content_tag(:span, c)}.join}.join("\n,\n").html_safe
      end
      

      请注意,您可以只说 Project.count 而不是 Project.all.count

      【讨论】:

        【解决方案6】:

        由于您使用的是 Rails,请使用 ActionView::Helpers::NumberHelper

        total = Project.all.count
        
        include ActionView::Helpers::NumberHelper
        number_with_delimiter(total, delimiter: ",").each_char.map { |c|
          content_tag(:span, c)
        }.join.html_safe
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-17
          • 2013-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-08
          相关资源
          最近更新 更多