# Puts bug?

**URL:** <https://www.ruby-forum.com/t/puts-bug/188356>\
**Category:** Ruby\
**Created:** [May 10, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/puts-bug/188356 "2010-05-10T20:09:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 10, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/puts-bug/188356/1 "2010-05-10T20:09:45Z")

</div>

With this file:

# coding: UTF-8

puts “asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”

output in windows is:

C:\dev\digitalarchive\_trunk\>ruby -v go.rb  
ruby 1.9.3dev (2010-05-11 trunk 27724) [i386-mingw32]  
asdfâ•¨Â£â•¨â••â•¨â•‘â•¨â••â•¨â•â•¨â–‘â•¤Ã¢â•¤Ã¼

Is this expected?

Also shouldn’t the following code fail?

“asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”.force\_encoding “IBM437”

(it doesn’t)

Thanks!  
-rp

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 10, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/puts-bug/188356/2 "2010-05-10T22:39:11Z")

</div>

On Tue, May 11, 2010 at 03:09:47AM +0900, Roger P. wrote:

> Is this expected?

Does your shell support UTF-8?

> Also shouldn’t the following code fail?
> 
> “asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”.force\_encoding “IBM437”
> 
> (it doesn’t)

Why would it fail? You’re just associating (by force) the encoding  
IBM437  
with some bag of bytes. Because you’re using force\_encoding(), whether  
or  
not those bytes represent valid characters in IBM437 is up to you.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 11, 2010, 11:10am UTC](https://www.ruby-forum.com/t/puts-bug/188356/3 "2010-05-11T11:10:26Z")

</div>

Roger P. wrote:

> With this file:
> 
> # coding: UTF-8
> 
> puts “asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”
> 
> output in windows is:
> 
> C:\dev\digitalarchive\_trunk\>ruby -v go.rb  
> ruby 1.9.3dev (2010-05-11 trunk 27724) [i386-mingw32]  
> asdfâ•¨Â£â•¨â••â•¨â•‘â•¨â••â•¨â•â•¨â–‘â•¤Ã¢â•¤Ã¼
> 
> Is this expected?

Yes.

One of the (many arbitrary) rules of ruby 1.9 encodings is that although  
there is an external\_encoding set on STDIN, there is no external  
encoding set on STDOUT.

So when you write a string to STDOUT, by default the sequence of bytes  
is transferred as-is to the terminal.

If you want strings to be transcoded on write, try putting this at the  
start of your program:

STDOUT.set\_encoding Encoding.locale\_charmap

or

STDOUT.set\_encoding Encoding.default\_external

or

STDOUT.set\_encoding “IBM437”

> Also shouldn’t the following code fail?
> 
> “asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”.force\_encoding “IBM437”
> 
> (it doesn’t)

No, it shouldn’t; you’re just tagging the string with a different  
encoding. In any case, notice that

str = “asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”.force\_encoding “IBM437”  
str.valid\_encoding?

returns true. This means that the sequence of bytes in str happens to be  
a valid sequence of characters in IBM437 (just not the characters you  
originally thought of)

However, the following does give an error, because you’re trying to  
transcode each character in the source string to an equivalent character  
in IBM437:

“asdfÐœÐ¸ÐºÐ¸Ð¼Ð°ÑƒÑ”.encode “IBM437”

# Encoding::UndefinedConversionError: U+041C from UTF-8 to IBM437

If you want to understand how String encodings work in ruby 1.9 - and I  
warn you in advance that you probably don’t - you could try reading

> <https://github.com/candlerb/string19/blob/master/string19.rb>

Then cry.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 12, 2010, 2:07pm UTC](https://www.ruby-forum.com/t/puts-bug/188356/4 "2010-05-12T14:07:02Z")

</div>

2010/5/11 Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > If you want to understand how String encodings work in ruby 1.9 - and I  
> > warn you in advance that you probably don’t - you could try reading  
> > [string19/string19.rb at master · candlerb/string19 · GitHub](http://github.com/candlerb/string19/blob/master/string19.rb)
> > 
> > Then cry.
> 
> Thanks much 🙂

There’s also James’ excellent article:

[http://blog.grayproductions.net/articles/miscellaneous\_m17n\_details](http://blog.grayproductions.net/articles/miscellaneous_m17n_details)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [May 11, 2010, 5:15pm UTC](https://www.ruby-forum.com/t/puts-bug/188356/5 "2010-05-11T17:15:23Z")

</div>

> If you want to understand how String encodings work in ruby 1.9 - and I  
> warn you in advance that you probably don’t - you could try reading  
> [string19/string19.rb at master · candlerb/string19 · GitHub](http://github.com/candlerb/string19/blob/master/string19.rb)
> 
> Then cry.

Thanks much 🙂  
-rp
