Skip to content

Commit

Permalink
clear_at and clear_at? are deprecated. Use unset_at and unset_at? ins…
Browse files Browse the repository at this point in the history
…tead.
  • Loading branch information
kenn committed Feb 28, 2012
1 parent af515cc commit 620937a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -20,7 +20,7 @@ Install
Usage
-----

Set and clear bits:
Set and unset bits:

```ruby
b = Bitwise.new("\x00")
Expand All @@ -34,10 +34,13 @@ b.set_at(4)
b.bits
=> "01001000"

b.clear_at(1)
b.unset_at(1)

b.bits
=> "00001000"

b.set_at?(4)
=> true
```

String-based accessor:
Expand Down
14 changes: 12 additions & 2 deletions lib/bitwise.rb
Expand Up @@ -19,19 +19,29 @@ def set_at(index)
@value.setbyte(@div, @byte | bitmask)
end

def clear_at(index)
def unset_at(index)
get_byte(index)
@value.setbyte(@div, @byte & ~bitmask)
end

def clear_at(index)
warn 'Bitwise#clear_at is deprecated. Use Bitwise#unset_at instead.'
unset_at(index)
end

def set_at?(index)
get_bit(index) == 1
end

def clear_at?(index)
def unset_at?(index)
get_bit(index) == 0
end

def clear_at?(index)
warn 'Bitwise#clear_at? is deprecated. Use Bitwise#unset_at? instead.'
unset_at?(index)
end

def get_bit(index)
get_byte(index)
(@byte & bitmask) > 0 ? 1 : 0
Expand Down
4 changes: 2 additions & 2 deletions spec/bitwise_spec.rb
Expand Up @@ -12,15 +12,15 @@
@bitwise.raw.encoding.should == Encoding::BINARY
end

it "set and clear" do
it "set and unset" do
@bitwise.bits.should == '00000000'

@bitwise.set_at(1)
@bitwise.set_at(4)
@bitwise.bits.should == '01001000'
@bitwise.cardinality.should == 2

@bitwise.clear_at(1)
@bitwise.unset_at(1)
@bitwise.bits.should == '00001000'
@bitwise.cardinality.should == 1

Expand Down

0 comments on commit 620937a

Please sign in to comment.