May contain a hash of options:



  • Continuing the discussion from The nerdy jokes thread:

    Meanwhile, over in wtf land:

          class Enum < Hash
              # Public: Initialize an enum.
              #
              # members - the array of enum members. May contain a hash of options:
              #           :start   - the number of first enum member. Defaults to 1.
              #
              # Examples
              #
              #   FRUITS = Enum.new(:apple, :orange, :kiwi)
              def initialize(*members)
                super({})
            
                options = members.extract_options!
                start = options.fetch(:start) { 1 }
            
                update Hash[members.zip(start..members.count + start)]
              end
            
              # Public: Access the number/value of member.
              #
              # ids_or_value - number or value of member.
              #
              # Returns value if number was provided, and number if value was provided.
              def [](id_or_value)
                fetch(id_or_value) { key(id_or_value) }
              end
          end
    


  • I don't get it.


  • Discourse touched me in a no-no place

    I guess "hash of options" just sounds weird, but I guess it's like a congress of weasels or whatever.



  • Ok, here's the definition of extract_options!:

    The file is over 50% comments. Ruby is a language with a lot of syntactic sugar and very little boilerplate.



  • Oh you mean the thread title? That was just a red herring. The rwtfs are, in numerical order:

    1. 1-based indexing.
    2. “Returns value if number was provided, and number if value was provided.”
    3. Rolling your own enum type.


  • So instead of explaining that, you pulled a @ben_lubar and just posted the content without an explanation until post number 5.



  • I don't understand why somebody would want the start option to be configurable.



  • Imagine if, when you had gone to the source of the extract_options method, you saw something that in no way produced the behavior that you would expect from the code sample I posted?

    Perplexed and disoriented is the correct frame of mind to be in while viewing this code.



  • Because they chose a ridiculous default?



  • I have no strong opinion about 0 or 1 based indexing. But it's pretty stupid to write code that could be either one with no way to tell from the code at the call site.



  • I know right? Because either the number that's assigned to each value doesn't matter, and there's no reason to configure it, or it does matter, and specifying it through the order of a variable's ctor arguments in the code is totally inappropriate.



  • @Buddy said:

    Perplexed and disoriented is the correct frame of mind to be in while viewing this code.

    @Buddy said:

    Because they chose a ridiculous default?

    I was going to say, "Because they chose a ridiculous language." Shit like this confirms my good sense in staying away from Ruby.



  • @Captain said:

    I have no strong opinion about 0 or 1 based indexing.

    Yes, I use both in different circumstances, and they are convenient in different ways.


  • Discourse touched me in a no-no place

    @Buddy said:

    Oh you mean the thread title? That was just a red herring. The rwtfs are, in numerical order:

    1. 1-based indexing.

    2. “Returns value if number was provided, and number if value was provided.”

    3. Rolling your own enum type.

    • Having an enum type at all in a language that can work so easily with string tokens.

    Seriously, why would you bother with all the tedious mapping to and from integers except when interfacing to some low-level C code? And then you'd put the mapping on the C side anyway…



  • Shit like this confirms my good sense

    Sometimes I feel like Ruby is just PHP in {sheep's, emperor's new} clothing. But what do I know, I'm just a stupid kid.

    @dkf:
    string tokens

    Do you mean strings (Ruby strings are mutable) or tokens (Ruby tokens are a memory leak)? Confusing the two (they cannot be used interchangeably) will cause you a world of trouble.

    why bother with the tedious mapping

    It's for storing them in the db. Except that they accidentally also used the ints as parameters in several places, making the whole thing an exercise in futility.



  • @Buddy said:

    Do you mean strings (Ruby strings are mutable) or tokens (Ruby tokens are a memory leak)? Confusing the two (they cannot be used interchangeably) will cause you a world of trouble.

    He means :symbols as should have been clear from the context. No 🚩 for you.


  • Discourse touched me in a no-no place

    Yep, and in the database I'd keep the “enum” with names in its own table and use foreign keys referencing that “enum table” in my main table. Like that, I wouldn't need to actually understand the tedious small numbers for the enum, when what it's actually talking about is “it's one choice from this small set”.

    Mapping enums to ints under the covers is merely an implementation feature of how C works.



  • This. We've got plenty of named options, named settings variants, etc. They're translated by enumerations in C code, but configuration (disk/db) and web UI (php) all use the strings.



  • Rails has a really nice* enum type, where you can define the enum on a class, and each of the enum members behaves like a boolean property.

    * ‘Nice’ as in ‘sickeningly sweet syntactic sugar’. It is critical to understand Ruby well in order to understand how to use enums effectively.



  • No pennant for a pendant? :<o>(


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.