enums 1.0 library / gem - safe enumeration types for ruby e.g. enum :Color, [:red, :green, :blue]

Hello,

   I've put together a new enums library / gem [1] for safe
enumeration types in ruby.

Yes, enums are just a set of symbolic keys bound to unique integer numbers.
Why not just use symbols :slight_smile: or constants? Do we really need a new enum type
and (yet another) library? Good point.

  Let the ruby meta-programming magic work for you :-).

    Enum.new( 'Color', :red, :green, :blue ) # -or-

    enum :Color, :red, :green, :blue # -or-

    enum :Color, [:red, :green, :blue]

  See what you get for "free":

    Color::RED #=> <Color @key=:red, @value=0>
    Color(0) #=> Color::RED -or- <Color @key=:red, @value=0>
    Color.zero #=> same as Color(0)
    Color.red #=> Color::RED
    Color.values #=> [0, 1, 2]
    Color.keys #=> [:red, :green, :blue]
    Color.members #=> [RED, GREEN, BLUE]
                         # -or-
                         # [<Color @key=:red, @value=0>,
                         # <Color @key=:green, @value=1>,
                         # <Color @key=:blue, @value=2>]
    Color(1) #=> Color::GREEN
    Color.value(1) # same as Color(1)
    Color[:red] #=> Color::RED
    Color.key(:red) # same as Color[:red]
    color = Color.red
    color.red? #=> true
    color == Color.red #=> true
    color.value #=> 0
    color.key #=> :red
    color.blue? #=> false
    color == Color.blue #=> false
    color.is_a? Enum #=> true
    color.is_a? Color #=> true

   and some more.

  Questions? Comments? Welcome. Cheers. Prost.

PS: The new enums 1.0 library is part of the safe data structures series. [2]

[1] https://github.com/s6ruby/enums
[2] https://github.com/s6ruby/safestruct

This looks really neat. Does it have any runtime dependencies?

Is it possible to assign a specific integer values to a symbol? For some
symbols? Or perhaps all or no symbols?

Thanks!

···

On 2/28/19 10:41 AM, Gerald Bauer wrote:

Hello,

    I've put together a new enums library / gem [1] for safe
enumeration types in ruby.

Yes, enums are just a set of symbolic keys bound to unique integer numbers.
Why not just use symbols :slight_smile: or constants? Do we really need a new enum type
and (yet another) library? Good point.

   Let the ruby meta-programming magic work for you :-).

     Enum.new( 'Color', :red, :green, :blue ) # -or-

     enum :Color, :red, :green, :blue # -or-

     enum :Color, [:red, :green, :blue]

   See what you get for "free":

     Color::RED #=> <Color @key=:red, @value=0>
     Color(0) #=> Color::RED -or- <Color @key=:red, @value=0>
     Color.zero #=> same as Color(0)
     Color.red #=> Color::RED
     Color.values #=> [0, 1, 2]
     Color.keys #=> [:red, :green, :blue]
     Color.members #=> [RED, GREEN, BLUE]
                          # -or-
                          # [<Color @key=:red, @value=0>,
                          # <Color @key=:green, @value=1>,
                          # <Color @key=:blue, @value=2>]
     Color(1) #=> Color::GREEN
     Color.value(1) # same as Color(1)
     Color[:red] #=> Color::RED
     Color.key(:red) # same as Color[:red]
     color = Color.red
     color.red? #=> true
     color == Color.red #=> true
     color.value #=> 0
     color.key #=> :red
     color.blue? #=> false
     color == Color.blue #=> false
     color.is_a? Enum #=> true
     color.is_a? Color #=> true

    and some more.

   Questions? Comments? Welcome. Cheers. Prost.

PS: The new enums 1.0 library is part of the safe data structures series. [2]

[1] GitHub - cryptopunksnotdead/punks.starter: DIY (Do-It-Yourself) - Yes, You Can! Pixel Art Collection Quick Starter - Generate Algorithmically Your Own Curated (or Randomized) Collection
[2] GitHub - ordbase/ordinals.cache: Ordinals (Inscription Content & Metadata) Cache - Vol. 1 - Bitcoin (BTC)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hello,

     Sorry for the late reply I was offline over the weekend in Venice
at the Carne Vale!

This looks really neat. Does it have any runtime dependencies?

   Thanks for your kind words.
   No. The enums library has NO dependencies other than ruby and the
standard library itself.

Is it possible to assign a specific integer values to a symbol? For some
symbols? Or perhaps all or no symbols?

    For now the enum is "immutable" and starts counting at zero for
the first member.
The enum for now follows the enum data type in solidity [1] because
that's the first use case in (secure) ruby
and why it was created in the first place :-).

      If there's really a need to assign specific integers and if you
have some great examples, for sure, it's possible to add in a future
update e.g. using

Enum.new( 'Color', red: 0, green: 1, blue: 2 )

   instead of

Enum.new( 'Color', :red, :green, :blue )

   Cheers. Prost.

[1] Types — Solidity 0.8.24 documentation

I understand now. It really doesn't make sense to explicitly assign integer values. Thank you.

Is this a gem that I can download and install using gem?

···

On 3/6/19 12:47 PM, Gerald Bauer wrote:

Hello,

      Sorry for the late reply I was offline over the weekend in Venice
at the Carne Vale!

This looks really neat. Does it have any runtime dependencies?

    Thanks for your kind words.
    No. The enums library has NO dependencies other than ruby and the
standard library itself.

Is it possible to assign a specific integer values to a symbol? For some
symbols? Or perhaps all or no symbols?

     For now the enum is "immutable" and starts counting at zero for
the first member.
The enum for now follows the enum data type in solidity [1] because
that's the first use case in (secure) ruby
and why it was created in the first place :-).

       If there's really a need to assign specific integers and if you
have some great examples, for sure, it's possible to add in a future
update e.g. using

Enum.new( 'Color', red: 0, green: 1, blue: 2 )

    instead of

Enum.new( 'Color', :red, :green, :blue )

    Cheers. Prost.

[1] Types — Solidity 0.8.24 documentation

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hello,

Is this a gem that I can download and install using gem?

   Yes, it's a regular "plain vanilla" gem. No dependencies. It's just
ruby. To install use:

$ gem install enums

   See enums | RubyGems.org | your community gem host for official gem page.

Cheers. Prost.

PS: Note: For now everything is in the Safe namespace / module. Try:

require 'enums'

module Safe
   Enum.new( :Color, :red, :blue, :green)
end

pp Safe::Color.values
pp Safe::Color.keys
etc.

Hello,

Is it possible to assign a specific integer values to a symbol? For some
symbols? Or perhaps all or no symbols?

  FYI: I'm building a new library - for safe (algebraic) union data types
  with enumerated variants.

  Now you can go "wild" and use your own integers or strings or really anything.
  Example:

      data :Color, :Red, [1],
                   :Green, [2],
                   :Blue, [3]

      # -or-

      data :Color, :Red, ['red'],
                   :Green, ['green'],
                   :Blue, ['blue']

      # -or-

      data :Color, :Red, [1,'red'],
                   :Green, [2, 'green'],
                   :Blue, [3, 'blue']

      # -or-

      data :Color, :Red, [255, 0, 0],
                   :Green, [0, 255, 0],
                   :Blue, [0, 0, 255],
                   :Other, [:r, :g, :b]

   and so on and so forth [1].

  Happy coding with ruby (and algebraic data types). Cheers. Prost.

[1] GitHub - cryptopunksnotdead/punks.js: punks.js - draw punk (pixel) art images using any design (in ascii text) in any colors; incl. 2x/4x/8x zoom for bigger sizes and more

Hello,

Is it possible to assign a specific integer values to a symbol? For some
symbols? Or perhaps all or no symbols?

FYI: The latest (and greatest) enums library v1.3 [1] now supports
assigning your own integer values too. Example:

enum :Color, { red:   0,
               green: 1,
               blue:  2 }

enum :FileAttrib, { read_only: 1<<0,    # 2^0 = 1  = 0b00000001
                    hidden:    1<<1,    # 2^1 = 2  = 0b00000010
                    system:    1<<2,    # 2^2 = 4  = 0b00000100
                    archive:   1<<5,    # 2^5 = 32 = 0b00100000
                  },
                  flags: true

and so on.

Happy coding with ruby. Cheers. Prost.

[1] GitHub - cryptopunksnotdead/punks.starter: DIY (Do-It-Yourself) - Yes, You Can! Pixel Art Collection Quick Starter - Generate Algorithmically Your Own Curated (or Randomized) Collection