[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Saving UTF-8 characters to YAML problem

Pawel Radecki

4/7/2008 10:25:00 PM

Hi there,

I'm trying to save multinational data to YAML with following simple
program:

require 'yaml'

# show "James Bond 007: Nightfire in Chinese"
# text in YAML form
puts '?????007:????'.to_yaml

and what I get is:
--- "\xE8\xA9\xB9\xE5\xA7\x86\xE6\x96\xAF\xE9\x82\xA6\xE5\xBE
\xB7007\xEF\xBC\x9A\xE6\x9A\x97\xE5\xA4\x9C\xE4\xB9\x8B\xE7\x81\xAB"

How can I make this to be human readable text?

Any help much appreciated.
Thanks in advance!

--
Pawel Radecki
e: pawel.j.radecki@gmail.com
w: http://radeckimarch.blo...
5 Answers

Phillip Gawlowski

4/7/2008 10:56:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

PaweÅ? Radecki wrote:
| Hi there,
|
| I'm trying to save multinational data to YAML with following simple
| program:
|
| require 'yaml'
|
| # show "James Bond 007: Nightfire in Chinese"
| # text in YAML form
| puts '詹å§?æ?¯é?¦å¾·007ï¼?æ??å¤?ä¹?火'.to_yaml
|
| and what I get is:
| --- "\xE8\xA9\xB9\xE5\xA7\x86\xE6\x96\xAF\xE9\x82\xA6\xE5\xBE
| \xB7007\xEF\xBC\x9A\xE6\x9A\x97\xE5\xA4\x9C\xE4\xB9\x8B\xE7\x81\xAB"
|
| How can I make this to be human readable text?

With an editor that understand UTF-8, I guess (considering that you use
characters I know from Polish, you probably already do). The OS has to
support UTF-8, too (I *think* that Windows does, and so should Mac OS X.
I'm not sure about other *NIX flavors).

If you mean Ruby, Iconv and Kconv are the way to go, AFAIK, to convert
strings between character sets.


- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan

Rule of Open-Source Programming #8:

Open-Source is not a panacea.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkf6pooACgkQbtAgaoJTgL/CCQCgo5vT8Jl0mynvcKRJn/36jNuc
XkkAn0RLEwKcyMv/A7BN6i0krgx++td7
=Umd0
-----END PGP SIGNATURE-----

Pawel Radecki

4/8/2008 9:30:00 PM

0

> install the ya2yaml gem and use "=F4=DA=D9=B5=DE=D9=DB=C0=D3=EC007=A3=BA=
=E4=DE=E5=A8=F1=FD=FB=FD".ya2yaml instead of .to_yaml

I really hoped this worked but it didn't for 100%.

Here is what I did:
I installed ya2yaml gem using "gem install ya2yaml" command (Windows
box) and "sudo gem install ya2yaml" Linux box. Got: ya2yaml-0.26.

Then I modified my simple program to be:
#!/usr/bin/env ruby

require 'yaml'
require 'ya2yaml'
require 'jcode'
$KCODE =3D 'u'

# show "James Bond 007: Nightfire" text
# in Chinese in YAML form
puts '=F4=DA=D9=B5=DE=D9=DB=C0=D3=EC007=A3=BA=E4=DE=E5=A8=F1=FD=FB=FD'.ya2ya=
ml

and ran it both on my Windows and Linux boxes.

Windows: When I redirect results to a file (running "yaml_test.rb >
test.yaml") it works perfectly but when I try to display results on a
screen (running "yaml_test.rb") with 65001 active code page (set
through "chcp 65001" in command line) I see:
(squares)007(squares) and an error message: "in 'write' Bad file
descriptor (Errno::EBADF)".

Linux: While running above program I get:
"./yaml_test.rb:4:in `require': no such file to load -- ya2yaml
(LoadError) from ./yaml_test.rb:4"

Any clue how to work around this?
Any help much appreciated!

--
Pawe=A9=A9 Radecki
e: pawel.j.radecki@gmail.com
w: http://radeckimarch.blo...

Justin Collins

4/8/2008 10:25:00 PM

0

Pawe?? Radecki wrote:
>> install the ya2yaml gem and use "oUUµÞUUAOi007?ºaÞa¨nyuy".ya2yaml instead of .to_yaml
>>
>
> I really hoped this worked but it didn't for 100%.
>
> Here is what I did:
> I installed ya2yaml gem using "gem install ya2yaml" command (Windows
> box) and "sudo gem install ya2yaml" Linux box. Got: ya2yaml-0.26.
>
> Then I modified my simple program to be:
> #!/usr/bin/env ruby
>
> require 'yaml'
> require 'ya2yaml'
> require 'jcode'
> $KCODE = 'u'
>
> # show "James Bond 007: Nightfire" text
> # in Chinese in YAML form
> puts 'oUUµÞUUAOi007?ºaÞa¨nyuy'.ya2yaml
>
> and ran it both on my Windows and Linux boxes.
>
> Windows: When I redirect results to a file (running "yaml_test.rb >
> test.yaml") it works perfectly but when I try to display results on a
> screen (running "yaml_test.rb") with 65001 active code page (set
> through "chcp 65001" in command line) I see:
> (squares)007(squares) and an error message: "in 'write' Bad file
> descriptor (Errno::EBADF)".
>
> Linux: While running above program I get:
> "./yaml_test.rb:4:in `require': no such file to load -- ya2yaml
> (LoadError) from ./yaml_test.rb:4"
>
> Any clue how to work around this?
> Any help much appreciated!
>
> --
> Pawe?? Radecki
> e: pawel.j.radecki@gmail.com
> w: http://radeckimarch.blo...
>
>

Use

require 'rubygems'

before

require 'ya2yaml'


-Justin

Pawel Radecki

4/9/2008 12:58:00 AM

0

> Use
>
> require 'rubygems'
>
> before
>
> require 'ya2yaml'
>
> -Justin

It worked. Thanks, guys! You saved me hours!

--
Pawe=B3 Radecki
e: pawel.j.radecki@gmail.com
w: http://radeckimarch.blo...

Joao Silva

9/9/2008 7:46:00 PM

0

Here's a patch for "rake extract_fixtures" that also uses ya2yaml:
http://fukamachi.org/wp/2007/05/18/rails-dump-database-to-fixtures-preser...
Saving YAML as UTF-8 should really be default behavior. UnicodeDammit!
--
Posted via http://www.ruby-....