to_yaml in utf-8 encoding

  • Thread starter Alexey Petrushin
  • Start date
A

Alexey Petrushin

Hello, can you please help me with this tricky problem:
Is there a way to force utf-8 encoding in to_yaml, ruby 1.9?

require 'yaml'

p "alex".to_yaml # "--- alex\n"
p "=D0=90=D0=BB=D0=B5=D0=BA=D1=81".to_yaml # "---
\"\\xD0\\x90\\xD0\\xBB\\xD0\\xB5\\xD0\\xBA\\xD1\\x81\"\n"

P.S.
Maybe I missing something or don't understand. I can't get it, why we
should bother about encoding in todays?

Why there's such a strange situation with encoding in ruby 1.9? It's
impossible to set utf-8 as default (only via command-line command)? All
thouse tricky defaults and conversions ...

According to TIOBE the Java user base, installation base and community
about 30 times more than Ruby. And it's the universal language, You can
apply it generally almost to anything, and the only encoding that exist
there - it's UTF. In 99.9% of cases You use UTF.

So, why the Ruby adds such a complexity? What's the point, why you may
want ever to go with ANSI-8BIT?

-- =

Posted via http://www.ruby-forum.com/.=
 
7

7stud --

The yaml docs for ruby 1.9.2 do not list a to_yaml() method. And I
get this:

puts RUBY_VERSION
p "alex".to_yaml

--output:--
1.9.2
prog.rb:2:in `<main>': undefined method `to_yaml' for "alex":String
(NoMethodError)


To encode all strings in your source file in UTF-8, put this line at the
top of your program:

# encoding: utf-8
 
7

7stud --

The following works for me in ruby 1.8.7:


# encoding: UTF-8

require 'yaml'

File.open('data.db', 'w') do |f|
YAML::dump("a =E2=82=AC symbol", f)
end

---
contents of data.db:
--- "a \xE2\x82\xAC symbol"



File.open('data.db', 'r') do |f|
puts YAML::load(f)
end

--output:--
"a =E2=82=AC symbol"

My original string contains a euro symbol, and after a roundtrip through =

yaml, I got the same string back.

-- =

Posted via http://www.ruby-forum.com/.=
 
7

7stud --

7stud -- wrote in post #991873:
I changed the string, so the contents of data.db are actually:

--- "a Euro symbol: \xE2\x82\xAC"
 
A

Alexey Petrushin

Thanks for Your help,
prog.rb:2:in `<main>': undefined method `to_yaml' for "alex":String

strange, but maybe there are the "require 'yaml'" statement missing?
To encode all strings in your source file in UTF-8, put this line at the
top of your program:
# encoding: utf-8

Yes, I heard of this, You can also use this shortcut to do that:

export RUBYOPT="-Ku -rrubygems"

A round trip through yaml works for UTF-8 encoded strings in ruby 1.8.7.
In the program below, my original string contains a euro symbol, and
after a roundtrip through yaml, I get the same string back.

Sorry, I don't understand it, You shown that the cryptic output of YAML
can be loaded back to normal object.
But in my situation it's not enough, I need not ony load it back later
but it also should be readable in it's marshalled form.

I need to modify thouse *.yaml configs by hand, so those cryptic stuff
like "\xE2\x82\xAC" in *.yaml files is unacceptable.
 
A

Alexey Petrushin

Strange, Iconv says that 'ASCII-8BIT' encoding is uncknown:

require 'iconv'
Iconv.conv('ASCII-8BIT', 'UTF-8',
"\xD0\x90\xD0\xBB\xD0\xB5\xD0\xBA\xD1\x81")

test.rb:29:in `conv': invalid encoding ("ASCII-8BIT", "UTF-8")
(Iconv::InvalidEncoding)
from test.rb:29:in `<main>'
 
A

Alexey Petrushin

solved, here's the solution:

$ brew install libyaml

require 'psych'
require 'yaml'

As far as I understood the default yaml engine 'syck' is broken and
eventually will be replaced by psych in future ruby versions.
 
K

koulikoff

solved, here's the solution:

    $ brew install libyaml

    require 'psych'
    require 'yaml'

As far as I understood the default yaml engine 'syck' is broken and
eventually will be replaced by psych in future ruby versions.

Try ya2yaml
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top