Skip to content

Instantly share code, notes, and snippets.

@rgarver
Created January 3, 2009 23:31
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 rgarver/42953 to your computer and use it in GitHub Desktop.
Save rgarver/42953 to your computer and use it in GitHub Desktop.
require 'json.red'
json = '{a:"a string"}'
o = Object.from_json(json)
o.a # => "a string"
class Module
def define_method(sym, &block)
`this.prototype['m$'+sym.__value__]=block.__block__.__unbound__`
`Red.updateChildren(this)`
`Red.updateIncluders(this)`
return `block`
end
end
class Object
def self.from_json(text)
ret = Object.new
meths = []
`var v = eval("("+#{text}.__value__+")")`
`for(var member in v){#{meths}.push(new Array($q(member), $q(v
[member])))}`
meths.each do |meth|
ret.class.send(:define_method, meth[0]) do
meth[1]
end
end
return ret
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment