Skip to content

Instantly share code, notes, and snippets.

@matthewtrask
Last active January 16, 2020 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewtrask/cf98d35e000002b0140493ba7fb4ce55 to your computer and use it in GitHub Desktop.
Save matthewtrask/cf98d35e000002b0140493ba7fb4ce55 to your computer and use it in GitHub Desktop.
- name: Apimatic Transformer
category: converters
language: SaaS
description: Transform API Descriptions to and from RAML, API Blueprint, OAI v2/v3, WSDL, etc.
link: https://apimatic.io/transformer
v2: true
v3: true
- name: Google Gnostic
category: converters
github: https://github.com/googleapis/gnostic
language: Go
description: Compile OpenAPI descriptions into equivalent Protocol Buffer representations
v2: true
v3: true
{"name"=>"Apimatic Transformer", "category"=>"converters", "language"=>"SaaS", "description"=>"Transform API Descriptions to and from RAML, API Blueprint, OAI v2/v3, WSDL, etc.", "link"=>"https://apimatic.io/transformer", "v2"=>true, "v3"=>true}
{"name"=>"Google Gnostic", "category"=>"converters", "github"=>"https://github.com/googleapis/gnostic", "language"=>"Go", "description"=>"Compile OpenAPI descriptions into equivalent Protocol Buffer representations", "v2"=>true, "v3"=>true}
Trying to add a new key "key" and a value.
I know about the YAML lib and looping, but I dont know how ot manipulate an array of hashes.
What I currently have:
```ruby
#!/usr/bin/env ruby
require 'yaml'
yaml_string = File.read "./_data/tools.yml"
tools = YAML.load yaml_string
tools.map do |tool|
tool.store("key", tool["name"].downcase)
end
File.open("./_data/tools.yml", "w") {|file| file.write(tools) }
```
@itzsaga
Copy link

itzsaga commented Jan 16, 2020

You'll need to do something like this:

require 'yaml'

yaml_string = File.read "./tools.yaml"
tools = YAML.load yaml_string

tools.map do |tool|
  tool["key"] = "key"
end

See: https://repl.it/repls/WirelessJubilantDecimal

@matthewtrask
Copy link
Author

@itszaga.. I updated the gist and what I have works in that it adds the key and value. Like I mentioned in the tweet, its writing to the file with the hash, instead of yaml, so im trying to figure that one out.

@itzsaga
Copy link

itzsaga commented Jan 16, 2020

Change your last line to File.open("./_data/tools.yml", "w") { |file| file.write(tools.to_yaml) }

@matthewtrask
Copy link
Author

@itszaga yooo that worked! Thank you so much!

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