Created
January 3, 2009 23:31
-
-
Save rgarver/42953 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json.red' | |
json = '{a:"a string"}' | |
o = Object.from_json(json) | |
o.a # => "a string" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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