Skip to content

Instantly share code, notes, and snippets.

@scharfie
Created November 8, 2008 14:28
Show Gist options
  • Save scharfie/23073 to your computer and use it in GitHub Desktop.
Save scharfie/23073 to your computer and use it in GitHub Desktop.
class Hash
def method_missing(name, *args)
key = name.to_s
return self[key[0..-2]] = args.first if key =~ /=$/
has_key?(key) ? self[key] : self.class.new
end
end
params = { 'user' => { 'name' => 'Chris' } }
# getter
params::user # => {"name"=>"Chris"}
params::user::name # => "Chris"
# setter
params::user::username = 'scharfie'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment