Ruby 如何打印出*和#x27表示的散列中每个键的值;s

Ruby 如何打印出*和#x27表示的散列中每个键的值;s,ruby,for-loop,hash,Ruby,For Loop,Hash,这是我的密码: class String def frequency chars.each_with_object(Hash.new(0)) do |char, h| h["#{char.upcase}:"] += 1 if char[/[[:alpha:]]/] end end end 我曾经尝试过用更小的代码来分解它,比如使用.times do循环,但我无法理解 例如: str = "\*" h["A:"] = count('a').times do s

这是我的密码:

class String
  def frequency
    chars.each_with_object(Hash.new(0)) do |char, h|
      h["#{char.upcase}:"] += 1 if char[/[[:alpha:]]/]
    end
  end
end
我曾经尝试过用更小的代码来分解它,比如使用.times do循环,但我无法理解

例如:

str = "\*"
h["A:"] = count('a').times do
str
end

您是否正在尝试执行以下操作:

counts = 'aassssvvvvv'.frequency
counts.each{|key,count| puts key + '*'*count}

# A:**
# S:****
# V:*****

Or if you want to change the key you can do:

counts.each{|key,amount| counts[key] = '*'*amount}

我不清楚。。输入字符串是什么?问题是什么。这个问题确实需要样本输入和输出。你是如此聪明和性感。#基本Ruby中似乎没有频率。它是Rails扩展吗?
frequency
是OP添加到String类的函数,请参见原始问题。