Source From Here
Question
How do I extend the String class, and attach a method named to_bytes?
How-To
Ruby already has a String#each_byte method which is aliased to String#bytes.
Prior to Ruby 1.9 strings were equivalent to bytes, i.e. a character was assumed to be a single byte. That's fine for ASCII text and the various text codings like Win-1252 andISO-8859-1 but fails badly with Unicode, which we see more and more often on the web. Ruby 1.9+ is Unicode aware, and strings are no longer considered to be made up of bytes, but instead are characters, which can be multiple bytes long.
So, if you are trying to manipulate text as single bytes, you'll need to ensure your input is ASCII, or at least a single-byte-based character set. If you might have multi-byte characters you should use String#each_char or String.split(//) or String.unpack with the U flag.
Example of unpack()
Question
How do I extend the String class, and attach a method named to_bytes?
How-To
Ruby already has a String#each_byte method which is aliased to String#bytes.
Prior to Ruby 1.9 strings were equivalent to bytes, i.e. a character was assumed to be a single byte. That's fine for ASCII text and the various text codings like Win-1252 andISO-8859-1 but fails badly with Unicode, which we see more and more often on the web. Ruby 1.9+ is Unicode aware, and strings are no longer considered to be made up of bytes, but instead are characters, which can be multiple bytes long.
So, if you are trying to manipulate text as single bytes, you'll need to ensure your input is ASCII, or at least a single-byte-based character set. If you might have multi-byte characters you should use String#each_char or String.split(//) or String.unpack with the U flag.
Example of unpack()
沒有留言:
張貼留言