{"id":881,"date":"2009-10-08T23:09:31","date_gmt":"2009-10-09T06:09:31","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=881"},"modified":"2009-10-09T09:33:52","modified_gmt":"2009-10-09T16:33:52","slug":"jamming-with-ruby-yaml","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2009\/10\/08\/jamming-with-ruby-yaml\/","title":{"rendered":"Jamming with Ruby YAML"},"content":{"rendered":"<p>When working with Ruby, the library\/class I use and abuse most often is YAML.  <a href=\"http:\/\/www.yaml.org\/\">YAML<\/a> stands for <b>YAML Ain&#8217;t Markup Language<\/b> and it is a versatile human friendly data serialization format.  It is easier to use and understand than <a href=\"http:\/\/www.json.org\/\">JSON<\/a>.<\/p>\n<p>A YAML file is much like a Java properties file in that is used to store name\/value pairs.  YAML is more powerful than simple Java properties file but that is a good way to think of it to begin with.  Here is a example of a simple YAM file used to store user name and password.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nuser: juixe-username\r\npass: juixe-password\r\n<\/pre>\n<p>The above YAML snippet can go into a file, typically with a yml extension.  To load the YAML file in ruby you can do it in with following Ruby code.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nrequire 'yaml'\r\n\r\nyml = YAML::load(File.open('userinfo.yml'))\r\nputs yml&#x5B;'user'] # juixe-username\r\n<\/pre>\n<p>Just replace userinfo.yml with the name and path of your YAML file.  The object that is loaded from the YAML file is a regular Ruby hash object so you can iterate through all the name\/value pairs like the following.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nrequire 'yaml'\r\n\r\nyml = YAML.load_file 'userinfo.yml'\r\nyml.each_pair { |key, value|\r\n  puts &quot;#{key} = #{value}&quot;\r\n}\r\n<\/pre>\n<p>What makes YAML files more powerful than a regular Java properties file is that you can complex object collections, structures, or hierarchies.  For example, imagine that I want to log into a series of Twitter accounts and get their most recent at replies.  I can keep a collection of twitter account usernames and passwords in a YAML file much like the following.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\njuixe:\r\n user: juixe-user\r\n pass: juixe-pass\r\ntechknow:\r\n user: techknow-user\r\n pass: techknow-pass\r\n<\/pre>\n<p>Here is the sample Ruby code that can be used to iterate through each user account from the YAML file.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nrequire 'yaml'\r\n\r\nyml = YAML.load_file 'userinfo.yml'\r\nyml.each_key { |key|\r\n  username = yml&#x5B;key]&#x5B;'user']\r\n  password = yml&#x5B;key]&#x5B;'pass']\r\n\r\n  puts &quot;#{username} =&gt; #{password}&quot;\r\n  # login ...\r\n}\r\n<\/pre>\n<p>You build more complex data structures than this using YAML, but this should be enough to get you going.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with Ruby, the library\/class I use and abuse most often is YAML. YAML stands for YAML Ain&#8217;t Markup Language and it is a versatile human friendly data serialization format. It is easier to use and understand than JSON. A YAML file is much like a Java properties file in that is used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[22,3],"tags":[57,55,56,806,54],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-ed","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/881"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=881"}],"version-history":[{"count":5,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":887,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/881\/revisions\/887"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}