From: Brian Candler Date: 2008-11-05T23:28:16+09:00 Subject: Re: irb and ruby giving different results Nit Khair wrote: > in IRB, > ASCII = (0..255).map{|c| c.chr } > PRINTABLE = ASCII.grep(/[[:print:]]/) > PRINTABLE.length >>>> 191 > > However, inside the ruby program PRINTABLE.length only gives 95 !! ??? > > #!/opt/local/bin/ruby > ASCII = (0..255).map{|c| c.chr } > puts(ASCII.length) > PRINTABLE = ASCII.grep(/[[:print:]]/) > puts(PRINTABLE.length) > # -> 95 instead of 191 > > (Using ruby 1.8.7 on OS X 10.5.5, powerpc). Ran both from same Terminal. > Both use /opt/local/bin/ruby. > > Why this difference? FWIW, I get 95 with irb187 under Ubuntu Dapper. Looking at source code, the [[:print:]] character class uses isascii(c) && isprint(c) man isprint says: NOTE The details of what characters belong into which class depend on the current locale. For example, isupper() will not recognize an A-umlaut (Ä) as an uppercase letter in the default C locale. So look at what ENV.grep(/^LC/) shows. You could try setting ENV['LC_ALL']='C' in irb, or export LC_ALL=C before running it. Or try 'POSIX' instead of 'C'. Finally, be completely sure that your irb is running the right ruby. Check RUBY_VERSION within irb. -- Posted via http://www.ruby-forum.com/.