I’m fairly sure that’s a useless title… sorry.
I want to be able to pass in a Class to a method, and then use that class. Here’s an easy, working, example:
def my_method(klass)
klass.new
end
Using that:
>> my_method(Product)
=> #<Product id:nil, created_at: nil, updated_at: nil, price: nil>
>> my_method(Order)
=> #<Order id:nil, created_at: nil, updated_at: nil, total_value: nil>
What doesn’t work is trying to use the klass variable on a module:
>> ShopifyAPI::klass.first
=> NoMethodError: undefined method `klass' for ShopifyAPI:Module
Am I attempting an impossible task? Can anyone shed some light on this?
Cheers
First off, I don’t think this is impossible.
Surely, there is no
klassmethod defined for modules <- this is true becauseShopifyAPI.methods.include? "klass" # => falseHowever, classes are constants in modules. And modules have a
constantsmethod that you may use to retrieve classes. The problem with this is method is that is also retrieves constants in the modules that are not classes.I came up with this workaround for your problem