Skip to content
Permalink
Browse files

added subscripting to fixnums

  • Loading branch information
Alex Gaynor
Alex Gaynor committed Apr 4, 2013
1 parent 463ab56 commit d26094086745e01295b305a6e8db4ec548346915
Showing with 6 additions and 10 deletions.
  1. +0 −10 spec/tags/core/fixnum/element_reference_tags.txt
  2. +6 −0 topaz/objects/intobject.py
@@ -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
@@ -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.