class Device
def initialize(device_id, data_resource)
@id = device_id
@data_resource = data_resource
end
def display_device
mode = @data_resource.get_display_device_mode(@id)
presets = @data_resource.get_display_device_presets(@id)
summary = "display_device: #{mode} ($#{presets})"
return "* #{summary}" if presets == "XTC909"
summary
end
def chip
mode = @data_resource.get_chip_mode(@id)
presets = @data_resource.get_chip_presets(@id)
summary = "chip: #{mode} ($#{presets})"
return "* #{summary}" if presets == "XTC909"
summary
end
def input_device
mode = @data_resource.get_input_device_mode(@id)
presets = @data_resource.get_input_device_presets(@id)
summary = "input_device: #{mode} ($#{presets})"
return "* #{summary}" if presets == "XTC909"
summary
end
end
As you can see from the code above, there is quite a bit of redundancy within the methods. Regardless of whether metaprogramming is the best way to reduce this redundancy, I am hoping to learn how to use metaprogramming in Ruby to reduce some of the repetitiveness here if someone could provide some suggestions.