# Unexpected length of Â£ (pound) character?

**URL:** <https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644>\
**Category:** Ruby\
**Created:** [June 4, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644 "2010-06-04T12:19:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [June 4, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644/1 "2010-06-04T12:19:07Z")

</div>

Hi,

Today I came across an issue with a customer custom report which was out  
by 1 char over 40 or so lines. At first thought I had incorrectly  
limited the field length, however the problem is only present where  
there is a ‘Â£’ char.

For example:  
“1234”.length =\> 4  
“1234Â£”.length =\> 6 (Expect 5)  
“1234Â£6”.length =\> 7 (Expect 6)

Tested on:  
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]  
and:  
ruby 1.8.6 (2009-03-31 patchlevel 368) [x86\_64-linux]

I could not find anything in google covering this (Perhaps my google-fu  
needs work) which brought me here.

Is this expected functionality in ruby? It does not seem right in my  
mind.

Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [June 4, 2010, 12:28pm UTC](https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644/2 "2010-06-04T12:28:54Z")

</div>

On Fri, Jun 4, 2010 at 6:19 PM, Anthony Ss  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For example:  
> “1234”.length =\> 4  
> “1234£”.length =\> 6 (Expect 5)  
> “1234£6”.length =\> 7 (Expect 6)

can’t help you there, but fyi

> RUBY\_VERSION  
> =\> “1.9.2”  
> “1234”.length  
> =\> 4  
> “1234£”.length  
> =\> 5  
> “1234£6”.length  
> =\> 6

kind regards -botp

---

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [June 4, 2010, 12:47pm UTC](https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644/3 "2010-06-04T12:47:56Z")

</div>

Anthony Stenhouse wrote:

> Hi,
> 
> Today I came across an issue with a customer custom report which was out  
> by 1 char over 40 or so lines. At first thought I had incorrectly  
> limited the field length, however the problem is only present where  
> there is a ‘Â£’ char.
> 
> For example:  
> “1234”.length =\> 4  
> “1234Â£”.length =\> 6 (Expect 5)  
> “1234Â£6”.length =\> 7 (Expect 6)

In UTF-8, “Â£” is two bytes, and ruby 1.8 gives you the number of bytes.

If you want to capture (say) the first 6 characters of the string, try  
this:

> > a = “1234Â£6789”  
> > =\> “1234\302\2436789”
> 
> > a =~ /\A(.{6})/u  
> > =\> 0
> 
> > puts $1  
> > 1234Â£6  
> > =\> nil

This may be sufficient for simple wrapping functions. Or look at the  
Iconv library.

> Is this expected functionality in ruby? It does not seem right in my  
> mind.

ruby 1.9 works in characters. It brings with it enormous complexity,  
pitfalls and inconsistencies. Pick your poison 🙂

---

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [June 4, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644/4 "2010-06-04T14:41:12Z")

</div>

On 2010-06-04 06:19:09 -0400, Anthony Ss said:

> Thanks. 🙂  
> Do yourself a favor, friend, and read this excellent article on  
> Character Encoding:

> **[The Absolute Minimum Every Software Developer Absolutely, Positively Must...](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/)**
>
> Ever wonder about that mysterious Content-Type tag? You know, the one you’re supposed to put in HTML and you never quite know what it should be? Did you ever get an email from your friends in…

Then, find out if the character encoding for your file and your  
interpreter is the same. 😛

---

<div class="post-metadata">

**Author:** ![anthonys](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@anthonys](https://www.ruby-forum.com/u/anthonys)\
**Post date:** [June 4, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/unexpected-length-of-a-pound-character/189644/5 "2010-06-04T15:04:02Z")

</div>

On Fri, Jun 4, 2010 at 6:47 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If you want to capture (say) the first 6 characters of the string, try
> 
> > > a = “1234£6789”  
> > > =\> “1234\302\2436789”  
> > > a =~ /\A(.{6})/u  
> > > =\> 0  
> > > puts $1  
> > > 1234£6  
> > > =\> nil

> “1234£6789”[0…5]  
> =\> “1234£6”

> ruby 1.9 works in characters. It brings with it enormous complexity,  
> pitfalls and inconsistencies. Pick your poison 🙂

All programmers are optimists. -Frederick Brooks, Jr.

😉

kind regards -botp
