Better way of writing multiple methods with similar definition ruby -
How can I improve this code so that there is no duplicate code and this dynamically gives me similar Will allow to add?
def fabric_ids = (property_name) @fabric_ids = [] if @ fabric_ids.blank? @ Fabric_s & lt; & Lt; $ Property_values [property_name] end def work_ids = (property_name) @work_ids = [] if @ work_ids.blank? @work_ids & lt; & Lt; $ Property_values [property_name] end def type_ids = (property_name) @type_ids = [] if @type_ids.blank? @type_id & lt; & Lt; $ Property_values [property_name] end
You define your methods dynamically In this way, like this:
% w (Fashion_ides work_ides type_id) .each do | Name | Define_method ("# {name} =") do Property_name | Instance_variable_set (name, []) if instance_variable_get (name) .blank? Instance_variable_get (name) & lt; & Lt; $ Property_value [attribute_name] end end
Comments
Post a Comment