Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an Enumerator for #each_key without a block #2

Merged
merged 1 commit into from
Dec 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/gdbm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,17 @@ def self.each_pair(file)

#Iterates over each key in the _file_.
def self.each_key(file)
keys = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Any reason to not check block_given? at the beginning and return self.keys.each? Seems simpler.-

Edit: haha I don't know what I'm talking about...never mind.

current = self.gdbm_firstkey file
until current.value.nil?
yield current.value
if block_given?
yield current.value
else
keys << current.value
end
current = self.gdbm_nextkey file, current
end
block_given? ? nil : keys.each
end

#Iterates over each value in the _file_.
Expand Down Expand Up @@ -345,8 +351,8 @@ def delete_if
alias :reject! :delete_if

def each_key(&block)
GDBM_FFI.each_key file, &block
self
enumerator = GDBM_FFI.each_key(file, &block)
enumerator || self
end

def each_pair(&block)
Expand Down
4 changes: 4 additions & 0 deletions test/test_gdbm-1.8.7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ def test_each_key
assert_equal(@gdbm, ret)
end

def test_each_key_without_block
assert_kind_of Enumerable::Enumerator, @gdbm.each_key
end

def test_keys
assert_equal([], @gdbm.keys)

Expand Down
4 changes: 4 additions & 0 deletions test/test_gdbm-1.9.1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ def test_each_key
assert_equal(@gdbm, ret)
end

def test_each_key_without_block
assert_kind_of ::Enumerator, @gdbm.each_key
end

def test_keys
assert_equal([], @gdbm.keys)

Expand Down