characterizable
“Cognitive mechanisms, on this view, take mathematically characterizable inputs to deliver mathematically characterizable outputs, and qua computational devices, that is all.”
What is a BetterHash?
It just brings some Ruby 1.9 behavior to Ruby 1.8 hashes… (from webonrails.com/2009/02/06/ruby-191-hash)
RUBY_VERSION => 1.8.6
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]
RUBY_VERSION => 1.9.1
>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.select{|k,v| k == :c }
=> {:c=>3}
I hope these two tests show the value of BetterHash in the context of this gem…
should "not be annoying to work with characteristics on a particular instance" do
a = SimpleAutomobile.new
a.make = 'Nissan'
assert_same_contents [:make], a.characteristics.effective.keys
assert_same_contents [:make], a.characteristics.effective.select { true }.keys
end
should "not be annoying to work with characteristics hashes on a class level"
assert_same_contents [:make, :model, :variant], SimpleAutomobile.characteristics.keys
assert_same_contents [:make, :model, :variant], SimpleAutomobile.characteristics.select { true }.keys
end
If you didn’t have BetterHash, you wouldn’t be able to call #keys
because in Ruby 1.8 Hash#select
(and #reject
) gives you back an Array.
What is a Snapshot?
It’s a hash of the characteristics at a particular time…
should "keep snapshots separately" do
my_car = Automobile.new
my_car.make = 'Ford'
my_car.model_year = 1999
snapshot = my_car.characteristics
assert_same_contents [:make, :model_year], snapshot.effective.keys
my_car.make = nil
assert_same_contents [], my_car.characteristics.effective.keys # up to date!
assert_same_contents [:make, :model_year], snapshot.effective.keys # frozen in time!
end
There are two important points here:
-
If you call
my_car.characteristics
, you will always get the most recent snapshot -
If you save the output to a variable like
snapshot
, that snapshot won’t change
Note on Patches/Pull Requests
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright
Copyright © 2010 Seamus Abshere. See LICENSE for details.