BrainLikeADullPencil
BrainLikeADullPencil

Reputation: 11653

comparing fixed number to a string

I'm trying to learn from some code created for RubyQuiz, but it seems to be built with an older version of Ruby that is now throwing an error when I try it with 1.9.2. I get this error when running the tests

 in `>=': comparison of Fixnum with String failed (ArgumentError)

running the line 'if c >= ?A and c <= ?Z'. Being rather inexperienced, I'm not sure if that's something that can be tweaked within the function itself, or if i'd need to post the whole code, which might not make it worthwhile. Please advise

def process(s, &combiner)
    s = sanitize(s)
    out = ""
    s.each_byte { |c|
      if c >= ?A and c <= ?Z            #error
        key = @keystream.get
        res = combiner.call(c, key[0])
        out << res.chr
      else
        out << c.chr
      end
    }
    return out
  end

Upvotes: 1

Views: 905

Answers (1)

oldergod
oldergod

Reputation: 15000

?A used to return the ascii code of the character in Ruby 1.8, but return the character itself now in Ruby 1.9. You should replace ?A by 'A'.ord.

In Ruby 1.8.x

?A
#=> 65

In Ruby 1.9.x

?A
#=> "A"

In both Ruby 1.8.x and 1.9.x

'A'.ord
#=> 65

Upvotes: 1

Related Questions