Reputation: 3029
I've started playing around with the Enumerable::Lazy functionality in Ruby 2.0 and it looks really useful.
I have an Enumerable collection that pages through a remote data source. Because of this, I would like it to be a "lazy" collection. However, I don't want to have to instruct every user of my class to call .lazy every time they want to use any of the Enumerable methods on it. Instead it would be nice to include a hypothetical LazyEnumerable module and have all the enumerable methods be lazy by default.
Does anyone have any ideas on a clean way to accomplish this? Thanks!
Upvotes: 3
Views: 188
Reputation: 3029
I had an idea and thought I would take a stab at this:
module LazyEnumerable
include Enumerable
def self.make_lazy(*methods)
methods.each do |method|
define_method method do |*args, &block|
lazy.public_send(method, *args, &block)
end
end
end
make_lazy *(Enumerable.public_instance_methods - [:lazy])
end
Curious to know if there is a more robust way to do this.
Upvotes: 2