# Multibyte and Gems

**URL:** <https://www.ruby-forum.com/t/multibyte-and-gems/170820>\
**Category:** Ruby\
**Created:** [June 23, 2009, 7:27am UTC](https://www.ruby-forum.com/t/multibyte-and-gems/170820 "2009-06-23T07:27:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 23, 2009, 7:27am UTC](https://www.ruby-forum.com/t/multibyte-and-gems/170820/1 "2009-06-23T07:27:29Z")

</div>

I’ve tracked down a problem with a Gem I am trying to use. It turns  
out that it has some non-ascii characters in it; for example the  
second quote in the regular expression below is not an ASCII character:

```
 parts = self.split( %r/( [:.;?!][] | (?:[]|^)["Â“] )/x )

```

It produces errors like this:

:in `require’: /opt/local/lib/ruby1.9/gems/1.9.1/gems/webby-0.9.4/lib/  
webby/core\_ext/string.rb:14: invalid multibyte char (US-ASCII)  
(SyntaxError)

I fixed it by adding the following to the top of the offending file:

# encoding: utf-8

My questions:

- Is this the preferred fix?

- Is there a way to work around this problem without modifying the Gem?

- Is there an easy way to see if gems have non-ascii source files but  
haven’t included an encoding comment? Some kind of Ruby warning for  
instance.

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 23, 2009, 9:26am UTC](https://www.ruby-forum.com/t/multibyte-and-gems/170820/2 "2009-06-23T09:26:29Z")

</div>

On Jun 22, 2009, at 22:26, Martin H. wrote:

> lib/webby/core\_ext/string.rb:14: invalid multibyte char (US-ASCII)  
> (SyntaxError)
> 
> I fixed it by adding the following to the top of the offending file:
> 
> # encoding: utf-8
> 
> My questions:
> 
> - Is this the preferred fix?

Yes.

> - Is there a way to work around this problem without modifying the  
> Gem?

File a bug with the author and have them release a new version,  
otherwise no.

> - Is there an easy way to see if gems have non-ascii source files  
> but haven’t included an encoding comment? Some kind of Ruby warning  
> for instance.

ruby -c will do this for you.

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 23, 2009, 6:44pm UTC](https://www.ruby-forum.com/t/multibyte-and-gems/170820/3 "2009-06-23T18:44:29Z")

</div>

So is it considered best practice to put an encoding comment at the  
begging of all your files now days? Such as:

# encoding: utf-8

or whatever encoding you like. is this what people are doing or are  
they doing it one off for the files that have non-ascii characters?

It seems to me that if you have a modern editor it isn’t too hard to  
accidentally slip in some non-ascii characters resulting in some pain  
down the road.

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 25, 2009, 4:08am UTC](https://www.ruby-forum.com/t/multibyte-and-gems/170820/4 "2009-06-25T04:08:38Z")

</div>

On Wed, Jun 24, 2009 at 1:43 AM, Martin H.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the road.  
> It isn’t hard to mess up any code in a lot of ways, so as usual, try  
> to run/test it before you release/deploy 🙂  
> That also means that using Ruby 1.9.1 for your daily coding might be a  
> better choice, otherwise you’ll have to use multiruby.

---

<div class="post-metadata">

**Author:** ![himdel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/c67d28/32.png) [@himdel](https://www.ruby-forum.com/u/himdel)\
**Post date:** [June 26, 2009, 12:25am UTC](https://www.ruby-forum.com/t/multibyte-and-gems/170820/5 "2009-06-26T00:25:53Z")

</div>

On Jun 24, 2009, at 19:08, Michael F. wrote:

> > It seems to me that if you have a modern editor it isn’t too hard to  
> > accidentally slip in some non-ascii characters resulting in some  
> > pain down  
> > the road.
> 
> It isn’t hard to mess up any code in a lot of ways, so as usual, try  
> to run/test it before you release/deploy 🙂  
> That also means that using Ruby 1.9.1 for your daily coding might be a  
> better choice, otherwise you’ll have to use multiruby.

With hoe, it’s as easy as:

multiruby\_setup the\_usual # only once  
rake multi
