Project

General

Profile

Actions

Bug #10276

closed

nil/true/false に singleton メソッドを定義できてしまう

Added by znz (Kazuhiro NISHIYAMA) about 10 years ago. Updated about 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.0dev (2014-09-21 trunk 47672) [x86_64-linux]
[ruby-dev:48569]

Description

nil/true/false が frozen object なのに singleton メソッドを定義できてしまいます。

% cat /tmp/f.rb
def d(o)
  p o.frozen?
  def o.m; end
rescue
  p [o, $!]
end
d(:sym)
d(1)
d(nil)
d(true)
d(false)
d(Object.new.freeze)
% ruby -v /tmp/f.rb
ruby 2.2.0dev (2014-09-21 trunk 47672) [x86_64-linux]
true
[:sym, #<TypeError: can't define singleton>]
true
[1, #<TypeError: can't define singleton>]
true
true
true
true
[#<Object:0x00000002706730>, #<RuntimeError: can't modify frozen object>]

Updated by znz (Kazuhiro NISHIYAMA) about 10 years ago

frozen なオブジェクトに特異クラスが作成できるかどうかも統一されていないようですが、そういうものなのでしょうか?

>> RUBY_DESCRIPTION
=> "ruby 2.2.0dev (2014-09-25 trunk 47711) [x86_64-linux]"
>> class << :sym; end
TypeError: can't define singleton
        from (irb):2
        from /home/kazu/.rbenv/versions/git/bin/irb:11:in `<main>'
>> class << 1; end
TypeError: can't define singleton
        from (irb):3
        from /home/kazu/.rbenv/versions/git/bin/irb:11:in `<main>'
>> class << nil; end
=> nil
>> class << Object.new.freeze; end
=> nil
>>

Updated by akr (Akira Tanaka) about 10 years ago

  • Status changed from Open to Feedback

開発者会議のときに、奇妙だがそういうものとしようということになった覚えがあります。

Updated by akr (Akira Tanaka) about 8 years ago

  • Status changed from Feedback to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0