开发者

ya2yaml vs. params

I'm working with Rails 3 and ya2yaml, and I'm having an encoding issue trying to convert a params hash to yaml. The keys for the params hash are being stored as ASCII-8BIT and the values are being stored as UTF-8. This creates a problem, since ya2yaml doesn't like ASCII, and if you try to generate yaml from an ASCII stri开发者_StackOverflow社区ng using ya2yaml, you get this:

--- 
? !binary |
    b25l

: "some_value"

Instead of this:

--- 
someKey : "some_value"

Blech. Does anyone know how to bypass this in ya2yaml, or force a hash's keys and values to use the same encoding?


I ended up just writing a method that duplicated the params hash and encoded the keys and values in UTF-8:

  def utf8_hash(some_hash) # convert hash key & values to utf-8 for proper translation
    new_hash = Hash.new
    some_hash.each do |key, value|
      new_hash[key.to_s.encode(Encoding::UTF_8)] = value.to_s.encode(Encoding::UTF_8)
    end    
    new_hash
  end

  utf8_hash(params).ya2yaml
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜