Nov 262009
 

I received an email from sensei on Monday (sent to every student) – if you’ll be here tonight, bring your fighting gear. Uh-oh. Her sensei was there and taught a few classes, including the adults. The entire class turned out to be pretty much a blur of drills. I’ll be able to dredge them out of my body memory, I think.
A drill about basics – chain two basics together, using shifting. Then up to six basics together, by series of two.
A couple of kicking drills. A few fighting drills – these are important. shift, step back, kick, finish, disengage.
Come in and jam, elbow, finish.
I am obfuscating these on purpose.
This was a harder training session than I’ve experienced in a while, as evidenced by the appearance after 40-some hours of a slight tightness in my upper abs and shoulder muscles.. Oh, and the fact that my lower legs were one solid body, instead of having a freely moving calf muscle.

Of course, I was supposed to the student in the best shape, so I wonder how the others did feel. This wasn’t particularly hard on my mind, just a little bit past my comfort zone.. I do feel like I am more aware that I can stand more than I think. Was my willpower muscle flexed? It seems like it was.

Nov 062009
 

An auto-vivifying hash is a hash that lets you create sub-hashes automatically. This means that the following code becomes possible:

def cnh # silly name "create nested hash"
  Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
end
my_hash = cnh
my_hash[1][2][3] = 4
my_hash # => { 1 => { 2 => { 3 =>4 } } }

This is useful because it reduces the amount of logic in the code. No more “If sub-hash doesn’t exist, create it!”

Courtesy of Robert Klemme on www.ruby-forum.com

_____

Update:

Facets has a way of doing an auto-vivifying hash, too! Thanks to Stackoverflow.

# Monkey patching Hash class:
# File lib/core/facets/hash/autonew.rb, line 19
  def self.autonew(*args)
    leet = lambda { |hsh, key| hsh[key] = new( &leet ) }
    new(*args,&leet)
  end

The standard new method for Hash accepts a block. This block is called in the event of trying to access a key in the Hash which does not exist. The block is passed the Hash itself and the key that was requested (the two parameters) and should return the value that should be returned for the requested key.

You will notice that the leet lambda does 2 things. It returns a new Hash with leet itself as the block for handling defaults. This is the behaviour which allows autonew to work for Hashes of arbitrary depth. It also assigns this new Hash to hsh[key] so that next time you request the same key you will get the existing Hash rather than a new one being created.

Sweet deal!