4 ms·
" A quick little metaprogramming loop goes over the default options hash and adds getters and setters for those properties, so that they work transparently from
by callmeed 17y ago
" A quick little metaprogramming loop goes over the default options hash and adds getters and setters for those properties, so that they work transparently from the object."
Where in your app does this loop exist? Does most Rails metaprogramming happen in models or controllers?
Great post. Very informative.
- patio11 17y agoCards on the table: I think Rails developers, as a community, spend too much time on "What is the right way to do this" questions when they could spend 40% of the time on "Does this way make sense?" and 60% on implementing more features for paying customers. That being said: the fact that this example both cares about how the data is structured internally and discusses the "business logic" of my domain both strongly encourage putting the associated logic in the model. You can certainly do metaprogramming in controllers, though I have no particular opinion on what the mix is for projects other than mine. An example where I could see using metaprogramming in a controller is having live AJAX-y update of fields in a form. (Why put that in the controller? Because it involves how you are structuring access from the application. And, more pragmatically, because adding methods to a controller gets you routes for free and adding methods to a model does not.) Rather than defining 10 different actions which are each strikingly similar, just iterate over what fields are in the model and add a controller method for each. You'll get your routes for free, so all you have to do is hook it all together in the view, which you can do with metaprogramming-enabled helpers.
- veemjeem 17y agoI think the whole notion of "you should be spending more time doing X" is kind of pointless. People do whatever they think is fun, especially for open source projects. Maybe you should be spending that time saving whales instead!
- gloob 17y agoWhat's more, if there's one group of programmers who should be worried about doing things The Right Way, it's library writers.
- Davertron 17y agoI think he's talking about people using Rails worrying too much, not the people writing the Rails framework. Just as an aside, I totally agree that we spend too much time worrying about this sort of thing. I'm definitely guilty of this myself. I think it's probably born out of the heavy use of "convention over configuration" in Rails; whenever I try to do something, I always think "Am I following convention here?". My instinct now usually tells me "probably not, but just get it done and worry about that later".
- callmeed 17y agoHey I was just curious about your experience, not really any "right" way to do anything. I'm an intermedaite rails developer at best and still wrapping my head around Ruby metaprogramming. Context was my goal. You can throw your cards on my table any time, though. I don't mind.
- raganwald 17y agoI think Rails developers, as a community, spend too much time on "What is the right way to do this" questions when they could spend 40% of the time on "Does this way make sense?" and 60% on implementing more features for paying customers. I do not self-identify as a Rails Programmer, so I can't speak for what they do and why. But I will say that taking the time to decide on the "right" way to do X is a win if X ever comes up again. That is very nearly the definition of "right" as opposed to "convenient" or "pragmatic:" What way makes sense now and in the future, what way is a general pattern we can apply when the same situation and forces arise? We often think that this won't come up in this app again, so why invest the extra energy? But in a blogging environment, when you share and discuss your idea of the right way with others, the community benefits from your work. Likewise, you benefit from others taking the time to navel-gaze over the right way to do stuff you haven't encountered yet. Summary of my suggestion: Investing extra energy in thinking about the "right" way to do things is beneficial in a broadly collaborative and highly communicative environment like the blogosphere.