Class: OrderedHash

### Home Classes Methods

### In Files

- lib/nanoc3/base/ordered_hash.rb

### Parent

Hash

### Methods

- ::[]
- ::new
- ::to_yaml
- #==
- #[]=
- #__class__
- #class
- #clear
- #delete
- #delete_if
- #each
- #each_key
- #each_pair
- #each_value
- #each_with_index
- #first
- #inspect
- #invert
- #keys
- #last
- #merge
- #merge!
- #orig_store
- #pop
- #push
- #reject
- #reject!
- #replace
- #select
- #shift
- #store
- #store_only
- #to_a
- #to_s
- #unshift
- #update
- #values
- #yaml_inline!
- #yaml_inline=

### Files

- ChangeLog
- LICENSE

### Class/Module Index

Quicksearch

- Nanoc3
- Nanoc3::ArrayExtensions
- Nanoc3::CLI
- Nanoc3::CLI::Base
- Nanoc3::CLI::Commands
- Nanoc3::CLI::Commands::Autocompile
- Nanoc3::CLI::Commands::Compile
- Nanoc3::CLI::Commands::CreateItem
- Nanoc3::CLI::Commands::CreateLayout
- Nanoc3::CLI::Commands::CreateSite
- Nanoc3::CLI::Commands::Debug
- Nanoc3::CLI::Commands::Help
- Nanoc3::CLI::Commands::Info
- Nanoc3::CLI::Commands::Update
- Nanoc3::CLI::Commands::View
- Nanoc3::CLI::Logger
- Nanoc3::CodeSnippet
- Nanoc3::Compiler
- Nanoc3::CompilerDSL
- Nanoc3::Context
- Nanoc3::DataSource
- Nanoc3::DataSources
- Nanoc3::DataSources::Delicious
- Nanoc3::DataSources::Filesystem
- Nanoc3::DataSources::FilesystemCombined
- Nanoc3::DataSources::FilesystemCompact
- Nanoc3::DataSources::FilesystemUnified
- Nanoc3::DataSources::FilesystemVerbose
- Nanoc3::DataSources::LastFM
- Nanoc3::DataSources::Twitter
- Nanoc3::DependencyTracker
- Nanoc3::DirectedGraph
- Nanoc3::Errors
- Nanoc3::Errors::CannotDetermineFilter
- Nanoc3::Errors::CannotLayoutBinaryItem
- Nanoc3::Errors::CannotUseBinaryFilter
- Nanoc3::Errors::CannotUseTextualFilter
- Nanoc3::Errors::DataNotYetAvailable
- Nanoc3::Errors::Generic
- Nanoc3::Errors::NoMatchingCompilationRuleFound
- Nanoc3::Errors::NoMatchingRoutingRuleFound
- Nanoc3::Errors::NoRulesFileFound
- Nanoc3::Errors::RecursiveCompilation
- Nanoc3::Errors::UnknownDataSource
- Nanoc3::Errors::UnknownFilter
- Nanoc3::Errors::UnknownLayout
- Nanoc3::Errors::UnmetDependency
- Nanoc3::Extra
- Nanoc3::Extra::AutoCompiler
- Nanoc3::Extra::CHiCk
- Nanoc3::Extra::CHiCk::CacheController
- Nanoc3::Extra::CHiCk::Client
- Nanoc3::Extra::CHiCk::RackClient
- Nanoc3::Extra::Context
- Nanoc3::Extra::Deployers
- Nanoc3::Extra::Deployers::Rsync
- Nanoc3::Extra::FileProxy
- Nanoc3::Extra::TimeExtensions
- Nanoc3::Extra::VCS
- Nanoc3::Extra::VCSes
- Nanoc3::Extra::VCSes::Bazaar
- Nanoc3::Extra::VCSes::Dummy
- Nanoc3::Extra::VCSes::Git
- Nanoc3::Extra::VCSes::Mercurial
- Nanoc3::Extra::VCSes::Subversion
- Nanoc3::Extra::Validators
- Nanoc3::Extra::Validators::Links
- Nanoc3::Extra::Validators::Links::EachPairEnumerator
- Nanoc3::Extra::Validators::W3C
- Nanoc3::Filter
- Nanoc3::Filters
- Nanoc3::Filters::BlueCloth
- Nanoc3::Filters::CodeRay
- Nanoc3::Filters::ColorizeSyntax
- Nanoc3::Filters::ERB
- Nanoc3::Filters::Erubis
- Nanoc3::Filters::Haml
- Nanoc3::Filters::Kramdown
- Nanoc3::Filters::Less
- Nanoc3::Filters::Markaby
- Nanoc3::Filters::Maruku
- Nanoc3::Filters::RDiscount
- Nanoc3::Filters::RDoc
- Nanoc3::Filters::Rainpress
- Nanoc3::Filters::RedCloth
- Nanoc3::Filters::RelativizePaths
- Nanoc3::Filters::RubyPants
- Nanoc3::Filters::Sass
- Nanoc3::HashExtensions
- Nanoc3::Helpers
- Nanoc3::Helpers::Blogging
- Nanoc3::Helpers::Breadcrumbs
- Nanoc3::Helpers::Capturing
- Nanoc3::Helpers::Filtering
- Nanoc3::Helpers::HTMLEscape
- Nanoc3::Helpers::LinkTo
- Nanoc3::Helpers::Rendering
- Nanoc3::Helpers::Tagging
- Nanoc3::Helpers::Text
- Nanoc3::Helpers::XMLSitemap
- Nanoc3::Item
- Nanoc3::ItemRep
- Nanoc3::Layout
- Nanoc3::NotificationCenter
- Nanoc3::PluginRegistry
- Nanoc3::PluginRegistry::PluginMethods
- Nanoc3::Rule
- Nanoc3::RuleContext
- Nanoc3::Site
- Nanoc3::StringExtensions
- Nanoc3::Tasks
- Nanoc3::Tasks::Clean
- Array
- Enumerable
- Hash
- Object
- OrderedHash
- String
- Time

No matching classes.

# OrderedHash

AUTHOR

    jan molic /mig/at/1984/dot/cz/

DESCRIPTION

    Hash with preserved order and some array-like extensions
    Public domain.

THANKS

    Andrew Johnson for his suggestions and fixes of Hash[],
    merge, to_a, inspect and shift

### Attributes

order[RW]
to_yaml_style[RW]

### Public Class Methods

[](*args) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 16
    def [] *args
      hsh = OrderedHash.new
      if Hash === args[0]
        hsh.replace args[0]
      elsif (args.size % 2) != 0
        raise ArgumentError, "odd number of elements for Hash"
      else
        0.step(args.size - 1, 2) do |a|
          b = a + 1
          hsh[args[a]] = args[b]
        end
      end
      hsh
    end

new(*a, &b) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 31
    def initialize(*a, &b)
      super
      @order = []
    end

to_yaml(opts = {}) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 177
    def to_yaml opts = {}
      begin
        @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
      rescue
        @to_yaml_style = :inline
        super
      end
    end

### Public Instance Methods

==(hsh2) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 44
    def == hsh2
        return false if @order != hsh2.order
        super hsh2
    end

[]=(a,b) click to toggle source
Alias for: store
__class__() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 160
    def __class__
      OrderedHash
    end

class() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 157
    def class
      Hash
    end

clear() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 48
    def clear
        @order = []
        super
    end

delete(key) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 52
    def delete key
        @order.delete key
        super
    end

delete_if() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 69
    def delete_if
        @order.clone.each { |k|
            delete k if yield(k)
        }
        self
    end

each() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 64
    def each
        @order.each { |k| yield k,self[k] }
        self
    end

Also aliased as: each_pair
each_key() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 56
    def each_key
        @order.each { |k| yield k }
        self
    end

each_pair() click to toggle source
Alias for: each
each_value() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 60
    def each_value
        @order.each { |k| yield self[k] }
        self
    end

each_with_index() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 192
    def each_with_index
      @order.each_with_index { |k, index| yield k, self[k], index }
      self
    end

first() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 83
    def first
      {@order.first => self[@order.first]}
    end

inspect() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 139
    def inspect
        ary = []
        each {|k,v| ary << k.inspect + "=>" + v.inspect}
        '{' + ary.join(", ") + '}'
    end

invert() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 89
    def invert
        hsh2 = Hash.new
        @order.each { |k| hsh2[self[k]] = k }
        hsh2
    end

keys() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 80
    def keys
        @order
    end

last() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 86
    def last
      {@order.last => self[@order.last]}
    end

merge(hsh2) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 149
    def merge hsh2
        self.dup update(hsh2)
    end

merge!(hsh2) click to toggle source
Alias for: update
orig_store(a,b) click to toggle source
Alias for: store
pop() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 127
    def pop
        key = @order.last
        key ? [key,delete(key)] : nil
    end

push(k,v) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 118
    def push k,v
        unless self.include? k
            @order.push k
            orig_store(k,v)
            true
        else
            false
        end
    end

reject(&block) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 94
    def reject &block
        self.dup.delete_if &block
    end

reject!(&block) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 97
    def reject! &block
        hsh2 = reject &block
        self == hsh2 ? nil : hsh2
    end

replace(hsh2) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 101
    def replace hsh2
        @order = hsh2.keys
        super hsh2
    end

select() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 152
    def select
        ary = []
        each { |k,v| ary << [k,v] if yield k,v }
        ary
    end

shift() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 105
    def shift
        key = @order.first
        key ? [key,delete(key)] : super
    end

store(a,b) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 39
    def store a,b
        @order.push a unless has_key? a
        super a,b
    end

Also aliased as: orig_store, []=
store_only(a,b) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 35
    def store_only a,b
        store a,b
    end

to_a() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 131
    def to_a
        ary = []
        each { |k,v| ary << [k,v] }
        ary
    end

to_s() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 136
    def to_s
        self.to_a.to_s
    end

unshift(k,v) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 109
    def unshift k,v
        unless self.include? k
            @order.unshift k
            orig_store(k,v)
            true
        else
            false
        end
    end

update(hsh2) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 144
    def update hsh2
        hsh2.each { |k,v| self[k] = v }
        self
    end

Also aliased as: merge!
values() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 75
    def values
        ary = []
        @order.each { |k| ary.push self[k] }
        ary
    end

yaml_inline!() click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 190
    def yaml_inline!() self.yaml_inline = true end

yaml_inline=(bool) click to toggle source

    # File lib/nanoc3/base/ordered_hash.rb, line 165
    def yaml_inline= bool
      if respond_to?("to_yaml_style")
        self.to_yaml_style = :inline
      else
        unless defined? @__yaml_inline_meth
          @__yaml_inline_meth =
            lambda {|opts|
              YAML::quick_emit(object_id, opts) {|emitter|
                emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
              }
            }
          class << self
            def to_yaml opts = {}
              begin
                @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
              rescue
                @to_yaml_style = :inline
                super
              end
            end
          end
        end
      end
      @__yaml_inline = bool
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.