Integer#[]

Integer#[]を使うと、ビットパターンが得られます。

10.downto(0) do |k|
  print 1024[k]
end
#=> 10000000000

きのう定義した(機能定義じゃなくて、昨日定義ね)Method#whereを使って、[]がどこで定義されているかを調べてみましょう。

class Method
  def where
    to_s =~ /#<Method: (\w+)(\((\w+)\))?#/
    Object.const_get($3 ? $3 : $1)
  end
end

p 1024.method(:[]).where      #=> Fixnum
p (2**32).method(:[]).where   #=> Bignum

1024の場合はFixnumで、2**32の場合はBignumでした。ちなみに両方ともIntegerのサブクラスです。どうしてわかるかというと、FixnumさんやBignumくんに聞けばわかります。

p Fixnum.superclass #=> Integer
p Bignum.superclass #=> Integer

ね。