0

我有以下程序:

class Matcher
  include Enumerable
  def initialize(string, match)
    @string = string
    @match = match
  end

  def each
    @string.scan(/[@#match]/) do |pattern|
      yield pattern
    end
  end
end

mch = Matcher.new("the quickbrown fox", "aeiou")
puts mch.inject {|x, n| x+n}

它应该aeiou与字符串匹配字符the quickbrown fox

无论我把什么作为模式,它都会奇怪地打印出字符:thc. 这是怎么回事?

4

1 回答 1

3

@string.scan(/[@#match]/) do |pattern|是不正确的。#{@match}就是你要找的。

于 2012-04-08T18:21:57.490 回答