实现比较运算符 - Ruby

时间:2012-12-20 13:52:43

标签: 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

3 个答案:

答案 0 :(得分:8)

来自Ruby Doc module Comparable

  

可比较用途&lt; =&gt;实现传统的比较运算符(&lt;,&lt; =,==,&gt; =和&gt;)以及?之间的方法。

当你想要实现这样的事情时,只实现<=>。其余的应该自动跟随。例如,如果您定义<,则会弄乱该课程。

您可以这样做:

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

答案 1 :(得分:1)

您只需按照以下步骤操作:

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

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

因此,在<=>的定义中,您可以添加自己的逻辑。

答案 2 :(得分:0)

评论您的原始帖子:当您收到

之类的消息时
  

in&#39;&gt;&#39 ;:未定义的方法&#39;&gt;&#39; for nil:NilClass(NoMethodError)

只需输入print语句即可显示值。因此,你会立即发现在initialize中,self.grades[ self.grade ]返回nil,因为Grade.new("A+")中的参数是一个String,但哈希中的键是符号,所以你需要转换与to_sym

您的原始课程已重新排列,并附有印刷语句(仅显示&gt;(其他)):

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 ] > ...将消息>发送为零,NilClass的一个实例,它没有定义>

注意诀窍@ grades =&lt;#{xyz}&gt;,用&lt;&gt;围绕插值。当值为零时,或者$$使显示更明显 另请注意ruby -w t.rb中的-w,显示有趣的警告消息。