Git Product home page Git Product logo

ruby-enumerable-comparable's Introduction

General Assembly Logo

Ruby Enumerable and Comparable

Prerequisites

Objectives

By the end of this talk, developers should be able to:

  • Add Comparable operators to a class.
  • Add Enumerable methods to a class.

Preparation

  1. Fork and clone this repository. FAQ
  2. Create a new branch, training, for your work.
  3. Checkout to the training branch.
  4. Install dependencies with bundle install.

Introduction

We'll explore an important Ruby mechanism for adding behavior to a class: mixins.

The Comparable Module

Lab - comparing cards

How do you compare cards?

In your squads create an algorithm to determine which of two cards, if either, is "greater" than the other.

Demo - A Card Model

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.

Lab - A list as a deck of cards

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.

Demo - A Deck Model

Let's explore the start of writing a card game in Ruby using lib/card.rb and lib/deck.rb.

Private methods

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

The Enumerable Module

We'll build our own list using Ruby's Enumerable module.

Code along - Stepped Range

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.

Tasks

Developers should run these often!

  • bin/rake nag (or bundle exec rake nag): runs code quality analysis tools on your code and complains.
  • bin/rake test (or bundle exec rake test): runs automated tests.

Additional Resources

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

ruby-enumerable-comparable's People

Contributors

raq929 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.