From: "Chealsea S." Date: 2008-11-03T08:17:01+09:00 Subject: Re: check if valid hexadecimal code? I tried "hex_pattern" with a string like "asdbgg" and it does not produce an error, even though "g" isn't A-F 0-9. I should specify that I'm trying to build a hexadecimal color checker. I tried the folllowing code but it doesn't work: def ishex(hexcolor) if (hexcolor.to_s.length == 6) == false then false elsif hexcolor.hex == 0 false else true end end Thanks for any help. Avdi Grimm wrote: > On Sun, Nov 2, 2008 at 4:30 PM, Chealsea S. > wrote: >> How would you check if a string is a valid hexadecimal code? > > irb(main):007:0> hex_pattern = /^[[:xdigit:]]+$/ > => /^[[:xdigit:]]+$/ > irb(main):008:0> hex_pattern === "0123456789ABCDEF" > => true > irb(main):009:0> hex_pattern === "Chunky Bacon" > => false > > > -- > Avdi > > Home: http://avdi.org > Developer Blog: http://avdi.org/devblog/ > Twitter: http://twitter.com/avdi > Journal: http://avdi.livejournal.com -- Posted via http://www.ruby-forum.com/.