File tree Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -26,14 +26,21 @@ def [](position)
26
26
end
27
27
28
28
# Iterate over each bit
29
- def each ( &block )
29
+ def each
30
+ return to_enum unless block_given?
30
31
@size . times { |position | yield self [ position ] }
31
32
end
32
33
33
34
# Returns the field as a string like "0101010100111100," etc.
34
35
def to_s
35
36
@field . collect { |ea | ( "%0#{ ELEMENT_WIDTH } b" % ea ) . reverse } . join [ 0 ..@size -1 ]
36
37
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
37
44
38
45
# Returns the total number of bits that are set
39
46
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
Original file line number Diff line number Diff line change @@ -26,7 +26,8 @@ def [](position)
26
26
end
27
27
28
28
# Iterate over each bit
29
- def each ( &block )
29
+ def each
30
+ return to_enum ( :each ) unless block_given?
30
31
@size . times { |position | yield self [ position ] }
31
32
end
32
33
@@ -38,6 +39,12 @@ def to_s
38
39
@field . bytes . collect { |ea | ( "%08b" % ea ) } . join [ 0 , @size ]
39
40
end
40
41
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
41
48
42
49
# Returns the total number of bits that are set
43
50
# (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
You can’t perform that action at this time.
0 commit comments