Skip to content

Instantly share code, notes, and snippets.

@LBRapid
Created June 6, 2016 20:55
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 LBRapid/c4be829f95105ac1c80d6d7a76638b88 to your computer and use it in GitHub Desktop.
Save LBRapid/c4be829f95105ac1c80d6d7a76638b88 to your computer and use it in GitHub Desktop.
class Something
attr_accessor :groups
def initialize
@groups = []
end
def respond_to_missing?(method_sym, include_private = false)
@groups.map(&:name).detect { |name| name == method_sym.to_s }
end
def method_missing(method_sym, *args, &block)
@groups.detect { |group| group.name == method_sym.to_s } || super
end
end
class Group
attr_accessor :name, :data
def initialize(name, data)
@name = name
@data = data
end
def to_h
{ name: name, data: data }
end
end
s = Something.new
s.groups << Group.new('foobar', 'bazbat')
s.foobar.to_h
s.respond_to? :foobar #=> true
s.respond_to? :bingo #=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment