1

I was looking through the ActiveModel::Validations source and found a comment that had this snippet of code in it:

    validates_each :first_name, :last_name do |record, attr, value|
      record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
    end

I understand what the snippet is demonstrating, but what caught my eye is the

    ?z

that the first character of the string is being compared to. So I spun up irb and, sure enough, ?z evaluates to a String with "z" as the value (while ?[ returns "[", ?? return "?", and ? followed by more than one character throws an exception).

I've done my fair share of googling and haven't been able to figure out what's going on here.

Any ideas?

RKitson
  • 2,003
  • 1
  • 18
  • 21
  • Yes. The answers to that question do answer this question. The problem I have is that I googled this for quite a while and never came across that question. – RKitson Sep 16 '15 at 22:07
  • And, as mentioned in the answers on the question that this duplicates, the ? is not an operator in this case. – RKitson Sep 16 '15 at 22:09
  • For googlers who find this answer, here are other related answers with detailed explanations: http://stackoverflow.com/q/4363067, http://stackoverflow.com/q/371333 – jkdev Nov 18 '15 at 21:27
  • Also: http://stackoverflow.com/q/5435937 – jkdev Nov 18 '15 at 21:32

0 Answers0