Skip to content

Commit 85220f3

Browse files
chesterbrmeh
authored andcommittedFeb 5, 2014
Adding Fixnum#[] (bits as element references)
http://www.ruby-doc.org/core-1.9.3/Fixnum.html#method-i-5B-5D
1 parent d21fba0 commit 85220f3

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed
 

‎opal/corelib/numeric.rb

+8
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ def >>(count)
195195
`self >> #{count.to_int}`
196196
end
197197

198+
def [](bit)
199+
bit = Opal.coerce_to! bit, Integer, :to_int
200+
min = -(2**30)
201+
max = (2**30) - 1
202+
203+
`(#{bit} < #{min} || #{bit} > #{max}) ? 0 : (self >> #{bit}) % 2`
204+
end
205+
198206
def +@
199207
`+self`
200208
end

‎spec/opal/rubyspecs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ core/fixnum/abs_spec
1818
core/fixnum/case_compare_spec
1919
core/fixnum/comparison_spec
2020
core/fixnum/equal_value_spec
21+
core/fixnum/element_reference_spec
2122
core/fixnum/even_spec
2223
core/fixnum/gt_spec
2324
core/fixnum/gte_spec

0 commit comments

Comments
 (0)