Coder Social home page Coder Social logo

ruby-enumerable-custom's Introduction

General Assembly Logo

Ruby Enumerable and Comparable

Instructions

Fork, clone, branch (training), and bundle install.

Prerequisites

Objectives

By the end of this lesson, students should be able to:

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

Introduction

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

The Comparable Module

The Comparable module provide common operators to a class that implements the <=> (spaceship) operator. Let's look at lib/card.rb.

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

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.

Cards in Ruby

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

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.

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

ruby-enumerable-custom's People

Watchers

James Cloos avatar Jonathan Janulewicz 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.