Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created June 12, 2015 15:31
Show Gist options
  • Save djberg96/0ce360f7bd111c85cb02 to your computer and use it in GitHub Desktop.
Save djberg96/0ce360f7bd111c85cb02 to your computer and use it in GitHub Desktop.
forwardable
require 'json'
require 'ostruct'
require 'forwardable'
class Person
extend Forwardable
# I want to delegate all the methods
delegate [:firstname] => :@ostruct
def initialize(json)
@json = json
@ostruct = JSON.parse(json, object_class: OpenStruct)
end
end
json_string = '{"firstname":"jeff", "lastname":"durand", "address": { "street":"22 charlotte rd", "zipcode":"01013", "residents": 3 }}'
p = Person.new(json_string)
p p.class
p p.firstname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment