Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An Automobile class uses these instance variables: @make, @model, @year, and @mi

ID: 3839621 • Letter: A

Question

An Automobile class uses these instance variables: @make, @model, @year, and @miles. The getters and setters for these variables are defined by
   attr_accessor :make, :model, :year, :miles
If a1 and a2 are two Automobile objects, a1 is defined to be greater than a2 if a1 has more miles on its odometer (shown by @miles) than a2. Which of the following properly defines the spaceship operator for the Automobile class?

Question 3 options:

def <=>(other)
   return @miles <=> @other
end

  

def <=>(other)
   return other.miles <=> self.miles
end

def <=>(other)
   return self.miles <=> other.miles
end

def space_ship(other)
   return other.miles <=> self.miles
end

def <=>(other)
   return @miles <=> @other
end

  

def <=>(other)
   return other.miles <=> self.miles
end

def <=>(other)
   return self.miles <=> other.miles
end

def space_ship(other)
   return other.miles <=> self.miles
end

Explanation / Answer

def <=>(other)
   return other.miles <=> self.miles
end

This properly defines the spaceship operator for the Automobile class.

This <=> operator will offer combined comparison in that it will :

Return 0 if values on either side are equal
Return 1 if value on the left is greater
Return -1 if the value on the right is greater

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote