Project

General

Profile

Actions

Feature #16703

open

Namespace parameter for `Module#name`

Added by sawa (Tsuyoshi Sawada) over 4 years ago. Updated over 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:97579]

Description

I often see code that intends to remove a portion of the namespace from a module/class name like this:

class A; class B class C; end end end
A::B::C.name.delete_prefix("A::") # => "B::C"
A::B::C.name.delete_prefix("A::B::") # => "C"

I think a large portion of the use cases of the method String#delete_prefix belongs to such use cases.

I propose to let Module#name take an optional parameter that expresses the name space. The parameter should be either a module, string, or a symbol.

I am not sure whether a positional argument or a keyword argument is better.

Positional argument:

A::B::C.name("A") # => "B::C"
A::B::C.name(:A) # => "B::C"
A::B::C.name(A) # => "B::C"
A::B::C.name("A::B") # => "C"
A::B::C.name(:"A::B") # => "C"
A::B::C.name(A::B) # => "C"

Keyword argument:

A::B::C.name(namespace: "A") # => "B::C"
A::B::C.name(namespace: :A) # => "B::C"
A::B::C.name(namespace: A) # => "B::C"
A::B::C.name(namespace: "A::B") # => "C"
A::B::C.name(namespace: :"A::B") # => "C"
A::B::C.name(namespace: A::B) # => "C"

If the module/class does not belong to the namespace given as the parameter, then perhaps it would be a good idea to prepend the name with ::.

class A; class B; class D end end end
class E end
A::B::C.name # => "A::B::C"
A::B::C.name(A::B::D) # => "::A::B::C"
A::B::C.name(E) # => "::A::B::C"
Actions #1

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)
Actions #2

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

  • Description updated (diff)
Actions

Also available in: Atom PDF

Like0
Like0Like0