Fork, clone, branch (training), and bundle install.
By the end of this lesson, students should be able to:
- Add Comparable operators to a class.
- Add Enumerable methods to a class.
We'll explore an important Ruby mechanism for adding behavior to a class: mixins.
The Comparable module provide
common operators to a class that implements the <=>
(spaceship) operator.
Let's look at lib/card.rb
.
How do you compare cards?
In your squads create an algorithm to determine which of two cards, if either, is "greater" than the other.
Adding the spaceship operator to Card
.
Let's simulate Enumerable methods using a deck of cards. In your squad, one of you will act as the method and another as the block. The third squad member will record the result.
Let's explore the start of writing a card game in Ruby using lib/card.rb
and
lib/deck.rb
.
We'll build our own list
using Ruby's Enumerable module.
We'll build a new range class that increments by a provided value.
The key to creating an Enumerable
class is a correct implementation of the
each
method.
Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.