Last active
November 27, 2017 21:22
-
-
Save monkstone/70c8fd7ee6c0256f30200232d9c2ed79 to your computer and use it in GitHub Desktop.
Reading and Writing YAML with jruby-complete.jar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
bubbles: | |
- xpos: 20 | |
ypos: 50 | |
diameter: 200 | |
emotion: happy | |
- xpos: 80 | |
ypos: 60 | |
diameter: 210 | |
emotion: sad | |
colors: | |
- xpos: 20 | |
ypos: 50 | |
diameter: 200 | |
colors: red | |
- xpos: 80 | |
ypos: 60 | |
diameter: 210 | |
colors: blue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"xpos"=>20, "ypos"=>50, "diameter"=>200, "emotion"=>"happy"} | |
{"xpos"=>80, "ypos"=>60, "diameter"=>210, "emotion"=>"sad"} | |
{"xpos"=>20, "ypos"=>50, "diameter"=>200, "colors"=>"red"} | |
{"xpos"=>80, "ypos"=>60, "diameter"=>210, "colors"=>"blue"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
data = YAML.load_file('data.yml') | |
data.fetch('bubbles').each do |bubble| | |
puts bubble | |
end | |
data.fetch('colors').each do |bubble| | |
puts bubble | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
values1 = [20, 50, 200, 'happy'] | |
values2 = [80, 60, 210, 'sad'] | |
labels = %w[xpos ypos diameter emotion] | |
labels2 = %w[xpos ypos diameter colors] | |
data = [values1, values2].map { |entry| labels.zip(entry).to_h } | |
data2 = [values1, values2].map { |entry| labels2.zip(entry).to_h } | |
hash = { 'bubbles' => data, 'colors' => data2 } | |
open('data.yml', 'w') { |file| file.write(hash.to_yaml) } |
Thank you! Extremely helpful example.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jeremydouglass
Using
jruby_complete
:-and
I have hand edited the data.yml produced by
write_yaml.rb