On Jan 20, 10:05 ¨Âí¬ ÂòéáÃáîäìå¼â®ãáîä®®®Àðïâïø®ãïí÷òïôåº > That simplifies things a bit. Here it is for completeness: > > --- myapp.rb --- > require 'rubygems' > require 'sinatra' > require 'json' > require 'widget' > > get '/widgets.json' do > Widget.all.to_json > end > > get '/widgets/:id.json' do > Widget.find(Integer(params[:id])).to_json > end > > post '/widgets.json' do > item = JSON.parse(request.body.read) > Widget.create(item).to_s > end > > --- widget.rb --- > # Rubbish model, not thread-safe! > class Widget > attr_accessor :id, :name, :price > def initialize(params) > @id params["id"] > @name params["name"] > @price = params["price"] > end > def to_json(*a) > instance_variables.inject({}) { |h,v| > h[v.to_s[1..-1]] = instance_variable_get(v) > h > }.to_json(*a) > end > > @all = [] > @seq = 0 > def self.all > @all > end > def self.add(item) > item.id = (@seq += 1) > all << item > return @seq > end > def self.create(params) > add(new(params)) > end > def self.find(id) > all.find { |item| item.id == id } > end > end > Widget.create("name" => "flurble", "price" => 12.3) > Widget.create("name" => "boing", "price" => 4.56) > > If you require 'json/add/rails' then you don't need to define your own > to_json method, but it also allows people to create arbitrary Ruby > objects on your machine (which makes me uncomfortable) Thanks. I'll give it a go. T.