Project

General

Profile

Actions

Feature #19450

closed

Is there an official way to set a class name without setting a constant?

Added by ioquatix (Samuel Williams) about 1 year ago. Updated about 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:112494]

Description

This is the best I could come up with:

klass = Class.new

Object.const_set("Klass", klass)
Object.send(:remove_const, "Klass")

puts klass.new
# => #<Klass:0x0000000100a9d688>

Can we do better?

What about something like:

Class.new(name: "Klass")

or

Class.new do
  def self.name
    "Klass"
  end
end

etc


Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #19521: Support for `Module#name=` and `Class#name=`.ClosedActions
Related to Ruby master - Feature #19520: Support for `Module.new(name)` and `Class.new(superclass, name)`.RejectedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0