# Fiddle: enums?

**URL:** <https://rubytalk.org/t/fiddle-enums/73992>\
**Category:** ruby-talk\
**Created:** [10 January 2017 21:08 UTC](https://rubytalk.org/t/fiddle-enums/73992 "2017-01-10T21:08:19Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Marvin\_Gulker](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Marvin\_Gulker](https://rubytalk.org/u/Marvin_Gulker)\
**Post date:** [11 January 2017 18:19 UTC](https://rubytalk.org/t/fiddle-enums/73992/4 "2017-01-11T18:19:35Z")

</div>

Hi,

> What's wrong with something like:
> 
> module MyLib  
> &nbsp;&nbsp;extend Fiddle::Importer  
> &nbsp;&nbsp;dlload "mylib.so"  
> &nbsp;&nbsp;FOO = 1  
> &nbsp;&nbsp;BAR = 2  
> &nbsp;&nbsp;BAZ = 3  
> &nbsp;&nbsp;extern "void func(int)"  
> end
> 
> MyLib.func(MyLib::BAR)

Somehow I haven't thought about using constants for the enum's values,  
but yes, it is an option indeed. It doesn't look as rubyish as a

&nbsp;&nbsp;&nbsp;&nbsp;MyLib.func(:bar)

would do, though, but I understand that this is something I have to  
implement. Thank you for your suggestion anyway.

> You could wrap that list of const assignments up into some sort of  
> class method `enum` that creates the consts from a list of names (and  
> maybe a starting offset).

Yes, that's the obvious consequence to draw from this approach.

> what you're asking for wrt fiddle treating enums like types rather  
> than named numbers is outside the scope of what fiddle is for. If you  
> want to do extra arg checking, that's on you. That's not the job of  
> ruby, fiddle, \_or\_ C.

The expectation came from using ruby-ffi, which does have special  
treatment for enums[1], and as ruby-ffi and fiddle serve the same  
purpose after all, it did not occur to me that handling of enums is out  
of scope for fiddle.

Greetings  
Marvin

[1]: [Enums · ffi/ffi Wiki · GitHub](https://github.com/ffi/ffi/wiki/Enums)

> **···**
>
> On Wed, Jan 11, 2017 at 03:53:06AM -0800, Ryan Davis wrote:
> 
> --  
> Blog: [http://www.guelkerdev.de](http://www.guelkerdev.de)  
> PGP/GPG ID: F1D8799FBCC8BC4F

---

_[View the full topic](https://rubytalk.org/t/fiddle-enums/73992)._
