From: Logan Capaldo Date: 2006-03-27T08:45:26+09:00 Subject: Re: Data Structure for n-dimension values with defaults On Mar 26, 2006, at 6:28 PM, Phrogz wrote: > Help! I can't figure out how to store a rather particular set of data. > > An object in my system has properties. Each property has a value. > However, the value of each property can change based on a number of > "build axes". There are an arbitrary number of orthogonal build axes, > none of which is more important than another. (However, if it's > needed, > I am happy to impose a particular ordering on them.) > > Each build axis has a number of 'notches', or discrete values along > the > axis. In addition to all the notches, there is one special notch named > "Master". A value set in this notch is a fallback, default value if > other notches in the axis have no values. > > So, for example, assume I'm talking about a Text object. One of it's > properties is "textString". > The system has two build axes: "Language" and "Rating". > > The "Language" axis has "French", "English", "German", and "Jibbrish" > notches in addition to the Master notch. The "Rating" axis has "G", > "PG", "R", and "X" values. > > Here's a monospace-font diagram of the values I want to store: > > -------------------Language------------------------- > > Master English French German Jibbrish > R Master (yell) - - Ach! - > A G - Wow! Oh! - - > T PG - Holy Crap! Merde! - - > I R - Holy Shit! Mon Dieu! - - > N X $!@* - - XXX - > G > > With the above, the textString property should have a value of "Ach!" > if the Language axis is set to German and the Rating axis is set to G, > PG, or R. > > If the Rating is set to "X", then the textString should be set to > "$!@*" regardless of the Language axis setting...except for if the > Language is set to German, in which case it should be "XXX" > > If the Language is set to "Jibbrish", then the value is "(yell)", > except when the Rating is "X". > > If the Rating is set to "PG" and the Language is set to "English", > then > the textString value should be set to "Holy Crap!". And so on for > other > values that are explicitly set for all axis combinations. > > > Hopefully that example is clear enough to show that explicit values > win > over master values. > > So, how the heck would you store this sort of information? I mostly > don't even care about speed. > > An array of hashes? Actually a hash of structs would probably be better Row = Struct.new(:master, :english, :french, :german, :gibberish) table = { :G => Row.new('(yell)', nil, nil, 'Ach!', nil), :PG => Row.new(nil, 'Wow!', 'Oh!', nil, nil), :R => Row.new(nil, 'Holy Shit!', 'Mon Dieu!', nil, nil), :X => Row.new('$!@*', nil, nil, 'XXX', nil) } def table.get(rating, language) res = self[rating].send(language) if res.nil? self[rating].master else res end end irb(main):040:0> table.get(:X, :german) => "XXX" irb(main):041:0> table.get(:X, :english) => "$!@*"