2016-08-09 193 views
0

我有我的汽車模型下面的方法,像這樣:紅寶石調用方法

class Vehicle < ActiveRecord::Base 
    def get_history(start_time, end_time) 
    #... 
    end 

    def gsm_get_history(start_time, end_time) 
    #... 
    end 
end 

get_history被稱爲遍佈代碼庫,我定義gsm_get_history方法是非常相似的,接受相同的參數。

我該如何在不改變我的代碼庫中這個方法調用的每個實例的情況下繼續調用get_history?並調用gsm_get_history只有在車輛上的通信信道是:gsm

回答

1

變化get_history看起來像這樣:

def get_history(start_time, end_time) 
    if communication_channel == :gsm 
    return gsm_get_history(start_time, end_time) 
    end 

    # ... 
end 
0

你可能帶有短路的get_history方法

class Vehicle < ActiveRecord::Base 
    def get_history(start_time, end_time) 
    return gsm_get_history(start_time, end_time) if communication_channel == :gsm 
    #... 
    end 

def gsm_get_history(start_time, end_time) 
    #... 
    end 
end