I am trying to validate strings in ruby. Any string which contains spaces,under scores or any special char should fail validation. The valid string should contain only chars a-z
You can just check if a special character is present in the string.
def validate str chars = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a str.chars.detect {|ch| !chars.include?(ch)}.nil? end
Result:
irb(main):005:0> validate "hello" => true irb(main):006:0> validate "_90 " => false