By the end of this talk, developers should be able to:
- Add Comparable operators to a class.
- Add Enumerable methods to a class.
- Fork and clone this repository. FAQ
- Create a new branch,
training
, for your work. - Checkout to the
training
branch. - Install dependencies with
bundle install
.
We'll explore an important Ruby mechanism for adding behavior to a class: mixins.
How do you compare cards?
In your squads create an algorithm to determine which of two cards, if either, is "greater" than the other.
The Comparable module provide
common operators to a class that implements the <=>
(spaceship) operator.
Let's look at lib/card.rb
.
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
.
It's a best practice to keep our exposed API as small as necessary. I like to
keep methods private by default (just like data is) by decorating them with
the private
method. This makes them uncallable outside the class definition.
For example:
class Foo
def bar
"baz"
end
private :bar
def qux
bar # this works
end
end
Foo.new.bar # this does not work
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.
Developers should run these often!
bin/rake nag
(orbundle exec rake nag
): runs code quality analysis tools on your code and complains.bin/rake test
(orbundle exec rake test
): runs automated tests.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.