How to detect if string contains only latin symbols using Ruby 1.9?

ascii, regex, ruby

Solution

I think that this should work for you:

 # encoding: UTF-8

 class String
   def just_latin?
     !!self.match(/^[a-zA-Z0-9_\-+ ]*$/)
   end
 end

 puts "123sdjjsf-4KSD".just_latin?
 puts "12333ыц4--sdf".just_latin?

Note that *#ascii_only?* is very close to what you want as well.

Problem

I need to detect if some string contains symbols from a non latin alphabet. Numbers and special symbols like `-`, `_`, `+` are good. I need to know whether there is any non latin symbols. For example: ``` "123sdjjsf-4KSD".just_latin? ``` should return `true`. ``` "12333ыц4--sdf".just_latin? ``` should return `false`.

Original source