Giter VIP home page Giter VIP logo

Comments (5)

beerlington avatar beerlington commented on August 28, 2024

The problem with using anonymous classes (ie Class.new) is that there isn't any way to know the name of the class before it is created, and therefor the inherited hook doesn't have the information it needs to properly create the enum member. Have you considered just using eval to do it?

[0.13, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30].each do |num|
  num_str = num.to_s.sub(/\./, '_')
  eval "ContaminantDepth::I#{num_str} < ContaminantDepth; end"
end

from classy_enum.

elsurudo avatar elsurudo commented on August 28, 2024

Hmm, interesting. That makes sense, thanks.

I tried to avoid eval, since using a string interpolation for this sort of thing inherently seems ugly.

I'm pretty new to Ruby's meta-programming features; are you aware of any way I could do the equivalent to that eval, but using the cleaner block syntax, as I had done before?

from classy_enum.

beerlington avatar beerlington commented on August 28, 2024

I'm not sure of a way to do it off hand, I would try Stack Overflow and see if anyone has a suggestion.

from classy_enum.

elsurudo avatar elsurudo commented on August 28, 2024

So just FYI, the solution I went with was defining the class in an eval, and then using class_eval to nicely define the methods in a block.

Like so:

[0.13, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30].each do |num|
    num_str = num.to_s.sub(/\./, '_')
    class_name = "In#{num_str}"

    # Must define the class non-anonymously, so superclass' 'inherited' hook gets class name
    eval "class ContaminantDepthIn::#{class_name} <  ContaminantDepthIn; end"

    ContaminantDepthIn.const_get(class_name).class_eval do
        define_method :value do
            num
        end
    end
end

Feels a little dirty, but it's better than just one long eval.

from classy_enum.

beerlington avatar beerlington commented on August 28, 2024

Awesome, glad you were able to come up with something you're happy with!

from classy_enum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.