ctype functionality without a gem?

A

Ammar Ali

Hello,

Is there a built-in method for identifying character types a la
ctype() in C? I would like to avoid requiring a gem dependency.

I'm considering the following approach, if nothing exists, but it
seems like overkill to me.

def ctype(c)
case c
when /[[:alnum:]]/; :alnum
when /[[:alpha:]]/; :alpha
# etc...
end
end

def alnum?(c); ctype(c) == :alnum end
def alpha?(c); ctype(c) == :alpha end
# etc...

Also, it requires some 1.8 vs 1.9 special cases for 'ascii' and
'word', if not more.

Regards,
Ammar
 
R

Robert Klemme

Is there a built-in method for identifying character types a la
ctype() in C? I would like to avoid requiring a gem dependency.

No other than regexp as far as I know.
I'm considering the following approach, if nothing exists, but it
seems like overkill to me.

def ctype(c)
=A0case c
=A0when /[[:alnum:]]/; :alnum
=A0when /[[:alpha:]]/; :alpha
=A0# etc...
=A0end
end

def alnum?(c); ctype(c) =3D=3D :alnum end
def alpha?(c); ctype(c) =3D=3D :alpha end
# etc...

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+)/ =3D~ 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
=3D> ["foo", "bar", "123"]

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

Ammar Ali

What do you need that for? =C2=A0Why not directly use a regexp to match a
string? =C2=A0Often 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+)/ =3D~ 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
=3D> ["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
 
A

Ammar Ali

This is similarily fragile. If you are only testing for one character,
it seems good, if you are going to test for strings, it will not be
enough.

I do only need to test individual characters. Extending the code to
match entire strings should be easy:

/^[[:alnum:]]+$/

Also, the whole solution is not necessarily fast - I do love regular
expressions, but I would search on for other methods to meet your
requirements. It really depends on what your goal is. If it is about
bit streams, you might want to look into bit-struct or similar
approaches that use Array#pack and String#unpack.

I agree, and wish this was built-in. Interesting suggestions. Thanks.

Regards,
Ammar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top