Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top