Search code examples
rubymethodsnamespacesargumentsrake-task

Inefficient Ruby method naming: passing namespace as argument as a way to call methods


There has got to be a more efficient way to do this in Ruby. I have a list of methods that scrape the same things (title, price) across multiple sites but in slightly different ways based on the code in each store. For example:

def store1_get_title
def store1_get_price

def store2_get_title
def store2_get_price

def store3_get_title
def store3_get_price

When calling all of these functions, I would just like a generic call with say a "namespace" parameter to do invoke any of these methods without having to type out all of them, something like:

for get_all_stores().each do |store|
     store::get_title
     store::get_price
end

...which would invoke store1_get_title, store1_get_price, store2_get_title, store2_get_price like I want. Is there something like this or a better way to do this?

Hope that makes sense. Thanks for any input!

Edit: these tasks are in rake task code.


Solution

  • This is a perfect use for classes. If you find two stores with the same software powering them (maybe Yahoo commerce or EBay stores) you can make instances of the classes with different parameters.

    class Amazon
      def get_price; end
      def get_title; end
    end
    
    class Ebay
      def initialize seller; end
      def get_price; end
      def get_title; end
    end
    
    [Amazon.new, Ebay.new("seller1"), Ebay.new("seller2")] each do |store|
       store.get_price
       store.get_title
    end
    

    And you can do this in any other object-oriented language by defining a base class or interface that all of the stores implement/inherit.