簡體   English   中英

實現比較運算符 - Ruby

[英]implementing the comparison operators - Ruby

我是Ruby新手,我正在嘗試實現Grades之間的比較,如示例所示

include Comparable

class Grade
        attr_accessor :grades, :grade

        def initialize( grade = "" )
                self.grades = { :nil => -1, :"F" => 0, :"D-" => 1, :"D" => 2, :"D+" => 3,
                                :"C-" => 4, :"C" => 5, :"C+" => 6, :"B-" => 7, :"B" => 8,
                                :"B+" => 9, :"A-" => 10, "A+" => 11 }
                if self.grades[ grade ]
                        self.grade = grade
                else
                        self.grade = nil

                end
        end

        def <=>( other )
                if self.grades[ self.grade ] < self.grades[ other.grade ]
                        return -1
                elsif self.grades[ self.grade ] == self.grades[ other.grade ]
                        return 0
                else
                        return 1
                end
        end
end

a_plus = Grade.new("A+")
a      = Grade.new("A")
[a_plus, a].sort # should return [a, a_plus]

所以,我得到了:

grade.rb:31:in `<': comparison of Fixnum with nil failed (ArgumentError)
    from grade.rb:31:in `<=>'
    from grade.rb:43:in `sort'
    from grade.rb:43:in `<main>'

我只想在Ruby中實現對象之間的Comparison

來自Ruby Doc模塊可比較

可比較使用<=>來實現傳統的比較運算符(<,<=,==,> =和>)和?之間的方法。

當你想實現這樣的事情時,只實現<=> 其余的應該自動跟隨。 例如,如果你定義< ,那么你將搞亂這個課程。

你可以這樣做:

class Grade
  include Comparable
  attr_reader :index
  @@grades = %w[F D- D D+ C- C C+ B- B B+ A- A+]
  def initialize (grade = nil); @index = @@grades.index(grade).to_i end
  def <=> (other); @index <=> other.index end
end

a_plus = Grade.new("A+")
a      = Grade.new("A")
a_plus > a
# => true
[a_plus, a].sort
# => `[a, a_plus]` will be given in this order

你只需要如下:

class Foo
  include Comparable
  attr_reader :bar
  def initialize bar
    @bar = bar
  end

  def <=>(another_foo)
    self.bar <=> another_foo.bar
  end
end

因此,在<=>的定義中,您可以添加自己的邏輯。

評論您的原始帖子:當您收到消息時

in'>':nil的未定義方法'>':NilClass(NoMethodError)

只需輸入print語句即可顯示值。 因此你會立即發現在initializeself.grades[ self.grade ]返回nil,因為Grade.new("A+")的參數是一個String,但是hash中的鍵是符號,所以你需要轉換為to_sym

您的原始類重新排列,帶有print語句(並且只顯示>(other)):

class Grade
    attr_reader :grade
    @@grades = { :nil  => -1, :"F"  =>  0, :"D-" =>  1, :"D"  => 2, :"D+" => 3,
                 :"C-" =>  4, :"C"  =>  5, :"C+" =>  6, :"B-" => 7, :"B"  => 8,
                 :"B+" =>  9, :"A-" => 10, :"A+" => 11 }

    def initialize( p_grade = "" )
        @grade = p_grade if @@grades[ p_grade.to_sym ]
        puts "init param=#{p_grade} value=<#{@@grades[ p_grade.to_sym ]}> @grades=<#{@grade}>"
    end

    def >( other )
        puts "in >( other ) : key1=#{self.grade} key2=#{other.grade}"
        puts "in >( other ) : $#{@@grades[ self.grade ]}$ ??? $#{@@grades[ other.grade ]}$"
        return @@grades[ self.grade ] > @@grades[ other.grade ]
    end
end

print '--- Grade.new("A+") : '; a_plus = Grade.new("A+")
print '--- Grade.new("A")  : '; a      = Grade.new("A")
print '--- a_plus > a : '; p a_plus > a 

執行:

$ ruby -w t.rb
--- Grade.new("A+") : init param=A+ value=<11> @grades=<A+>
--- Grade.new("A")  : t.rb:9: warning: instance variable @grade not initialized
init param=A value=<> @grades=<>
--- a_plus > a : in >( other ) : key1=A+ key2=
in >( other ) : $$ ??? $$
t.rb:15:in `>': undefined method `>' for nil:NilClass (NoMethodError)
    from t.rb:21:in `<main>'

Grade.new("A") :由於散列中不存在A ,因此未設置實例變量@grade,並且self.grades[ self.grade ] > ...將消息>發送到nil,一個實例NilClass沒有定義>

注意訣竅@grades = <#{xyz}>,用<>或$$圍繞內插值使得當值為nil時顯示更明顯。
還要注意ruby -w t.rb中的-w,顯示有趣的警告消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM