[#115884] Windows Ruby 3.2.2: Non-English character added to Windows Registry String Value — Jay Mav via ruby-core <ruby-core@...>
Hello,
3 messages
2023/12/24
[ruby-core:115973] [Ruby master Feature#20093] Syntax or keyword to reopen existing classs/modules, never to define new classs/modules
From:
"tagomoris (Satoshi Tagomori) via ruby-core" <ruby-core@...>
Date:
2023-12-29 05:28:43 UTC
List:
ruby-core #115973
Issue #20093 has been updated by tagomoris (Satoshi Tagomori).
I commented that "class_eval / module_eval can do the same things functionally.". However, as @Eregon pointed out, there is a different rule about constants around `class_eval` / `module_eval`.
The method definition in `class_eval`/`module_eval` cannot refer to the class/module constants, and the block of `class_eval`/`module_eval` block cannot add/update the class/module constants. So, guiding people to use `class_eval` / `module_eval` instead of `class`/`module` for monkey patching should cause trouble and confusion.
For the same reason, @matheusrich 's idea (`Module#reopen` method) is not ideal in my opinion. Adding methods instead of a new keyword should be much easier, but it's not what I want.
The idea of top-level keywords, @Edwing123 's `open class` and @zverok 's `refine` look very hard to implement properly to me. `open class` conflicts with the existing `Kernel#open`. If `refine` is a method to take a block argument, it has the same problem with `class_eval`. If `refine` is a new top-level keyword (without following `do`), it will conflict with the `Module#refine` (Ruby core team members may be able to find any solution to avoid the conflict, though).
So, My idea is still to have a new keyword between `class`/`module` and the name of class/module: `class extension String`.
@Eregon It's true that this ticket's idea came up in an offline discussion about namespaces, but I thought this idea has its own value to be proposed as an independent feature, and I think it's (partially, at least) true because some people are making comments to agree with the intention.
----------------------------------------
Feature #20093: Syntax or keyword to reopen existing classs/modules, never to define new classs/modules
https://bugs.ruby-lang.org/issues/20093#change-105936
* 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/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/