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

(Gerald Bauer) #1

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

(RRRoy BBBean) #2

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] https://github.com/s6ruby/enums
[2] https://github.com/s6ruby/safestruct

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

(Gerald Bauer) #3

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] https://solidity.readthedocs.io/en/latest/types.html#enums

(RRRoy BBBean) #4

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] https://solidity.readthedocs.io/en/latest/types.html#enums

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

(Gerald Bauer) #5

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 https://rubygems.org/gems/enums 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.

(Gerald Bauer) #6

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] https://github.com/s6ruby/safedata

(Gerald Bauer) #7

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] https://github.com/s6ruby/enums