I'm new to Ruby
, and I'm trying to implement a comparison between Grades
, was shown in the example
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]
So, I'm getting:
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>'
I just want to implement Comparison
between objects in Ruby
From Ruby Doc module Comparable :
Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method between?.
When you want to implement such thing, only implement <=>
. The rest should automatically follow. If you define <
, for example, you will mess up the class.
You can do something like this:
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
You just need to go as following:
class Foo
include Comparable
attr_reader :bar
def initialize bar
@bar = bar
end
def <=>(another_foo)
self.bar <=> another_foo.bar
end
end
So at the <=>
's definition you can add your own logic.
Comment to your original post : when you have a message like
in '>': undefined method '>' for nil:NilClass (NoMethodError)
just put print statements to display the values. Thus you would have immediately found that in initialize
, self.grades[ self.grade ]
was returning nil, because the parameter in Grade.new("A+")
is a String, but the keys in the hash are symbols, so you need to convert with to_sym
.
Your original class rearranged, with print statements (and only showing >(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
Execution :
$ 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")
: as A does not exist in the hash, the instance variable @grade is not set, and self.grades[ self.grade ] > ...
sends the message >
to nil, an instance of NilClass which doesn't define >
.
Notice the trick @grades=<#{xyz}>, surrounding the interpolated value with <> or $$ makes the display more obvious when the value is nil.
Note also the -w in ruby -w t.rb, displaying interesting warning messages.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.