3 ms·
Ahhh, the Rails inflector. An entire library dedicated solely to making things less explicit, more magical and more confusing.
by mcantor 16y ago
Ahhh, the Rails inflector. An entire library dedicated solely to making things less explicit, more magical and more confusing.
- sgrove 16y agoOh? I'm curious as to what you mean. I can imagine the CamelCase to camel_case bit that's used for a lot of the internal class/file-naming stuff might fit your description, but certainly not the singularize and pluralize methods.
- mcantor 16y agoI don't really have a huge problem with it. I dislike Rails in general because it's a 500-pound gorilla that people use instead of understanding what's actually going on in their app, and I'm frustrated that it's the first point of contact most people have with Ruby. Rails has a lot of sorcery in its code, which is trivially exemplified[1] by the inflector, and Ruby doesn't have to be that way. For example, let's say you have a Big Giant App with fifty database tables, each with a corresponding model object. You can use the Rails inflector, which is God-knows-how-many lines of code by now. Or, you can have a 50-line file that looks like this: model_table_map = { User => 'users', Shark => 'sharks', Lazer => 'lazers', Antenna => 'antennae', ... Locus => 'loci', Species => 'species' } (Don't ask me what kind of screwed up, terrifying app you could possibly be writing that requires these classes, 'cause I don't know!) This has numerous advantages over using the Inflector: - The conversion from singular class name to plural table name is unambiguous and self-documenting, so if you come back to the code in 6 months, or if you are working with someone for whom English is not their primary language, you don't have to wonder, "Hmmm... is 'locus' one of those words with an irregular plural inflection? If so, does the Inflector know about it?" - It's one less dependency you need to trust. - Memoization makes this already-minuscule point nearly moot, but calling model_table_map[obj.class] is faster than inflecting it. Like I said: It's really not a big deal, and probably counts more towards personal preference than anything else. I simply prefer to be as explicit as possible until it would simply hurt less to use magic. "Design is the successive application of constraints until only a unique product is left." - Donald Norman, The Design of Everyday Things 1. Can something non-trivial be trivially exemplified? I dunno. :-/