Skip to content

Commit

Permalink
Adding Fixnum#[] (bits as element references)
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterbr authored and meh committed Feb 5, 2014
1 parent d21fba0 commit 85220f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions opal/corelib/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ def >>(count)
`self >> #{count.to_int}`
end

def [](bit)
bit = Opal.coerce_to! bit, Integer, :to_int
min = -(2**30)
max = (2**30) - 1

`(#{bit} < #{min} || #{bit} > #{max}) ? 0 : (self >> #{bit}) % 2`
end

def +@
`+self`
end
Expand Down
1 change: 1 addition & 0 deletions spec/opal/rubyspecs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ core/fixnum/abs_spec
core/fixnum/case_compare_spec
core/fixnum/comparison_spec
core/fixnum/equal_value_spec
core/fixnum/element_reference_spec
core/fixnum/even_spec
core/fixnum/gt_spec
core/fixnum/gte_spec
Expand Down

0 comments on commit 85220f3

Please sign in to comment.