https://github.com/krisleech/persistent-hash
Simple example used to teach several Ruby coding techniques
https://github.com/krisleech/persistent-hash
Last synced: about 1 year ago
JSON representation
Simple example used to teach several Ruby coding techniques
- Host: GitHub
- URL: https://github.com/krisleech/persistent-hash
- Owner: krisleech
- Created: 2010-02-09T14:24:22.000Z (over 16 years ago)
- Default Branch: master
- Last Pushed: 2010-02-09T14:39:57.000Z (over 16 years ago)
- Last Synced: 2025-03-17T04:44:46.674Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 74.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
Awesome Lists containing this project
README
A Persistent Hash
A Hash is a very powerful class in Ruby and can be quickly turned in to a rudimentary database with full CRUD and search capabilities.
This simple class example demonstrates a number of Ruby techniques including:
* read/write files
* YAML
* inheritance
* writing classes
* super
* KISS
* CoC (Convention Over Configuration)
* blocks
* method chaining
== Example Usage ==
require 'phash'
class Products < Database
end
# Create a new database, or load an existing database
products = Products.new
# FIND A RECORD
products[:blue_widget]
# => Hash
# ITERATE THROUGH ALL RECORDS
products.each do | name, attributes |
puts name
puts attributes.inspect
end
# DELETE A RECORD
products.delete(:green_widget)
# CREATE/UPDATE A RECORD
products[:red_widget] = { :name => 'Red Widget', :size => 'large', :weight => 50, :color => 'red' }
products.save
# QUERY THE DATABASE
products.select { |p| p[:weight] > 60 }.each do | product |
product[:size] = 'large'
end
# Note the key can be anything so long as it is unique, for example a SKU code.
== This is used as a teaching example in the Impartica IT Training RubyOnRails Course ==