2015-10-20 65 views
-1
class LogEntry 
    attr_reader :term, :index, :command 

    def initialize(term, index, command) 
     @term, @index, @command = term, index, command 
    end 

    def ==(other) 
     [:term, :index, :command].all? do |attr| 
     self.send(attr) == other.send(attr) 
     end 
    end 

    def eql?(other) 
     self == other 
    end 

    def hash 
     [:term, :index, :command].reduce(0) do |h, attr| 
     h ^= self.send(attr) 
     end 
    end 
    end 

我正在阅读这样的代码,如何理解"self.send"在这一行?ruby​​ self.send如何理解

self.send(attr) == other.send(attr) 

感谢

+0

你看过红宝石DOC(http://ruby-doc.org/core-2.2.3/ Object.html#方法-I-发送) –

+0

重复:http://stackoverflow.com/q/3337285/2988 –

回答

3

貌似做的一个奇特的方式:

self.term == other.term and 
self.index == other.index and 
self.comment == other.comment