Help us understand the problem. What is going on with this article?

シングルトンクラスのインスタンスは作れない

More than 1 year has passed since last update.

Rubyならできると思ったんだけど。

class Sample; end

class << Sample
  def initialize; end
end

# Sample.singleton_class.new でも同じ
(class << Sample; self; end).new

# これでももちろんだめ
Sample.singleton_class.class_eval do |obj|
  obj.new
end

can't create instance of singleton class (TypeError) でエラー。頑張ったら相応のことはいけそうな気がするけど。

Note) ちなみにシングルトンクラスのサブクラスも作れない。仮に、サブクラスを作れるとしたら、そのクラスがシングルトンクラスかどうか見分けがつかず、インスタンスを作成することができてしまい、矛盾を発生してしまうからかも。

class Sample; end
Class.new(Sample.singleton_class) # can't make subclass of singleton class (TypeError)
knknkn1162
今後は http://cstmize.hatenablog.jp/ で更新すると思いますm(_ _)m
http://cstmize.hatenablog.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした