T
Thomas Hafner
Hello,
how about the following implementation for lazy lookups?
def define_lookup
h = lambda{ |k|
h = {}
# ... fill h (expensive!) ...
h[k]
}
self.class.class_eval{ define_methodlookup){ |k| h[k] } }
end
Once ``define_lookup'' has been called, the method ``lookup'' is
defined and will act in a lazy manner. Just call ``lookup'' as often
as you need, but only for the first time the expensive operation (e.g.
dealing with files) will be performed. Due to duck typing no condition
needs to be checked: for the first call ``h[k]'' implies calling the
lambda, for the next times ``h[k]'' implies accessing the hash.
(Tried with Ruby 1.8.6.)
Regards
Thomas
how about the following implementation for lazy lookups?
def define_lookup
h = lambda{ |k|
h = {}
# ... fill h (expensive!) ...
h[k]
}
self.class.class_eval{ define_methodlookup){ |k| h[k] } }
end
Once ``define_lookup'' has been called, the method ``lookup'' is
defined and will act in a lazy manner. Just call ``lookup'' as often
as you need, but only for the first time the expensive operation (e.g.
dealing with files) will be performed. Due to duck typing no condition
needs to be checked: for the first call ``h[k]'' implies calling the
lambda, for the next times ``h[k]'' implies accessing the hash.
(Tried with Ruby 1.8.6.)
Regards
Thomas