Skip to content

Instantly share code, notes, and snippets.

@danmcclain
Created December 2, 2011 02:04
Show Gist options
  • Select an option

  • Save danmcclain/1421387 to your computer and use it in GitHub Desktop.

Select an option

Save danmcclain/1421387 to your computer and use it in GitHub Desktop.
Metaprogramming instance methods
module Rubyoverflow
class Client
Rubyoverflow.constants.select {|c| Class === Rubyoverflow.const_get(c) and Rubyoverflow.const_get(c) < Rubyoverflow::Base}.each do |class_sym|
send :attr_reader, class_sym.downcase
send "define_method", class_sym.downcase do
instance_variable_set("@#{class_sym.downcase}", Rubyoverflow.const_get(class_sym).new(self)) unless instance_variable_get("@#{class_sym.downcase}")
instance_variable_get "@#{class_sym.downcase}"
end
end
end
end
module Rubyoverflow
class Client
@users = nil
def users
@user ||= Users.new(self)
end
#Repeated for all the other routes
end
end
module Rubyoverflow
class Users < Rubyoverflow::Base
#...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment