2016-08-19 54 views
1

我在我的程序中有一個Team類,我試圖使用method_missing ,但是當方法不存在時不運行該函數,它給了我一個錯誤:「undefined method`老鷹隊的球隊:類(NoMethodError)」Method_missing not running when it should

我的代碼如下:

class Team 
    attr_accessor :cust_roster, :cust_total_per, :cust_name, :cust_best_player 
    @@teams = [] 
    def initialize(stats = {}) 
    @cust_roster = stats.fetch(:roster) || [] 
    @cust_total_per = stats.fetch(:per) 
    @cust_name = stats.fetch(:name) 
    @cust_best_player = stats.fetch(:best) 
    @@teams << self 

    end 
    def method_missing(methId) 
    str = methID.id2name 
    Team.new(roster:[], per: 0, name: str.uppercase, best: 0) 

    end 



    class <<self 
    def all_teams 
     @@teams 
    end 
    end 

end 
hawks = Team.hawks 
+1

你是不是指'hawks = Team.new.hawks'? 'Team.hawks'試圖調用不存在的類方法'hawks'。 –

+1

或者'def self.method_missing'? – ScottJ

+0

不行,因爲Team.new已經是一個函數,它不會運行method_missing –

回答

4

有許多與你的代碼的問題。讓我們一一瀏覽。

從文檔,

method_missing(*args) private Invoked by Ruby when obj is sent a message it cannot handle.

這裏messagemethod。在Ruby中,每當你調用一個對象的方法,你實際上send荷蘭國際集團一messageobject

爲了更好地理解這一點,試試這個在IRB外殼。

1+2 
=> 3 
1.send(:+,2) 
=> 3 

這裏1和2是Fixnum類的對象。您可以使用1.class進行確認。好的,回到你的問題。所以,應該在實例上調用method_missing方法。

team = Team.new 
team.hawks 

如果您嘗試了上面的代碼,你會得到一個錯誤說'fetch': key not found: :roster (KeyError)

你可以通過一個default value作爲第二個參數來fetch方法解決這個問題。與

def initialize(stats = {}) 
    @cust_roster = stats.fetch(:roster, []) 
    @cust_total_per = stats.fetch(:per, 0) 
    @cust_name = stats.fetch(:name, "anon") 
    @cust_best_player = stats.fetch(:best, "anon") 
    @@teams << self 

末更換你initialize方法

如果執行該腳本,你會得到一個stack level too deep (SystemStackError),因爲在這一行小錯字。

str = methID.id2name 

在方法定義,您收到的參數與methId的名字,但裏面你想打電話methID。與

str = methId.id2name 

修復它。如果你執行你的腳本,你會再次得到一個錯誤說undefined method uppercase for "hawks":String (NoMethodError)

這是因爲對字串沒有uppercase方法。您應該改用upcase方法。

Team.new(roster:[], per: 0, name: str.upcase, best: 0) 

,你應該是好去。

如需更多信息,請參閱http://apidock.com/ruby/BasicObject/method_missing

希望這有助於!

+0

哇感謝很多我不能相信我錯過了 –

+0

@AvyayVaradarajan,我已經更新了我的答案,以反映關於'大寫'方法。看看 –

+0

感謝所有的幫助 –

0
class Team 
    attr_accessor :cust_roster, :cust_total_per, :cust_name, :cust_best_player 
    @@teams = [] 
    def initialize(stats = {roster: [], per: 0, name: "", best: 0}) # I added the default values here. 
    @cust_roster = stats.fetch(:roster) 
    @cust_total_per = stats.fetch(:per) 
    @cust_name = stats.fetch(:name) 
    @cust_best_player = stats.fetch(:best) 
    @@teams << self 

    end 
    def method_missing(name, *args) 
    self.cust_name = name.to_s.upcase 
    end 

    class << self 
    def all_teams 
     @@teams 
    end 
    end 

end 

team_hawks = Team.new #=> create an instance of Team class, I renamed the object to avoid confusions. 
team_hawks.hawks  #=> called method_missing which assigned the cust_name variable to "HAWKS" 

team_hawks.cust_name #=> HAWKS, so cust_name is assigned to be hawks. This is to check if the assignment worked. 

希望這是你在找什麼。