module Hashie::Extensions::MethodWriter

MethodWriter gives you key_name= shortcuts for writing to your hash. Keys are written as strings, override convert_key if you would like to have symbols or something else.

Note that MethodWriter also overrides respond_to such that any method_name= will respond appropriately as true.

@example

class MyHash < Hash
  include Hashie::Extensions::MethodWriter
end

h = MyHash.new
h.awesome = 'sauce'
h['awesome'] # => 'sauce'

Public Instance Methods

convert_key(key) click to toggle source

@example Normalize a writer key

hash = {}.extend(Hashie::Extensions::MethodWriter)
hash.convert_key(:name) # => 'name'
# File lib/hashie/extensions/method_access.rb, line 104
def convert_key(key)
  key.to_s
end
method_missing(name, *args) click to toggle source

@example Write string keys with assignment syntax

hash = {}.extend(Hashie::Extensions::MethodWriter)
hash.name = 'Ada'
hash # => { 'name' => 'Ada' }
Calls superclass method
# File lib/hashie/extensions/method_access.rb, line 95
def method_missing(name, *args)
  return self[convert_key(Regexp.last_match[1])] = args.first if args.size == 1 && name.to_s =~ /(.*)=$/

  super
end
respond_to_missing?(name, include_private = false) click to toggle source

@example Advertise arbitrary writers

hash = {}.extend(Hashie::Extensions::MethodWriter)
hash.respond_to?(:name=) # => true
hash.respond_to?(:name) # => false
Calls superclass method
# File lib/hashie/extensions/method_access.rb, line 85
def respond_to_missing?(name, include_private = false)
  return true if name.to_s =~ /=$/

  super
end