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.
-
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-based indexing.
- “Returns value if number was provided, and number if value was provided.”
- 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.
-
Perplexed and disoriented is the correct frame of mind to be in while viewing this code.
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.
-
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.
-
Oh you mean the thread title? That was just a red herring. The rwtfs are, in numerical order:
-
1-based indexing.
-
“Returns value if number was provided, and number if value was provided.”
-
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 tokensDo 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.
-
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.
-
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
enum
s toint
s 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>(
-
That reads like a set, rather than an enum.
-
Setting one property clears the others.
-
So
ttt.foo = true; ttt.bar = true; print(ttt.foo ? "true" : "false");
Would return false? Seems quite unintuitive to me.
-
Idiomatic ruby would be
ttt.foo! ttt.bar! ttt.foo? #returns false
which, while effectively the same thing, somehow seems a lot more intuitive to me.
-
PHP in the Emperor's new clothes seems like an accurate description.
-
-
-
OK, that's the second time I've looked at that and I still have no idea what this alleged "canonical style" actually looks like. Would it kill them to put even 5 lines of code up on that page?
OTOH, I suppose I should approve of pedantic style nazism, given the cirumstances...
-
It's Go. Do you really want to know what it looks like?
-
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 mappingyour whole application on the C side anyway…FTFY. Lesser of two evils, and all that...
-
It's Go. Do you really want to know what it looks like?
I'm guessing it looks like poorly formatted C code with strange punctuation and weird neologisms like goroutine scattered all over the place...
-
It's Go. Do you really want to know what it looks like?
No. No Go. No, no. No, Go; no.
-
-
They tried to make @HardwareGeek go to rehab, he say.....
HardwareGeek:No Go. No
I can only give you one like for this, so have a .
-
Putting it all in C? That gets really tedious with a larger application…
-
Hey, it seems like
<del>and <ins> tags have a cool diff background now. I don't think that was there before...Putting it all in C? That gets really tedious with a larger application…
Still better than Ruby.
-
Hey, it seems like <del> and <ins> tags have a cool diff background now. I don't think that was there before...
This evening: http://what.thedailywtf.com/t/strike-del-ins/6860