Lots of great points here. It really comes to what problem you are solving, and in order to be able to identify when to use what, you'd need experience. Here's something that I came across not too long ago at work that I solved using procedural.
The problem that I was solving was related to caching:
1. Get value from cache
2. If not available in cache, get it from database
3. Then store the value from the database to the cache
In Ruby, you can pass an anonymous function to another function. The function that receives this anonymous function may decide (or not) to invoke. This is what the function roughly looks like:
def get_from_cache(key, &block) value = cache.get(key) return value if value value = block.call # call the anon function cache.set(key, value) return value end
The function is simple enough. Attempt to get value from cache, if found, return value immediately. If cache doesn't have the key, invoke another function, and use that function's return value as the value for the key.
We can 'chain' this up, creating complex logic structure. Examples:
# Get value from database and cache it value = get_from_cache("foo") do get_from_database("foo") end # Get value from HTTP and cache it value = get_from_cache("foo") do http("http://www.gamedev.net/posts?key=foo") end # Or multiple caching layers value = get_from_cache("foo") do get_from_another_cache("foo") do get_from_database("foo") do http("http://www.gamedev.net/posts?key=foo") end end end