Skip to content

Commit

Permalink
added subscripting to fixnums
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Gaynor committed Apr 4, 2013
1 parent 463ab56 commit d260940
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
10 changes: 0 additions & 10 deletions spec/tags/core/fixnum/element_reference_tags.txt
@@ -1,12 +1,2 @@
fails:Fixnum#[] returns 1 if the nth bit is set
fails:Fixnum#[] returns 0 if the nth bit is not set
fails:Fixnum#[] returns 0 if the nth bit is greater than the most significant bit
fails:Fixnum#[] returns 0 when passed a negative argument
fails:Fixnum#[] calls #to_int to convert the argument to an Integer and returns 1 if the nth bit is set
fails:Fixnum#[] calls #to_int to convert the argument to an Integer and returns 0 if the nth bit is set
fails:Fixnum#[] accepts a Float argument and returns 0 if the bit at the truncated value is not set
fails:Fixnum#[] accepts a Float argument and returns 1 if the bit at the truncated value is set
fails:Fixnum#[] raises a TypeError when passed a String
fails:Fixnum#[] raises a TypeError when #to_int does not return an Integer
fails:Fixnum#[] calls #to_int to coerce a String to a Bignum and returns 0
fails:Fixnum#[] returns 0 when passed a Float in the range of a Bignum
6 changes: 6 additions & 0 deletions topaz/objects/intobject.py
Expand Up @@ -294,3 +294,9 @@ def method_chr(self, space):
raise space.error(space.w_RangeError, "%d out of char range" % self.intvalue)
else:
return space.newstr_fromstr(chr(self.intvalue))

@classdef.method("[]", idx="int")
def method_subscript(self, space, idx):
if not 0 <= idx < LONG_BIT:
return space.newint(0)
return space.newint(int(bool(self.intvalue & (1 << idx))))

0 comments on commit d260940

Please sign in to comment.