Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Symbols-only Hash for Ruby
Ruby
tree: 7764bb33ae

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
doc
lib
test
.gitignore
README
Rakefile
symboltable.gemspec

README

+++ SymbolTable +++

SymbolTable is a handy little Ruby class that was conceived from the union of
Hash and Symbol. SymbolTable directly extends Hash, but it stores all keys
internally as symbols. Any key that cannot be converted to a Symbol is not
valid.

While this may seem restrictive, it does have the nice side effect of making
all keys slightly more memorable and usable. For example, values may be set
and retrieved using any key that resolves to the same symbol.

    h = Hash.new
    h[:a] = 1
    h[:a]       # => 1
    h['a']      # => nil
    h.a         # => NoMethodError: undefined method `a' for {}:Hash

    require 'symboltable'

    t = SymbolTable.new
    t[:a] = 1
    t[:a]       # => 1
    t['a']      # => 1
    t.a         # => 1
    t.b = 2     # => 2

Also, because SymbolTable subclasses Hash, you get all the nice methods of Hash
for free.

    t.keys      # => [:a, :b]
    t.values    # => [1, 2]

    t.merge!('c' => 3, :d => 4)
    t.keys      # => [:a, :b, :c, :d]
    t.values    # => [1, 2, 3, 4]

    t.each do |k, v|
      puts "#{k} = #{v}"
    end

You get the idea. This kind of data structure is mainly useful for large
configuration objects and the like, where you want to store a bunch of data and
you don't want to have to remember later how you stored it.

== Why?

Mainly just for fun, and because I sometimes miss the automatic and ubiquitous
type conversion you get in PHP.

== Installation

Using RubyGems:

    $ sudo gem install symboltable

From a local copy:

    $ git clone git://github.com/mjijackson/symboltable.git
    $ cd symboltable
    $ rake package && sudo rake install

== License

Copyright 2010 Michael Jackson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Something went wrong with that request. Please try again.