Skip to content

Add support for sub/superclasses (NoMethodError: undefined method `values' for nil ?) #49

Description

@coneybeare

I have an older project, just updated to rails 6.1 and ruby 3. However, I am seeing an issue when trying to read the values from here, and not sure why

class PostVideoType < PostMediaType
  include Ruby::Enum

  define :RAW,          'Raw Video'
  define :GFYCAT,       'Gfycat'
  define :GIPHY,        'Giphy'
  define :KICKSTARTER,  'Kickstarter'
  define :INSTAGRAM,    'Instagram'
  define :IMGUR,        'Imgur'
  define :TWITTER,      'Twitter'
  define :VIDME,        'Vidme'
  define :VIMEO,        'Vimeo'
  define :VINE,         'Vine'
  define :YOUTUBE,      'YouTube'
end
[1] pry(main)> PostVideoType.keys
=> [:RAW, :GFYCAT, :GIPHY, :KICKSTARTER, :INSTAGRAM, :IMGUR, :TWITTER, :VIDME, :VIMEO, :VINE, :YOUTUBE]
[2] pry(main)> PostVideoType.value(:RAW)
=> "Raw Video"
[3] pry(main)> PostVideoType.values
NoMethodError: undefined method `values' for nil
from /Users/redacted/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/ruby-enum-1.0.0/lib/ruby-enum/enum.rb:126:in `values'

It doesn't appear that the values method is working correctly, but I can't see why.

Activity

  1. coneybeare commented on Aug 14, 2024

    @coneybeare
    ContributorAuthor

    Ah, think I figured it out. I had include Ruby::Enum in the superclass PostMediaType, even though I put it here to try and simplify it. The @_enum_hash seems to have not been accessible that way. Moving include Ruby::Enum into every subclass directly, and removing from the super class works again.

  2. dblock commented on Aug 14, 2024

    @dblock
    Owner

    I think supporting superclass enums is a very desirable feature! Let's reopen this.

    Want to try and write some (failing) specs and maybe an implementation?

  3. reopened this on Aug 14, 2024
  4. changed the title [-]NoMethodError: undefined method `values' for nil ?[/-] [+]Add support for sub/superclasses (NoMethodError: undefined method `values' for nil ?)[/+] on Aug 14, 2024
  5. pldavid2 commented on Feb 17, 2026

    @pldavid2

    Related to this issue, changing

    result = @_enum_hash.values.map(&:value)
    to
    result = (@_enum_hash || {}).values.map(&:value)

    make it possible to have a subclass without defining new constants.

  6. dblock commented on Feb 20, 2026

    @dblock
    Owner

    @pldavid2 attempt a PR?

  7. dblock commented on Aug 15, 2026

    @dblock
    Owner

    Copilot fixed it for us in #58

  8. added a commit that references this issue on Aug 15, 2026
    5552b1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions