From: "Eregon (Benoit Daloze)" Date: 2012-10-28T06:45:07+09:00 Subject: [ruby-core:48502] [ruby-trunk - Feature #3222] Can bignums have singleton class & methods? Issue #3222 has been updated by Eregon (Benoit Daloze). ko1 (Koichi Sasada) wrote: > (2012/10/27 22:20), Eregon (Benoit Daloze) wrote: > > It makes sense to me to have them frozen, but I think we would need to freeze all Numeric instances as well for consistency (currently, Fixnum, Rational and Complex can have ivars). > > Float is frozen because of introduction of Flonum technique. > I think Fixnum should be also frozen. > > I'm not sure about complex and rational. I don't think inconsistent > because we can make your own Numeric classes (and we can't force it > frozen). But I have no objection. I think it's to custom Numeric subclasses authors to chose for them to be frozen or not, but I think having all core Numeric subclasses instances frozen would make sense. These are anyway already immutable for their functionality, it seems weird to allow some kind of changes like singleton methods and ivars. But you're right, as Rational and Complex are composed of other types, it seems less important for them to follow Fixnum/Bignum/Float (and BigDecimal?). However, freezing Fixnum would likely affect more code (notably because their instances are supposed to be unique in MRI). So theoretically numeric primitives should always be totally immutable, but on the other hand I like having them as full-fledged objects (that is I dislike having an object/type distinction) and freezing them reduces freedom. > class Fixnum; def fib; @fib ||= (self-1).fib + (self-2).fib; end; end; [0,1].each { |i| i.instance_eval { @fib = i } } => [0, 1] > 42.fib => 267914296 This is fun but evil ... ---------------------------------------- Feature #3222: Can bignums have singleton class & methods? https://bugs.ruby-lang.org/issues/3222#change-31833 Author: marcandre (Marc-Andre Lafortune) Status: Assigned Priority: Normal Assignee: ko1 (Koichi Sasada) Category: core Target version: 2.0.0 =begin 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 =end -- http://bugs.ruby-lang.org/