Skip to content

Commit 674126d

Browse files
dblockCopilot
andcommitted
Rename Ruby::Enum to RubyEnum::Enum
Ruby 4.0 reserves the top-level Ruby module for the language itself, so reopening 'module Ruby' in this gem is no longer safe. Renames the gem's namespace from Ruby::Enum to RubyEnum::Enum and bumps the major version to 2.0.0. For backward compatibility, Ruby::Enum (and Ruby::Enum::Case, Ruby::Enum::Errors) remain available as deprecated aliases that warn on inclusion and delegate to RubyEnum::Enum. They will be removed in a future major version. Closes #55. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 91bfc0e commit 674126d

21 files changed

Lines changed: 180 additions & 80 deletions

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
### 1.2.0 (Next)
1+
### 2.0.0 (Next)
22

3+
* [#55](https://github.com/dblock/ruby-enum/issues/55): Renamed `Ruby::Enum` to `RubyEnum::Enum` since Ruby 4.0 reserves the top-level `Ruby` module - [@dblock](https://github.com/dblock).
34
* Your contribution here.
45

56
### 1.1.0 (2026/6/20)

‎README.md‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Ruby::Enum
1+
RubyEnum::Enum
22
==========
33

44
[![Gem Version](http://img.shields.io/gem/v/ruby-enum.svg)](http://badge.fury.io/rb/ruby-enum)
@@ -41,7 +41,7 @@ Define enums, and reference them as constants.
4141

4242
``` ruby
4343
class OrderState
44-
include Ruby::Enum
44+
include RubyEnum::Enum
4545

4646
define :CREATED, 'created'
4747
define :PAID, 'paid'
@@ -51,7 +51,7 @@ end
5151
``` ruby
5252
OrderState::CREATED # 'created'
5353
OrderState::PAID # 'paid'
54-
OrderState::UNKNOWN # raises Ruby::Enum::Errors::UninitializedConstantError
54+
OrderState::UNKNOWN # raises RubyEnum::Enum::Errors::UninitializedConstantError
5555
OrderState.keys # [ :CREATED, :PAID ]
5656
OrderState.values # [ 'created', 'paid' ]
5757
OrderState.to_h # { :CREATED => 'created', :PAID => 'paid' }
@@ -63,7 +63,7 @@ Define enums, and reference them as class methods.
6363

6464
``` ruby
6565
class OrderState
66-
include Ruby::Enum
66+
include RubyEnum::Enum
6767

6868
define :created, 'created'
6969
define :paid, 'paid'
@@ -85,7 +85,7 @@ The value is optional. If unspecified, the value will default to the key.
8585

8686
``` ruby
8787
class OrderState
88-
include Ruby::Enum
88+
include RubyEnum::Enum
8989

9090
define :UNSPECIFIED
9191
define :unspecified
@@ -206,22 +206,22 @@ OrderState.key('failed')
206206

207207
### Duplicate enumerator keys or duplicate values
208208

209-
Defining duplicate enums raises `Ruby::Enum::Errors::DuplicateKeyError`.
209+
Defining duplicate enums raises `RubyEnum::Enum::Errors::DuplicateKeyError`.
210210

211211
```ruby
212212
class OrderState
213-
include Ruby::Enum
213+
include RubyEnum::Enum
214214

215215
define :CREATED, 'created'
216216
define :CREATED, 'recreated' # raises DuplicateKeyError
217217
end
218218
```
219219

220-
Defining a duplicate value raises `Ruby::Enum::Errors::DuplicateValueError`.
220+
Defining a duplicate value raises `RubyEnum::Enum::Errors::DuplicateValueError`.
221221

222222
```ruby
223223
class OrderState
224-
include Ruby::Enum
224+
include RubyEnum::Enum
225225

226226
define :CREATED, 'created'
227227
define :RECREATED, 'created' # raises DuplicateValueError
@@ -232,11 +232,11 @@ The `DuplicateValueError` exception is raised to be consistent with the unique k
232232

233233
### Inheritance
234234

235-
When inheriting from a `Ruby::Enum` class, all defined enums in the parent class will be accessible in sub classes as well. Sub classes can also provide extra enums, as usual.
235+
When inheriting from a `RubyEnum::Enum` class, all defined enums in the parent class will be accessible in sub classes as well. Sub classes can also provide extra enums, as usual.
236236

237237
``` ruby
238238
class OrderState
239-
include Ruby::Enum
239+
include RubyEnum::Enum
240240

241241
define :CREATED, 'CREATED'
242242
define :PAID, 'PAID'
@@ -264,16 +264,16 @@ ShippedOrderState.values # ['CREATED', 'PAID', 'PREPARED', SHIPPED']
264264

265265
### Exhaustive case matcher
266266

267-
If you want to make sure that you cover all cases in a case stament, you can use the exhaustive case matcher: `Ruby::Enum::Case`. It will raise an error if a case/enum value is not handled, or if a value is specified that's not part of the enum. This is inspired by the [Rust Pattern Syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html). If multiple cases match, all matches are being executed. The return value is the value from the matched case, or an array of return values if multiple cases matched.
267+
If you want to make sure that you cover all cases in a case stament, you can use the exhaustive case matcher: `RubyEnum::Enum::Case`. It will raise an error if a case/enum value is not handled, or if a value is specified that's not part of the enum. This is inspired by the [Rust Pattern Syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html). If multiple cases match, all matches are being executed. The return value is the value from the matched case, or an array of return values if multiple cases matched.
268268

269269
> NOTE: This will add checks at runtime which might lead to worse performance. See [benchmarks](#benchmarks).
270270
271-
> NOTE: `:else` is a reserved keyword if you want to use `Ruby::Enum::Case`.
271+
> NOTE: `:else` is a reserved keyword if you want to use `RubyEnum::Enum::Case`.
272272
273273
```ruby
274274
class Color < OrderState
275-
include Ruby::Enum
276-
include Ruby::Enum::Case
275+
include RubyEnum::Enum
276+
include RubyEnum::Enum::Case
277277

278278
define :RED, :red
279279
define :GREEN, :green

‎UPGRADING.md‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,45 @@
1-
# Upgrading Ruby::Enum
1+
# Upgrading RubyEnum::Enum
2+
3+
## Upgrading to >= 2.0.0
4+
5+
### Module renamed from `Ruby::Enum` to `RubyEnum::Enum`
6+
7+
Ruby 4.0 reserves the top-level `Ruby` module for the language itself, so this
8+
gem's `Ruby::Enum` namespace is no longer safe to use. As of `2.0.0`, the gem
9+
uses `RubyEnum::Enum` instead.
10+
11+
`gem 'ruby-enum', '< 2.0.0'`
12+
13+
``` ruby
14+
class Color
15+
include Ruby::Enum
16+
17+
define :RED, 'red'
18+
end
19+
```
20+
21+
`gem 'ruby-enum', '>= 2.0.0'`
22+
23+
``` ruby
24+
class Color
25+
include RubyEnum::Enum
26+
27+
define :RED, 'red'
28+
end
29+
```
30+
31+
For backward compatibility, `Ruby::Enum` is still available as a deprecated
32+
alias for `RubyEnum::Enum` and will emit a deprecation warning when included.
33+
It will be removed in a future major version. Update your code to use
34+
`RubyEnum::Enum` and `RubyEnum::Enum::Case` directly.
35+
36+
See [#55](https://github.com/dblock/ruby-enum/issues/55) for more information.
237

338
## Upgrading to >= 0.9.0
439

5-
### Inheritance & `Ruby::Enum.values`
40+
### Inheritance & `RubyEnum::Enum.values`
641

7-
This only applies to classes that inherit from another which is a `Ruby::Enum`.
42+
This only applies to classes that inherit from another which is a `RubyEnum::Enum`.
843

944
Prior to version `0.9.0`, the `values` class method would enumerate only the
1045
values defined in the class.
@@ -14,7 +49,7 @@ the entire class heirarchy, ancestors first.
1449

1550
``` ruby
1651
class PrimaryColors
17-
include Ruby::Enum
52+
include RubyEnum::Enum
1853

1954
define :RED, 'RED'
2055
define :GREEN, 'GREEN'

‎benchmarks/case.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
##
99
# Test enum
1010
class Color
11-
include Ruby::Enum
12-
include Ruby::Enum::Case
11+
include RubyEnum::Enum
12+
include RubyEnum::Enum::Case
1313

1414
define :RED, :red
1515
define :GREEN, :green

‎lib/config/locales/en.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ en:
99
resolution: "The enumerated value could not be found in class %{name}. Use 'define' to declare it.\n
1010
\_Example:\n
1111
\_\_module %{name}\n
12-
\_\_\_include Ruby::Enum\n
12+
\_\_\_include RubyEnum::Enum\n
1313
\_\_\_define %{key}, 'value'\n
1414
\_\_end"
1515
duplicate_key:

‎lib/ruby-enum.rb‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
# Try to load the I18n gem and provide a mock if it is not available.
99
begin
1010
require 'i18n'
11-
Ruby::Enum.i18n = I18n
11+
RubyEnum::Enum.i18n = I18n
1212
rescue LoadError
1313
# I18n is not available
1414
# :nocov:
1515
# Tests for this loading are in the spec_i18n folder
16-
Ruby::Enum.i18n = Ruby::Enum::I18nMock
16+
RubyEnum::Enum.i18n = RubyEnum::Enum::I18nMock
1717
# :nocov:
1818
end
1919

20-
Ruby::Enum.i18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml')
20+
RubyEnum::Enum.i18n.load_path << File.join(File.dirname(__FILE__), 'config', 'locales', 'en.yml')
2121

2222
require 'ruby-enum/errors/base'
2323
require 'ruby-enum/errors/uninitialized_constant_error'
2424
require 'ruby-enum/errors/duplicate_key_error'
2525
require 'ruby-enum/errors/duplicate_value_error'
26+
require 'ruby-enum/deprecated'

‎lib/ruby-enum/deprecated.rb‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# Deprecated backward-compatible alias for RubyEnum::Enum.
4+
#
5+
# Ruby 4.0 reserves the top-level `Ruby` module for the language itself, so
6+
# this namespace is deprecated and will be removed in a future major version.
7+
# Use RubyEnum::Enum instead.
8+
module Ruby
9+
module Enum
10+
def self.included(base)
11+
warn '[DEPRECATION] `Ruby::Enum` is deprecated and will be removed in a future version. ' \
12+
'Use `RubyEnum::Enum` instead.'
13+
14+
base.include RubyEnum::Enum
15+
end
16+
17+
module Case
18+
def self.included(base)
19+
warn '[DEPRECATION] `Ruby::Enum::Case` is deprecated and will be removed in a future version. ' \
20+
'Use `RubyEnum::Enum::Case` instead.'
21+
22+
base.include RubyEnum::Enum::Case
23+
end
24+
end
25+
26+
Errors = RubyEnum::Enum::Errors
27+
end
28+
end

‎lib/ruby-enum/enum.rb‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
module Ruby
3+
module RubyEnum
44
module Enum
55
class << self
66
# Needed for I18n mock
@@ -53,7 +53,7 @@ def store_new_instance(key, value)
5353
end
5454

5555
def const_missing(key)
56-
raise Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key
56+
raise RubyEnum::Enum::Errors::UninitializedConstantError, name: name, key: key
5757
end
5858

5959
# Iterate over all enumerated values.
@@ -128,7 +128,7 @@ def keys
128128
def values
129129
result = @_enum_hash.values.map(&:value)
130130

131-
if superclass < Ruby::Enum
131+
if superclass < RubyEnum::Enum
132132
superclass.values + result
133133
else
134134
result
@@ -164,13 +164,13 @@ def upper?(s)
164164
def validate_key!(key)
165165
return unless @_enum_hash.key?(key)
166166

167-
raise Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key
167+
raise RubyEnum::Enum::Errors::DuplicateKeyError, name: name, key: key
168168
end
169169

170170
def validate_value!(value)
171171
return unless @_enums_by_value.key?(value)
172172

173-
raise Ruby::Enum::Errors::DuplicateValueError, name: name, value: value
173+
raise RubyEnum::Enum::Errors::DuplicateValueError, name: name, value: value
174174
end
175175
end
176176
end

‎lib/ruby-enum/enum/case.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22

3-
module Ruby
3+
module RubyEnum
44
module Enum
55
##
66
# Adds a method to an enum class that allows for exhaustive matching on a value.
77
#
88
# @example
99
# class Color
10-
# include Ruby::Enum
11-
# include Ruby::Enum::Case
10+
# include RubyEnum::Enum
11+
# include RubyEnum::Enum::Case
1212
#
1313
# define :RED, :red
1414
# define :GREEN, :green
@@ -33,7 +33,7 @@ def self.included(klass)
3333
end
3434

3535
##
36-
# @see Ruby::Enum::Case
36+
# @see RubyEnum::Enum::Case
3737
module ClassMethods
3838
class ValuesNotDefinedError < StandardError
3939
end

‎lib/ruby-enum/enum/i18n_mock.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# :nocov:
4-
module Ruby
4+
module RubyEnum
55
module Enum
66
##
77
# Mock I18n module in case the i18n gem is not available.

0 commit comments

Comments
 (0)