From: Ammar Ali Date: 2010-10-28T18:16:09+09:00 Subject: Re: ctype functionality without a gem? On Thu, Oct 28, 2010 at 11:47 AM, Robert Klemme wrote: > On Thu, Oct 28, 2010 at 9:50 AM, Ammar Ali wrote: >> Is there a built-in method for identifying character types a la >> ctype() in C? I would like to avoid requiring a gem dependency. > What do you need that for?  Why not directly use a regexp to match a > string?  Often you can use capturing groups in a single regexp, e.g. > > irb(main):009:0> %w{foo bar 123}.each do |s| > irb(main):010:1* if /(\d+)|(\w+)/ =~ s > irb(main):011:2> puts "number" if $1 > irb(main):012:2> puts "chars" if $2 > irb(main):013:2> end > irb(main):014:1> end > chars > chars > number > => ["foo", "bar", "123"] That's very cool. I'll have to remember that one. I do actually need to test if a character is of a certain type, all by itself, as part of a parser I'm working on. I came up with this quick solution for now, http://gist.github.com/650968 Thanks, Ammar