Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Last active August 29, 2015 13:57
Show Gist options
  • Save adammcarth/9717472 to your computer and use it in GitHub Desktop.
Save adammcarth/9717472 to your computer and use it in GitHub Desktop.
Using ("dynamic"?) classes in ruby...
# Active Record lets you do things like:
Post.find(3).title
#=> "Hello, world!"
# Cool. So my question is, how exactly do you setup a class that allows the
# programmer to use there own keyword? My use case:
def get(type, id)
# blah
end
get("preferencs", 4)
#=> { "fav_color" => "blue", "food" => "pasta" } ... I HAVE THIS WORKING
get("preferences", 4).fav_color
#=> blue
# Somewhere, somehow, I need to be able to access the information returned by
# get("prefrences", 4) AND be able to access the "fav_color" keyword (in this case).
# Is that some extra code I need in my get method? Or is a seperate class required?
# This is the inheritence structure I am working with
module Get
def get(type, id)
# blah
end
end
class Main
extend Get
end
# Main.get(blah)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment