Search code examples
rubystringutf-8byterabbitmq

Ruby: Limiting a UTF-8 string by byte-length


This RabbitMQ page states:

Queue names may be up to 255 bytes of UTF-8 characters.

In ruby (1.9.3), how would I truncate a UTF-8 string by byte-count without breaking in the middle of a character? The resulting string should be the longest possible valid UTF-8 string that fits in the byte limit.


Solution

  • For Rails >= 3.0 you have ActiveSupport::Multibyte::Chars limit method.

    From API docs:

    - (Object) limit(limit) 
    

    Limit the byte size of the string to a number of bytes without breaking characters. Usable when the storage for a string is limited for some reason.

    Example:

    'こんにちは'.mb_chars.limit(7).to_s # => "こん"