sebassebas1313
sebassebas1313

Reputation: 413

Conditionally choose method

I have this:

def some_method()
   if auto_pagination
    customers_list.auto_paging_each do |customer|
     customer_hash = importer.extract(customer)
     arr << customer_hash
    end
  else
    customers_list.each do |customer|
    customer_hash = importer.extract(customer)
    arr << customer_hash
    end
  end
end

I would like to refactor it in a shorter version. I though about send method but with the block I turns out a bit tricky. Any ideas?

Upvotes: 0

Views: 34

Answers (1)

Tom Lord
Tom Lord

Reputation: 28285

I though about send method but with the block I turns out a bit tricky

I'm not sure what you mean by "tricky", but:

def some_method()
  method_name = auto_pagination ? :auto_paging_each : :each

  customers_list.send(method_name) do |customer|
    customer_hash = importer.extract(customer)
    arr << customer_hash
  end
end

Upvotes: 3

Related Questions