Skip to content

Instantly share code, notes, and snippets.

@aren55555
Created October 13, 2017 23:23
Show Gist options
  • Save aren55555/4eca83025be26b41ccc52a9fbf100165 to your computer and use it in GitHub Desktop.
Save aren55555/4eca83025be26b41ccc52a9fbf100165 to your computer and use it in GitHub Desktop.
Dima Example of Optional Args
require 'pp'
require 'securerandom'
require 'faker'
def generate_car(opts={})
type = opts[:car] || "car"
id = opts[:id] || SecureRandom.uuid
attrs = opts[:json_attrs] || {}
attributes = {
make: Faker::Company.name,
model: Faker::Commerce.product_name,
year: (1980...2017).to_a.sample,
}.merge(attrs)
{
"data": {
"id": id,
"type": type,
"attributes": attributes.merge(attrs),
}
}
end
c1 = generate_car()
c2 = generate_car(json_attrs: {make: "Ford"})
pp c1
pp c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment