Skip to content

Instantly share code, notes, and snippets.

@parndt
Created January 20, 2012 01:26
Show Gist options
  • Select an option

  • Save parndt/1644377 to your computer and use it in GitHub Desktop.

Select an option

Save parndt/1644377 to your computer and use it in GitHub Desktop.
Psych outputs a different file than it read in
require 'yaml'
my_yaml = YAML::load(File.read('en.yml')).to_yaml
=begin
1.9.3-p0 :002 > YAML::load(File.read('en.yml')).to_yaml
=> "---\nen:\n refinery:\n plugins:\n lovelies:\n title: Lovely Profiles\n people:\n admin:\n lovelies:\n actions:\n create_new: Add New Lovely profile\n reorder: Reorder Lovely profiles\n reorder_done: Done Reordering Lovely profiles\n records:\n title: Lovely\n sorry_no_results: Sorry! There are no results found.\n no_items_yet: There are no Lovely profiles yet. Click \"Add New Lovely\n profile\" to add one.\n lovely:\n view_live_html: View the lovely area live <br/><em>(opens in a new window)</em>\n edit: Edit this lovely profile\n delete: Remove this lovely profile forever\n lovelies:\n show:\n other: Other Lovely profiles\n activerecord:\n attributes:\n refinery/things/lovely:\n name: Name\n title: Title\n photo: Photo\n bio: Bio\n"
=end
# the problem is this: no_items_yet: There are no Lovely profiles yet. Click \"Add New Lovely\n profile\" to add one.
# Note how after \"Add New Lovely there is a \n as is demonstrated in en_after.yml
File.open('en_after.yml', 'w+') {|f| f.puts my_yaml }
en:
refinery:
plugins:
lovelies:
title: Lovely Profiles
people:
admin:
lovelies:
actions:
create_new: Add New Lovely profile
reorder: Reorder Lovely profiles
reorder_done: Done Reordering Lovely profiles
records:
title: Lovely
sorry_no_results: Sorry! There are no results found.
no_items_yet: There are no Lovely profiles yet. Click "Add New Lovely profile" to add one.
lovely:
view_live_html: View the lovely area live <br/><em>(opens in a new window)</em>
edit: Edit this lovely profile
delete: Remove this lovely profile forever
lovelies:
show:
other: Other Lovely profiles
activerecord:
attributes:
'refinery/things/lovely':
name: Name
title: Title
photo: Photo
bio: Bio
---
en:
refinery:
plugins:
lovelies:
title: Lovely Profiles
people:
admin:
lovelies:
actions:
create_new: Add New Lovely profile
reorder: Reorder Lovely profiles
reorder_done: Done Reordering Lovely profiles
records:
title: Lovely
sorry_no_results: Sorry! There are no results found.
no_items_yet: There are no Lovely profiles yet. Click "Add New Lovely
profile" to add one.
lovely:
view_live_html: View the lovely area live <br/><em>(opens in a new window)</em>
edit: Edit this lovely profile
delete: Remove this lovely profile forever
lovelies:
show:
other: Other Lovely profiles
activerecord:
attributes:
refinery/things/lovely:
name: Name
title: Title
photo: Photo
bio: Bio
@tenderlove
Copy link
Copy Markdown

I can't seem to get libyaml to stop wrapping that string. It's definitely a behavior of libyaml and not psych (the ruby wrapper). Is there a reason you need to prevent wrapping? Libyaml will roundtrip the data structures correctly. Even though a newline is added, when you read the YAML back in, the data will be exactly the same.

Here is an example test to show what I mean:

require 'psych'
require 'minitest/autorun'

class RoundTripTest < MiniTest::Unit::TestCase
  def test_load
    load_original = Psych.load_file 'en.yml'

    my_yaml = load_original.to_yaml

    # Make sure the dumped YAML is different than the file
    refute_equal my_yaml, File.read('en.yml')

    # Make sure the first load equals the second load
    assert_equal load_original, Psych.load(my_yaml)
  end
end

Note that even though the output yaml is different than the original file, the ruby data structures are exactly the same. I hope this was helpful. :(

@parndt
Copy link
Copy Markdown
Author

parndt commented Jan 24, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment