Issue #20093 has been updated by rubyFeedback (robert heiler).
Dan0042 wrote:
I think what you're looking for is class_eval /
module_eval
I am not entirely certain tagomoris meant to use **.class_eval** here.
See his alternative suggestion of "reopen".
Of course I may be wrong, but if I understood it correctly his issue
request may be more related towards being able to extend something in
ruby no matter if it is a class or a module or already-defined versus
yet-to-be-defined; a bit like a "promise" of functionality, not unlike
refinements make isolated modifications to existing classes/code.
E. g.:
class extension String
class reopen String
reopen String # just as an example to drop the class/module name altogether and may be
shorter
In particular in the last case, I have had use cases to want to **avoid** having to type
either "class" or "module" altogether. I also agree with the use case
of refinements, by the way, but
I always felt the syntax awkward. This is also one slight problem I have here, in that I
think
"class foobar String" is too verbose - and it may also be a bit confusing for
ruby users. But I
think tagomoris has a point as well - the intention should be for a ruby developer to
"modify behaviour xyz". As stated, I may be mistaken, so perhaps tagomoris can
refer to the .class_eval example. We should ideally come up with a useful syntax, though -
being an elegant language is
one of ruby's strong points, even if what is elegant differs between individuals
naturally.
----------------------------------------
Feature #20093: Syntax or keyword to reopen existing classs/modules, never to define new
classs/modules
https://bugs.ruby-lang.org/issues/20093#change-105878
* Author: tagomoris (Satoshi Tagomori)
* Status: Open
* Priority: Normal
----------------------------------------
`class A` and `module B` will reopen existing class A or module B to add/re-define methods
if A/B exists. Otherwise, these will define the new class/module A/B.
But, in my opinion, the code of `class A` for patching existing classes doesn't work
expectedly when `A` is not defined beforehand. It expects other codes to define `A` before
being called.
For example:
```ruby
# string_exclude.rb
class String
def exclude?(string)
!include?(string)
end
end
```
This code expects that there is the `String` class, and it has the `include?` method. This
code doesn't work if the file is loaded in the way below:
```ruby
load('string_exclude.rb', true)
```
This code doesn't raise errors and will define an almost empty class (only with a
method `exclude?` to raise NameError). It should be unexpected for every user.
So, I want to propose a new syntax to reopen the existing class/module or raise errors if
the specified class/module is not defined.
```ruby
class extension String
def exclude?(string)
!include?(string)
end
end # adds #exclude? to String class
class extension Stroooong
def exclude?(string)
!include?(string)
end
end # will raise NameError (or something else)
```
Some additional things:
* `class extension String` (and `module extension String`) causes a compile error
(SyntaxError) on Ruby 3.3. So we have space to add a keyword between class/module and the
class/module name.
* I don't have a strong opinion about the keyword name `extension`. An alternative
idea is `reopen`.
--
https://bugs.ruby-lang.org/