Skip to content

Instantly share code, notes, and snippets.

@dbussink
Created November 3, 2008 19:13
Show Gist options
  • Save dbussink/21952 to your computer and use it in GitHub Desktop.
Save dbussink/21952 to your computer and use it in GitHub Desktop.
diff --git a/lib/extlib/lazy_array.rb b/lib/extlib/lazy_array.rb
index 091559d..12e3922 100644
--- a/lib/extlib/lazy_array.rb
+++ b/lib/extlib/lazy_array.rb
@@ -4,10 +4,9 @@ class LazyArray # borrowed partially from StrokeDB
include Enumerable
# these methods should return self or nil
- RETURN_SELF = [ :<<, :clear, :concat, :collect!, :delete_if,
+ RETURN_SELF = [ :clear, :concat, :collect!, :delete_if,
:each, :each_index, :each_with_index, :freeze, :insert, :map!,
- :push, :replace, :reject!, :reverse!, :reverse_each, :sort!,
- :unshift ]
+ :replace, :reject!, :reverse!, :reverse_each, :sort! ]
RETURN_SELF.each do |method|
class_eval <<-EOS, __FILE__, __LINE__
@@ -70,11 +69,48 @@ class LazyArray # borrowed partially from StrokeDB
@load_with_proc
end
+ def push(*args)
+ if loaded?
+ super
+ else
+ @tail.push(*args)
+ end
+ self
+ end
+
+ def <<(arg)
+ if loaded?
+ super
+ else
+ @tail << arg
+ end
+ self
+ end
+
+ def include?(arg)
+ if loaded?
+ super
+ else
+ @tail.include?(arg) || @head.include?(arg) || super
+ end
+ end
+
+ def unshift(*args)
+ if loaded?
+ super
+ else
+ @head.unshift(*args)
+ end
+ self
+ end
+
private
def initialize(*args, &block)
@loaded = false
@load_with_proc = proc { |v| v }
+ @head = []
+ @tail = []
@array = Array.new(*args, &block)
end
@@ -88,6 +124,9 @@ class LazyArray # borrowed partially from StrokeDB
return if loaded?
mark_loaded
@load_with_proc[self]
+ @array = @head + @array + @tail
+ @head.clear
+ @tail.clear
end
def mark_loaded
diff --git a/lib/extlib/simple_set.rb b/lib/extlib/simple_set.rb
index 3d3b181..8567f2e 100644
--- a/lib/extlib/simple_set.rb
+++ b/lib/extlib/simple_set.rb
@@ -10,7 +10,7 @@ module Extlib
#
# @return <Array> The array the Set was initialized with
def initialize(arr = [])
- arr.each {|x| self[x] = true}
+ Array(arr).each {|x| self[x] = true}
end
# @param value<Object> Value to add to set.
diff --git a/spec/lazy_array_spec.rb b/spec/lazy_array_spec.rb
index f576527..3fc1687 100644
--- a/spec/lazy_array_spec.rb
+++ b/spec/lazy_array_spec.rb
@@ -495,7 +495,6 @@ describe LazyArray do
end
describe '#push' do
- it_should_return_self(:push)
it 'should return self' do
@lazy_array.push(@steve).object_id.should == @lazy_array.object_id
@@ -505,6 +504,12 @@ describe LazyArray do
@lazy_array.push(@steve)
@lazy_array.should == [ @nancy, @bessie, @steve ]
end
+
+ it 'should lazy push' do
+ @lazy_array.push(@steve)
+ @lazy_array.loaded?.should be_false
+ end
+
end
it 'should provide #reject' do
@@ -862,7 +867,6 @@ describe LazyArray do
end
describe '#unshift' do
- it_should_return_self(:unshift)
it 'should return self' do
@lazy_array.unshift(@steve).object_id.should == @lazy_array.object_id
@@ -872,6 +876,12 @@ describe LazyArray do
@lazy_array.unshift(@steve)
@lazy_array.should == [ @steve, @nancy, @bessie ]
end
+
+ it 'should lazy unshift' do
+ @lazy_array.unshift(@steve)
+ @lazy_array.loaded?.should be_false
+ end
+
end
it 'should provide #values_at' do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment