Ross
Ross

Reputation: 1425

Should this use the "map" method?

Is there a more elegant way to do this? I feel like "map" should be in there somewhere:

[
    :method_a,
    :method_b,
    :method_c
].each do |method|
    items.each do |item|
        self.send(method, item)
    end
end

Upvotes: 0

Views: 162

Answers (2)

Mladen Jablanović
Mladen Jablanović

Reputation: 44080

BTW, you can use product method to eliminate nested structures:

[
  :method_a,
  :method_b,
  :method_c
].product(items).map{|method, item|
  send(method, item)
}

Upvotes: 3

toddsundsted
toddsundsted

Reputation: 6345

Whether to use map or each depends totally on whether or not you want the operation to return a list of the results of the application of the method (the send) or not.

Upvotes: 7

Related Questions