Feature #3222

Can bignums have singleton class & methods?

Added by Marc-Andre Lafortune almost 2 years ago. Updated 10 months ago.

[ruby-core:29891]
Status:Assigned Start date:04/30/2010
Priority:Normal Due date:
Assignee:Yukihiro Matsumoto % Done:

0%

Category:core
Target version:2.0.0

Description

Fixing up the rubyspecs led me to the following:

  bn = 1 << 100
  class << bn
    def foo
      42
    end
  end
  # => TypeError: can't define singleton method "foo" for Bignum

  bn.define_singleton_method(:foo){42}
  # => TypeError: can't define singleton method "foo" for Bignum

On the other hand...

  module Bar
    def foo
      42
    end
  end
  class << bn
    include Bar
  end
  bn.foo # => 42

If Ruby won't allow singleton methods for Bignum, then shouldn't it disallow access to the singleton class completely?

See also issue #601

Related issues

related to ruby-trunk - Bug #601: an instance of Bignum can have singleton methods Closed 09/25/2008

History

Updated by Shyouhei Urabe almost 2 years ago

Seems like a bug to me.

Updated by Yusuke Endoh almost 2 years ago

  • Assignee set to Yukihiro Matsumoto
  • Target version changed from 1.9.2 to 2.0.0
Hi,

I think this is not a bug.

It is still possible to define method to Bignum by redefining
singleton_method_added first:

  x = (1 << 64)
  def x.singleton_method_added(x)
  end
  def x.foo
    p 1
  end
  x.foo  #=> 1

Matz actually fixed #601, but he was not keen to fix this kind
of issue.  [ruby-dev:36778]
In fact, the above loophole is indicated by matz himself.


I agree that Ruby should disallow access to the singleton class
completely, but I don't know if it is easy or not.
Anyway, we should discuss this matter towards 1.9.3 or later.
I move this ticket to Feature tracker.

-- 
Yusuke Endoh <mame@tsg.ne.jp>

Updated by Shyouhei Urabe over 1 year ago

  • Status changed from Open to Assigned

Also available in: Atom PDF