String#chop chops last byte, not char

E

Evgeni Belin

This is happening in ruby 1.8.6:

% ruby --version
ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-darwin9.1.0]

Documentation says:

% ri String#chop
...Returns a new +String+ with the last character removed. If...

This is irb and Rails script/console sessions:

% ./script/console
Loading development environment (Rails 2.0.2)=> "абвг"

% irb=> ""


As you can see chop removes last byte vs last char. Btw, problem
happened while stuffing strings into legacy (ms sql server) db,
varchar(255) column. I came up with the following method (see below).
Maybe there is an easier alternative?

def truncate(text, size = 254, suffix = "...")
if text.nil? then return end
l = size - suffix.size
if text.size > size
truncated_text = ""
text.each_char do |c|
if truncated_text.size + c.size < l
truncated_text << c
else
break
end
end
truncated_text += suffix
else
text
end
end

I did not report this as a bug, since I am not sure who is supposed to
be right - ruby or documentation.
 
C

Charles Oliver Nutter

Evgeni said:
This is happening in ruby 1.8.6:

In Ruby 1.8.6, character == byte basically everywhere. You'll need to
use a regex to remove the last character safely (UTF-8).

- Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top