The Ruby Workshop

Book description

Get to grips with the fundamentals of the Ruby programming language and learn how to build your own applications with the help of real-world examples and hands-on activities

Key Features

  • Learn the fundamentals of Ruby object-oriented programming (OOP)
  • Use the Ruby on Rails framework to build interactive web applications
  • Discover how to quickly build complex programs with fewer lines of code

Book Description

The beauty of Ruby is its readability and expressiveness. Ruby hides away a lot of the complexity of programming, allowing you to work quickly and 'do more' with fewer lines of code. This makes it a great programming language for beginners, but learning any new skill can still be a daunting task. If you want to learn to code using Ruby, but don't know where to start, The Ruby Workshop will help you cut through the noise and make sense of this fun, flexible language.

You'll start by writing and running simple code snippets and Ruby source code files. After learning about strings, numbers, and booleans, you'll see how to store collections of objects with arrays and hashes. You'll then learn how to control the flow of a Ruby program using boolean logic.

The book then delves into OOP and explains inheritance, encapsulation, and polymorphism. Gradually, you'll build your knowledge of advanced concepts by learning how to interact with external APIs, before finally exploring the most popular Ruby framework ? Ruby on Rails ? and using it for web development.

Throughout this book, you'll work on a series of realistic projects, including simple games, a voting application, and an online blog. By the end of this Ruby book, you'll have the knowledge, skills and confidence to creatively tackle your own ambitious projects with Ruby.

What you will learn

  • Master the syntax and features of Ruby to build useful applications
  • Use common design patterns to simplify code and improve efficiency
  • Understand how to implement object-oriented programming with Ruby
  • Explore ways to fetch, process, and output data
  • Work with public APIs and create reusable RubyGems
  • Debug code to troubleshoot application behavior
  • Create interactive web applications with Ruby on Rails

Who this book is for

The Ruby Workshop is designed for anyone who is new to Ruby and wants a practical introduction to the language. Whether you're completely new to programming, or have experience in another language and want to broaden your skillset, this book will quickly get you up and running.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. About the Book
      1. About the Chapters
      2. Conventions
      3. Before You Begin
      4. Installing Ruby 2.6
      5. Installing the Code Bundle
  2. 1. Writing and Running Ruby Programs
    1. Introduction
      1. Key Features of Ruby
        1. Object-Oriented
        2. Interpreted Language
        3. Duck Typing and Dynamic Typing
        4. Multi-paradigm Language
        5. Reflection
        6. Metaprogramming
    2. Interactive Ruby Shell (IRB)
      1. Exercise 1.01: Creating and Assigning Variables
      2. Exercise 1.02: Assigning a Variable of One Data Type to a Different Type
      3. Exercise 1.03: Getting the Type of a Variable
      4. Getting the Details of the Public Methods of an Object
      5. Running Ruby Code from Ruby Files
      6. Exercise 1.04: Getting User Input in a Ruby Program
    3. Standard Data Types
      1. Number
      2. Exercise 1.05: Performing Common Integer Operations
      3. Exercise 1.06: Using Common Integer Methods to Perform Complex Arithmetic
      4. Floating-Point Numbers
      5. Exercise 1.07: Performing Common Operations for Floating-Point Numbers
      6. String
      7. Exercise 1.08: Using Common String Methods
      8. Exercise 1.09: Performing String Concatenation
      9. Exercise 1.10: Performing String Interpolation
      10. Exercise 1.11: Extracting and Searching a Substring from a String
      11. Exercise 1.12: Replacing Part of a String with Another String
      12. Exercise 1.13: Replacing All the Values inside a String Using gsub
      13. Exercise 1.14: Splitting a String and Joining a String
      14. Activity 1.01: Generating Email Addresses Using Ruby
      15. Boolean
      16. Activity 1.02: Calculating the Area and Perimeter of a Candy Manufacturing Plant
    4. Summary
  3. 2. Ruby Data Types and Operations
    1. Introduction
    2. Arrays
      1. Iterating Through an Array
      2. Operations on Arrays
        1. Merging Two Arrays
        2. Removing Occurrences of Similar Elements from an Array
        3. Inserting Elements into an Array at the End
        4. Finding the Last Element of an Array without Modifying It
        5. Finding the Last Element of an Array and Removing it
      3. Exercise 2.01: Performing Simple Operations on Arrays
        1. Creating an Array That Cannot Be Modified
        2. Finding All the Unique Elements in an Array
        3. Sorting the Elements of an Array
        4. Finding the Number of Elements in an Array
        5. Establishing Whether an Element Exists in an Array (Case-Sensitive)
        6. Converting Elements of an Array into a String
      4. Exercise 2.02: Performing Complex Operations on Arrays
    3. Hashes
      1. Operations on Hashes
        1. Getting Values from the Hash
        2. Sorting a Hash
        3. Merging Hashes
        4. Retrieving Keys or Values from a Hash
        5. Deleting a Value from a Hash by Key
        6. Removing or Rejecting Elements from a Hash
        7. Establishing whether a Hash Contains a Particular Value
      2. Exercise 2.03: Converting a Time String to a Hash
    4. Ruby Methods
      1. Passing Arguments to a Method
      2. Ruby Methods with Default Values
      3. Return Value(s) from Methods
      4. Naming Conventions for Methods
      5. Activity 2.01: Dice Roller Program
    5. Summary
  4. 3. Program Flow
    1. Introduction
    2. Boolean Operators
      1. The AND Operator
        1. The OR Operator
        2. The NOT Operator
      2. Truth Tables
      3. Truthiness
      4. Precedence
      5. Exercise 3.01: Implementing Boolean Operators
      6. Conditional Expressions
        1. The if Statement
        2. The else Statement
        3. The elsif Statement
        4. The unless Statement
    3. Comparison
      1. Comparison Operators
      2. Comparing Strings
      3. Exercise 3.02: Creating a Method to Check Your Balance
      4. The case/when Statement
      5. The === Operator
    4. The Ternary Operator
      1. Exercise 3.03: Speed Determiner
    5. Loops
      1. The while/do Loop
      2. Exercise 3.04: Developing a Mind Reader
      3. The until/do Loop
      4. The do/while Loop
      5. Iterators and Enumerators
        1. The each Method
        2. The each_with_index Method
        3. The map/collect Loop
      6. Exercise 3.05: Developing an Arbitrary Math Method
      7. Activity 3.01: Number-Guessing Game
    6. Summary
  5. Ruby Methods
    1. Introduction
    2. The Basic Structure of the Ruby Method
      1. Method Components: Signature and Implementation
      2. The Method Signature
      3. Method Arguments
      4. Positional Arguments
      5. Variable Scope
      6. Optional Parentheses and Code Style
      7. Mandatory and Optional Arguments
      8. Keyword Arguments
    3. Return Values
      1. Multiple Return Values
      2. Exercise 4.01: Performing Operations on an Array
    4. The Splat Operator
      1. The Single Splat (*) Operator
        1. The Double Splat (**) Operator
      2. Exercise 4.02: Creating a Method to Take Any Number of Parameters
    5. Duck Typing
    6. Sending a Message
      1. Using Built-In Modules: Time and Math
      2. Math
      3. Exercise 4.03: Using the Math Library to Perform Mathematical Operations
      4. Time
      5. Exercise 4.04: Performing Method Operations with the Time Module
      6. Activity 4.01: Blackjack Card Game
    7. Summary
  6. 5. Object-Oriented programming with Ruby
    1. Introduction
    2. Classes and Objects
      1. Instance Methods
    3. Getters and Setters
      1. Exercise 5.01: Modeling a Company's Organizational Chart Using Classes
      2. Class Methods
        1. Method 1: Calling a Method Directly on the Class
        2. Method 2: Using the self Keyword
      3. Class Variables
      4. Exercise 5.02: Generating URLs for Sharing Content on Social Platforms
    4. Inheritance
      1. The Inheritance of Methods
      2. The Inheritance of Variables
      3. Calling super
      4. The Different Ways to Call super
        1. Calling super without Arguments
        2. Calling super with Arguments
        3. Calling super with the Naked Splat Operator
      5. Exercise 5.03: Modelling a Class for Location Addresses
    5. Encapsulation
      1. Public Methods
        1. Private Methods
        2. Protected Methods
      2. Exercise 5.04: Demonstrate the Visibility Rules of Ruby Methods
      3. Bypassing Visibility
        1. A Quick Note about File Organization
      4. Activity 5.01: Voting Application for Employee of the Month
    6. Summary
  7. 6. Modules and Mixins
    1. Introduction
    2. Including Modules
      1. Exercise 6.01: Controlling the Operations of Services in an Application
      2. Inheritance with Module Methods
      3. Inclusion Ordering
    3. extend
      1. Exercise 6.02: Extending Your Modules with Class and Instance Methods
      2. Module Callbacks
      3. Exercise 6.03: Using Module Functions to Extend Classes
      4. Importing Modules as Class Methods using Extended
      5. Combining include and extend
      6. Enumerated Types
      7. Exercise 6.04: Writing a Module to Define Payment Methods for a Product Application
    4. Module Methods
    5. Namespaces
      1. Exercise 6.05: How to Reuse Constants with a Namespace
    6. prepend
      1. prepend with Subclasses
      2. Exercise 6.06: Prepending Modules to Classes
      3. Activity 6.01: Implementing Categories on the Voting Application Program
    7. Summary
  8. 7. Introduction to Ruby Gems
    1. Introduction
    2. RubyGems and the require Method
      1. gem search
        1. gem install
        2. gem dependency
        3. gem Versioning
        4. gem list
    3. Using Gems in Your Code
      1. Exercise 7.01: Installing and Using a Ruby Gem
    4. File I/O
      1. Creating Files
        1. Reading Files
        2. Using the File.read Method
        3. Using the File.open Method
        4. Using the File.foreach Method
        5. read versus open versus foreach
        6. Writing to Files
        7. Using the File.new Method (Initializer)
        8. Using the File.open Method with a Block Method
        9. Using the File.write Method
        10. File.open versus File.new
      2. File Modes
      3. File Helpers
    5. Handling CSV Data
      1. Reading Data from a CSV File Using CSV.read
      2. Using Column Headers as Field Names
      3. Exercise 7.02: Reading Data from a .csv File and Printing a Few Columns
      4. Reading Data from a .csv File Using CSV.foreach
      5. Response Type Variations
    6. Writing Data
      1. Writing CSV Data from an Enumerable
      2. Exercise 7.03: Writing an Array of Users to a CSV File
    7. Service Objects
      1. The Single Responsibility Principle
      2. Service Objects versus Modules
      3. Class Method Execution
      4. Exercise 7.04: Building a Service Object
      5. Activity 7.01: Presenting Voting Data in CSV Format Using Ruby Gems
    8. Summary
  9. 8. Debugging with Ruby
    1. Introduction
    2. Logging and Debugging
      1. Logging with the logger Class
    3. Basic Logging
      1. Log Levels
      2. Exercise 8.01: Logging User Information
      3. Exercise 8.02: Creating a Basic Exception Logger
      4. Setting Log Levels by String
      5. Log Output
        1. STDOUT
        2. STDERR
        3. File
      6. Log Formatting
        1. Custom Date Formats
        2. Custom String Formats
        3. Time Zones and UTC
      7. Log File Management
        1. Size-Based Retention
        2. Time-Based Retention
      8. Exercise 8.03: Creating a Rolling Log File
    4. Debugging
      1. Debugging Concepts
        1. Breakpoints
        2. Stack Trace/Call Stack/Back Trace
        3. Viewing the Stack Trace
        4. StepOver, StepInto, and StepOut
      2. Debugging via the CLI with byebug
      3. Conditional Debugging
      4. Navigating with byebug
      5. Exercise 8.04: Debugging with byebug
      6. Debugging with Visual Studio Code
      7. Setting Breakpoints
      8. Logpoints
      9. The Debug Console
      10. Exercise 8.05: Debugging with Visual Studio Code
      11. Activity 8.01: Perform Debugging on a Voting Application
    5. Summary
  10. 9. Ruby Beyond the Basics l
    1. Introduction
    2. Metaprogramming
      1. Blocks
      2. Syntax for Blocks
        1. With do/end
        2. With curly brackets ({})
      3. yield with Blocks
      4. Exercise 9.01: Building a Simple Calculator
      5. block_given? with Blocks
      6. Exercise 9.02: Building a Flight Status Display System
      7. The proc Object
      8. Exercise 9.03: Performing the sum Function on a Range of Numbers
      9. Exercise 9.04: Calculating Profit Using proc on an Input Price List
      10. Lambdas
      11. Exercise 9.05: Creating a Program to Sum a Range of Numbers Using Lambdas
      12. proc versus lambda
      13. The Story of the Chef and the Restaurant
      14. Activity 9.01: Invoice Generator
    3. Summary
  11. 10. Ruby Beyond the Basics ll
    1. Introduction
    2. Metaprogramming – A Deep Dive
      1. Opening Classes
      2. Exercise 10.01: Opening the Ruby String Class to Add a New Method to it
      3. Monkey Patching
      4. Exercise 10.02: Using Monkey Patching to Add/Subtract Values
      5. method_missing
      6. Exercise 10.03: Managing Ghost Methods for the Company Class
      7. The Define Method
      8. Exercise 10.04: Creating a Welcome Message Program Using define_method
    3. HTTP Requests
      1. HTTP Requests with Ruby
      2. Exercise 10.05: Creating a Ruby Program to Make a Successful GET Request
      3. Status Codes
      4. Exercise 10.06: Creating a POST Request Using Ruby
    4. Creating a Ruby Gem
      1. Exercise 10.07: Creating Your Own Ruby Gem
      2. Activity 10.01: Implementing GET and POST Data to an External Server
    5. Summary
  12. 11. Introduction to Ruby on Rails l
    1. Introduction
      1. MVC
        1. Model
        2. View
        3. Controller
    2. Generating Our First Rails App
      1. Exercise 11.01: Generating a Simple Rails Application
    3. Anatomy of a Rails application
      1. Exercise 11.02: Replacing the Default Page
      2. Exercise 11.03: Displaying a Page Heading from the Value Served from Its Action
      3. Extending Our View and Controller
      4. Exercise 11.04: Adding a Resource to the Rails Application
    4. Models, Migrations, and Databases
      1. Exercise 11.05: Creating a Review Model for citireview
    5. The Rails Console
      1. Exercise 11.06: Completing the Reviews Web Application
        1. The devise Ruby Gem
      2. Activity 11.01: Adding Authentication for the Review Application
    6. Summary
  13. 12. Introduction to Ruby on Rails ll
    1. Introduction
    2. Associations
      1. belongs_to
      2. has_one
      3. has_many
      4. has_many :through
      5. has_one :through
      6. has_and_belongs_to_many
      7. Exercise 12.01: Creating a Model to Enable Users to Comment on Reviews on the CitiReview Application
    3. Validations
      1. Database Validations
      2. Controller-Level Validations
      3. Client-Side Validations
      4. Model-Level Validations
      5. Methods that Trigger Validations
      6. Methods that Skip Validations
        1. Checking Validations
      7. Exercise 12.02: Adding Validation for the Citireview Application
      8. Scaffolding
      9. Exercise 12.03: Using Rails Scaffolding to Create a Rails Application for a Store with a List of Products
      10. Activity 12.01: Create a Blog Application and Host It Live on a Cloud Platform
    4. Summary
  14. Appendix
    1. 1. Writing and Running Ruby Programs
      1. Activity 1.01: Generating Email Addresses Using Ruby
      2. Activity 1.02: Calculating the Area and Perimeter for a Candy Manufacturing Plant
    2. 2. Ruby Data Types and Operations
      1. Activity 2.01: Dice Roller Program
    3. 3. Program Flow
      1. Activity 3.01: Number-Guessing Game
    4. 4. Ruby Methods
      1. Activity 4.01: Blackjack Card Game
    5. 5. Object-Oriented Programming with Ruby
      1. Activity 5.01: Voting Application for Employee of the Month
    6. 6. Modules and Mixins
      1. Activity 6.01: Implementing Categories on the Voting Application Program
    7. 7. Introduction to Ruby Gems
      1. Activity 7.01: Presenting Voting Data in CSV Format Using Ruby Gems
    8. 8. Debugging with Ruby
      1. Activity 8.01: Perform Debugging on a Voting Application
    9. 9. Ruby Beyond the Basics I
      1. Activity 9.01: Invoice Generator
    10. 10. Ruby Beyond the Basics II
      1. Activity 10.01: Implementing GET and POST Data to an External Server
    11. 11. Introduction to Ruby on Rails I
      1. Activity 11.01: Adding Authentication for the Review Application
    12. 12. Introduction to Ruby on Rails II
      1. Activity 12.01: Create a Blog Application and Host It Live on a Cloud Platform

Product information

  • Title: The Ruby Workshop
  • Author(s): Akshat Paul, Peter Philips, Daniel Szabo, Cheyne Wallace
  • Release date: October 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838642365