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?