Posts [ 3 ]

Topic: Quick Tip: One for all and all for One!

I just converted a method that used to take (among other arguments) one id in aparamter and do a bunch of stuff lookups and computations and (gasp) return some result.

Next thing I know is that I really need another method just like that one, that does it's thing for an entire list of id's. Here is one thing I did to simplify my code and use just a single method than can handle either a single id, or an array of ids:

def big_method(id_or_ids, other, args, here)

ids = [id_or_ids].flatten ...

end

I know you it's kind of a yawner, but it beats doing things like this:

if id_or_ids.kind_of?(Array) ids = id_or_ids else ids = [ id ] end

Anyhow -- once you got "ids", you know it's an array, and you can convert the entire method to just work on the array. Ok ok ... I'm just still excited by ruby is all.

In this case it repeats the call for each item in the array if an array is given. One advantage to this is it returns the responses in an array if an array is given, but just returns the response normally if no array is given.

Re: Quick Tip: One for all and all for One!

OOoo seski! Thanks!Of course in my case -- I actually don't want to return an array, but a cumulative something. For example a graph that can handle showing information about 1 user, or N users -- either case I still just want 1 graph.