Skip to content

Instantly share code, notes, and snippets.

@qpfiffer
Created June 10, 2015 21:42
Show Gist options
  • Save qpfiffer/a6ccb303e17951563799 to your computer and use it in GitHub Desktop.
Save qpfiffer/a6ccb303e17951563799 to your computer and use it in GitHub Desktop.
# Access elements of a dictionary or a class by string:
def get_var_ruby_style(var, x):
return x[var] if type(x).__name__ == 'dict' else getattr(x, var)
# Return a bound function that always accesses said element, like 'get_var_bound_ruby_style("added_at")' would
# always access the "added_at" value of whatever you pass in to it. Hooray closures!
def get_var_bound_ruby_style(var):
def bound_ruby_style(x):
return get_var_ruby_style(var, x)
return bound_ruby_style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment