Calculating string length in a model

32 views
Skip to first unread message

Ronald Fischer

unread,
Jun 11, 2014, 8:02:17 AM6/11/14
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Matt Jones

unread,
Jun 12, 2014, 9:12:13 AM6/12/14
to rubyonra...@googlegroups.com


On Wednesday, 11 June 2014 07:02:17 UTC-5, Ruby-Forum.com User wrote:
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?



"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. :)

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages