4

我是新手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>'

我只想Comparison在 Ruby 中的对象之间实现

4

3 回答 3

8

来自Ruby Doc 模块 Comparable

Comparable 使用 <=> 来实现常规的比较运算符(<、<=、==、>= 和 >)以及 between? 方法。

当你想实现这样的事情时,只实现<=>. 其余的应该自动跟随。例如,如果你定义<,你会搞砸类。

你可以这样做:

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
于 2012-12-20T13:57:17.473 回答
2

您只需要按照以下方式进行:

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

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

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

于 2012-12-20T14:02:41.237 回答
0

对您的原始帖子发表评论:当您收到类似的消息时

在“>”中:nil:NilClass (NoMethodError) 的未定义方法“>”

只需放置打印语句来显示值。因此您会立即发现 in initialize,self.grades[ self.grade ]正在返回 nil,因为 in 参数Grade.new("A+")是一个字符串,但哈希中的键是符号,因此您需要使用to_sym.

您的原始课程重新排列,带有打印语句(并且仅显示 >(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}> 的技巧,用 <> 或 $$ 包围内插值使得当值为零时显示更加明显。
还要注意 ruby​​ -w t.rb 中的 -w,显示有趣的警告消息。

于 2012-12-20T18:20:23.297 回答