2015-11-11 42 views
1

由於某些歷史原因,在Rails項目中,TigerElephant中的一段代碼是相同的。Ruby'返回'從另一種方法的方法?

我不喜歡重複,但如果我在AnimalController類中創建一個新的方法和移動這些代碼了進去,我不能return來自新方法walkrunning方法。

我認爲從另一種方法返回可能不是一個好的做法,但我真的很討厭重複,有人可以幫助我重構嗎?

class AnimalController 
    # I want create a new method here 
    #def all_in 
    #end 
end 

class TigerController < AnimalController 
    def running # This is an Action 
    some_different_codes... 

    if arm.blank? 
     render_not_found 
     return  # <- how can I return `running` from the new method? 
    end 
    if lag.nil? 
     invalid_id 
     return  # <- 
    end 

    some_different_codes... 
    end 
end 

class ElephantController < AnimalController 
    def walk  # This is an Action 
    some_different_codes... 

    if arm.blank? 
     render_not_found 
     return 
    end 
    if lag.nil? 
     invalid_id 
     return 
    end 

    some_different_codes... 
    end 
end 

回答

2

如果方法不想讓調用方返回,則方法無法返回。所以這個新方法將執行檢查(同時呈現某些內容),並且它將返回檢查的結果。調用方法分析返回值並決定要做什麼。沿着這些路線的東西:

class AnimalController 
    def all_in 
    if invalid_id 
     render_not_found 
     return false 
    end 

    if lag.nil? 
     invalid_id 
     return false 
    end 

    true 
    end 
end 

class TigerController < AnimalController 
    def running # This is an Action 
    some_different_codes... 

    return unless all_in 

    some_different_codes... 
    end 
end 
0

可能是一個好事來查找關於callbackssuperclassing


回調基本上允許你在代碼基於另一個函數的響應運行登錄。

因爲我們把它們放在Rails上,所以沒有多少人真正瞭解它們的功能。如果你曾經用JS實現它們,你就會知道它們的全部!

-

超類是你從現有的類繼承 - 允許你使用(&擴展)的類具有的功能。這是命令來自的命令。


我會做到這一點(貌似Sergio的回答實際上,這是令人欣慰):

#app/controllers/animals_controller.rb 
class AnimalsController < ApplicationController 

    private 

    def all_in? 
     if invalid_id 
      return false 
     end 
     if lag.nil? 
      invalid_id 
      return false 
     end 
     true #-> ruby automatically returns the last line 
    end 
end 

以上是你叫什麼回調 - 你會能夠呼叫all_in(在一個實例中)並且接收truefalseresponse

這會給你調用該方法(如果你superclassing,該方法將可環比下滑的能力:

#app/controllers/elephants_controller.rb 
class ElephantController < AnimalController 
    def walk  # This is an Action 
    some_different_codes... 

    if all_in? 
     some_different_codes... 
    end 
    end 
end 

現在,有些事情你必須知道的。

這種類型的行爲應該被放在控制您的應用程序的 - 它應該是在你的模式

#app/models/animal.rb 
class Animal < ActiveRecord::Base 
    def walk 
    end 
end 

#app/models/animals/elephant.rb 
class Elephant < Animal 
    def walk 
     super ... 
    end 
end 

#app/models/animals/tiger.rb 
class Tiger < Animal 
end 

上面被稱爲STI (Single Table Inheritance)。它基本上是一種將其主要模型與其他「依賴」模型&進行分類的方法。

因爲ruby是​​,所以應該在對象本身上調用對象特定的方法;

#config/routes.rb 
resources :tigers, :elephants, controller: :animals 

#url.com/tigers/ 
#url.com/elephants/ 

#app/controllers/animals_controller.rb 
class AnimalsController < ApplicationController 
    def show 
    @tiger = Tiger.find params[:id] 
    @tiger.walk 
    end 
end 

這或多或少縮影與state machine

class Vehicle 
    attr_accessor :seatbelt_on, :time_used, :auto_shop_busy 

    state_machine :state, :initial => :parked do 
    before_transition :parked => any - :parked, :do => :put_on_seatbelt 

    after_transition :on => :crash, :do => :tow 
    after_transition :on => :repair, :do => :fix 
    after_transition any => :parked do |vehicle, transition| 
     vehicle.seatbelt_on = false 
    end 

... 
相關問題