

Ruby: How to break a potentially unicode string in bytes
source link: https://www.codesd.com/item/ruby-how-to-break-a-potentially-unicode-string-in-bytes.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Ruby: How to break a potentially unicode string in bytes
I'm writing a game which is taking user input and rendering it on-screen. The engine I'm using for this is entirely unicode-friendly, so I'd like to keep that if at all possible. The problem is that the rendering loop looks like this:
"string".each_byte do |c|
render_this_letter(c)
end
I don't know a whole lot about i18n, but I know enough to know the above code is only ever going to work for me and people who speak my language. I'd prefer something like:
"unicode string".each_unicode_letter do |u|
render_unicode_letter(u)
end
Does this exist in the core distribution? I'm somewhat averse to adding additional requirements to the install, but if it's the only way to do it, I'll live.
For extra fun, I have no way of knowing if the string is, in fact, a unicode string.
EDIT: The library I'm using can indeed render entire strings, however I'm letting the user edit what comes up on the fly - if they hit 'backspace', essentially, I need to know how many bytes to chop off the end.
Unfortunately ruby 1.8.x has poor unicode support. It's being addressed in 1.9. But in the mean time, libraries like this one (http://snippets.dzone.com/posts/show/4527) are a good solution. Using the linked library, your code would look something like this:
"unicode_string".each_utf8_char do |u|
render_unicode_letter(u)
end
Recommend
-
8
The Ruby core team cares a lot about Unicode, and this is why we have pretty good Unicode support in the language. Even though the Unicode standard evolves consta...
-
4
Pretty funny to convert an array of bytes to a String or vice versa. String stores his value in a byte[], so the conversion should be straightforward. Convert byte[] to String In String.java we have a lot...
-
4
Truncating UTF String to the given number of bytes while preserving its validity [for DB insert] November 2, 2007 Often you n...
-
4
Python3和golang中bytes与string的转换 本文是最近对于python和golang中的bytes与string互相转换的总结与记录。 1 Bytes 创建 # Python my_bytes = bytes([0x06, 0xe5, 0x33, 0xfd, 0x1a, 0x...
-
13
In this article, we will learn what are Bytes and String in Python and how to convert bytes to a string using different techniques in Python. Table Of Contents What we know...
-
8
In this article, we will discuss what are Bytes and String, and also we will learn how to convert string to bytes using python. Table Of Contents Before Python3, the strings and bytes were of same object type, which...
-
8
Python Program to Convert Bytes to StringPython Program to Convert Bytes to String110 Views12/07/2022<p>In this video, we will learn...
-
7
Python Program to Convert String into BytesSkip to content Python Program to Convert String into Bytes...
-
7
-
11
Performance Gains in Ruby 3.3- String.dup vs. String#+ Jan 23, 2024 •
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK