Skip to content

Commit 2ae5283

Browse files
committed
Merge branch 'questionmarkexclamationpoint-master'
2 parents 26a5cb0 + 365e144 commit 2ae5283

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/bitarray-array.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ def [](position)
2626
end
2727

2828
# Iterate over each bit
29-
def each(&block)
29+
def each
30+
return to_enum unless block_given?
3031
@size.times { |position| yield self[position] }
3132
end
3233

3334
# Returns the field as a string like "0101010100111100," etc.
3435
def to_s
3536
@field.collect{|ea| ("%0#{ELEMENT_WIDTH}b" % ea).reverse}.join[0..@size-1]
3637
end
38+
39+
# Iterate over each byte
40+
def each_byte
41+
return to_enum(:each_byte) unless block_given?
42+
@field.each { |byte| yield byte }
43+
end
3744

3845
# Returns the total number of bits that are set
3946
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)

lib/bitarray.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def [](position)
2626
end
2727

2828
# Iterate over each bit
29-
def each(&block)
29+
def each
30+
return to_enum(:each) unless block_given?
3031
@size.times { |position| yield self[position] }
3132
end
3233

@@ -38,6 +39,12 @@ def to_s
3839
@field.bytes.collect { |ea| ("%08b" % ea) }.join[0, @size]
3940
end
4041
end
42+
43+
# Iterates over each byte
44+
def each_byte
45+
return to_enum(:each_byte) unless block_given?
46+
@field.bytes.each{ |byte| yield byte }
47+
end
4148

4249
# Returns the total number of bits that are set
4350
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)

0 commit comments

Comments
 (0)