Module: Hashie::Extensions::MethodReader

### Home Classes Methods

### In Files

- lib/hashie/extensions/method_access.rb

### Methods

- #method_missing
- #respond_to?

### Class/Module Index

Quicksearch

- Hashie
- Hashie::Clash
- Hashie::Clash::ChainError
- Hashie::CoercionError
- Hashie::Dash
- Hashie::Extensions
- Hashie::Extensions::Coercion
- Hashie::Extensions::Coercion::ClassMethods
- Hashie::Extensions::Coercion::InstanceMethods
- Hashie::Extensions::Dash
- Hashie::Extensions::Dash::IndifferentAccess
- Hashie::Extensions::Dash::IndifferentAccess::ClassMethods
- Hashie::Extensions::DeepFetch
- Hashie::Extensions::DeepFetch::UndefinedPathError
- Hashie::Extensions::DeepFind
- Hashie::Extensions::DeepMerge
- Hashie::Extensions::IgnoreUndeclared
- Hashie::Extensions::IndifferentAccess
- Hashie::Extensions::KeyConversion
- Hashie::Extensions::Mash
- Hashie::Extensions::Mash::SafeAssignment
- Hashie::Extensions::MergeInitializer
- Hashie::Extensions::MethodAccess
- Hashie::Extensions::MethodAccessWithOverride
- Hashie::Extensions::MethodOverridingWriter
- Hashie::Extensions::MethodQuery
- Hashie::Extensions::MethodReader
- Hashie::Extensions::MethodWriter
- Hashie::Extensions::Parsers
- Hashie::Extensions::Parsers::YamlErbParser
- Hashie::Extensions::PrettyInspect
- Hashie::Extensions::StringifyKeys
- Hashie::Extensions::StringifyKeys::ClassMethods
- Hashie::Extensions::SymbolizeKeys
- Hashie::Extensions::SymbolizeKeys::ClassMethods
- Hashie::Hash
- Hashie::Mash
- Hashie::Rash
- Hashie::Trash

No matching classes.

# Hashie::Extensions::MethodReader

MethodReader allows you to access keys of the hash via method calls. This gives you an OStruct like way to access your hash's keys. It will recognize keys either as strings or symbols.

Note that while nil keys will be returned as nil, undefined keys will raise NoMethodErrors. Also note that respond_to? has been patched to appropriately recognize key methods.

@example

    class User < Hash
      include Hashie::Extensions::MethodReader
    end

    user = User.new
    user['first_name'] = 'Michael'
    user.first_name # => 'Michael'

    user[:last_name] = 'Bleigh'
    user.last_name # => 'Bleigh'

    user[:birthday] = nil
    user.birthday # => nil

    user.not_declared # => NoMethodError

### Public Instance Methods

method_missing(name, *args) click to toggle source

    # File lib/hashie/extensions/method_access.rb, line 35
    def method_missing(name, *args)
      if key?(name)
        self[name]
      else
        sname = name.to_s
        if key?(sname)
          self[sname]
        elsif sname[-1] == '?'
          kname = sname[0..-2]
          key?(kname) || key?(kname.to_sym)
        else
          super
        end
      end
    end

respond_to?(name, include_private = false) click to toggle source

    # File lib/hashie/extensions/method_access.rb, line 30
    def respond_to?(name, include_private = false)
      return true if key?(name.to_s) || key?(name.to_sym)
      super
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.