문제

I know this question may seem very specific, but I'm trying to update some scripts to run on ruby 1.9 and have run into this very similar error on more than one occasion. I'm trying to run this code here: http://x.gfax.ch/Archives/Scripts/boggle.rb but get stuck in:

def Dict.to_n(ch)
    ch[0]-'a'[0]
end

In ruby1.8, this generates a random NxN boggle board then finds and outputs all the words it can find in it. In ruby1.9, however, the interpreter gives me this message:

boggle.rb:182:in `to_n': undefined method `-' for "a":String (NoMethodError)

What's wrong with my syntax? (If you need a dictionary file to play with the script, I'm using the one given at the bottom of this example page: http://learnruby.com/boggle/index.shtml) Thank you ahead of time for any guidance.

도움이 되었습니까?

해결책

You may want to try something like this:

  def Dict.to_n(ch)
    if ch.respond_to? :codepoints #1.9+
      ch.codepoints.first - 'a'.codepoints.first
    else
      ch[0]-'a'[0]
    end
  end

다른 팁

In Ruby 1.9, the method String#[] returns a string, while in Ruby 1.8 it returns a numerical value. (cf. here) You might consider using String#chars instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top