Skip to content

Instantly share code, notes, and snippets.

@akinsgre
Created September 2, 2011 13:38
Show Gist options
  • Save akinsgre/1188605 to your computer and use it in GitHub Desktop.
Save akinsgre/1188605 to your computer and use it in GitHub Desktop.
JSON generation
#ruby 1.9.2
require 'json'
class Item
def initialize(name, description )
@name = name
@description = description
end
def to_json(*a)
{"name" => @name, "description" => @description}.to_json(*a)
end
end
item = Item.new("Grain", "Base and Specialty Malts")
puts item.to_json(:only => [:name])
# {"name":"Grain","description":"Base and Specialty Malts"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment