Calculating string length in a model

I’m using Ruby 2.1.1 and Rails 4.1.1, with SQLite and, respectively,
Postgres.

All my strings are supposed to be UTF-8 encoded.

Now, assuming I create a field in my model as

data:string{20}

can I safely assume, that this will store up to 20 characters, even if
each character happens to be 3 bytes long in UTF-8 encoding?

On Wednesday, 11 June 2014 07:02:17 UTC-5, Ruby-Forum.com User wrote:

can I safely assume, that this will store up to 20 characters, even if
each character happens to be 3 bytes long in UTF-8 encoding?

From the Postgres docs
(http://www.postgresql.org/docs/9.3/static/datatype-character.html):

“SQL defines two primary character types: character varying(n) and
character(n), where n is a positive integer. Both of these types can
store
strings up to n characters (not bytes) in length.”

So that sounds like yes. :slight_smile:

–Matt J.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs