O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Comprehensive Ruby Programming

Book Description

This book will provide you with all of the tools you need to be a professional Ruby developer. Starting with the core principles, such as syntax and best practices, and up to advanced topics like metaprogramming and big data analysis.

About This Book

  • Provides the core skills required to become a Ruby programmer
  • Covers how to use the most popular Ruby Gem libraries
  • Includes details on regular expressions

Who This Book Is For

This is a complete course written from the ground up for beginners wanting to gain a solid understanding of the Ruby language. It starts at the beginning with how to install Ruby and work with it on multiple machines, so simply have a computer that's connected to the Internet and you'll be ready.

What You Will Learn

  • Learn how to use Ruby code effectively, picking the right tool for the job and not duplicating built-in functionality
  • Gain best software development practices, and how to identify and fix common errors
  • Absorb core programming skills, such as variables, strings, loops, conditionals, and much more
  • Explore object-oriented programming and learn to create modular, reusable code that you can use across projects
  • Build 10 practical Ruby programs as you work through the book on topics such as big data analysis and solving Euler equations

In Detail

Ruby is a powerful, general-purpose programming language that can be applied to any task. Whether you are an experienced developer who wants to learn a new language or you are new to programming, this book is your comprehensive Ruby coding guide. Starting with the foundational principles, such as syntax, and scaling up to advanced topics such as big data analysis, this book will give you all of the tools you need to be a professional Ruby developer. A few of the key topics are: object-oriented programming, built-in Ruby methods, core programming skills, and an introduction to the Ruby on Rails and Sinatra web frameworks. You will also build 10 practical Ruby programs.

Created by an experienced Ruby developer, this book has been written to ensure it focuses on the skills you will need to be a professional Ruby developer. After you have read this book, you will be ready to start building real-world Ruby projects.

Style and approach

This is a comprehensive course for learning the Ruby programming language that works methodically through everything that you need to know. It begins with the basics of the language and then works through some complete projects to apply your skills and ensure that you have fully absorbed them and can use them in the real world.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book 
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Introduction to the Ruby Programming Language
    1. What is Ruby?
    2. Popular sites that use Ruby
    3. Installing Ruby on a computer
      1. Ruby language dashboard
    4. Installing different Ruby versions with RVM
    5. Summary
  3. Ruby Variables
    1. Using variables in Ruby
      1. Variable code implementation
    2. Printing to the Ruby console
      1. Using puts
      2. Using p
    3. Getting input from the Ruby console using gets and chomp
      1. Code implementation
    4. Variable scope and variable types
      1. Local variables
      2. Global variables
      3. Instance variables
      4. Constants
      5. Class variables
    5. Summary
  4. Ruby Strings
    1. Using strings in Ruby
      1. Missing quotation marks
      2. Printing strings
      3. Storing strings in variables
    2. String interpolation guide for Ruby
      1. What is string interpolation?
    3. String manipulation guide
      1. What is string manipulation?
        1. String manipulation code examples
        2. Method chaining
    4. A practical implementation
      1. Give it a bang!
    5. String substitution guide
      1. Adding a bang
      2. Becoming a block head
    6. Split, strip, and join guides for strings
      1. Using the strip method
      2. Using the split method
      3. Using the join method
    7. Summary
  5. Working with Numbers in Ruby
    1. Integer arithmetic guide
    2. Arithmetic order of operations
    3. Difference between integers and floats
    4. Summary
  6. Ruby Methods
    1. Creating methods in Ruby
    2. What does a Ruby method return?
    3. What are the differences between class and instance methods in Ruby?
    4. Ruby proc tutorial
      1. Proc code example
      2. What is a block?
    5. The Ruby lambda tutorial
      1. Stabby lambdas
    6. What is the difference between procs and lambdas in Ruby?
      1. Argument count
      2. Return behavior
    7. Guide to method arguments in Ruby
      1. What are method arguments?
      2. Method argument syntax
        1. Method argument code examples
      3. Named arguments
      4. Default argument values
    8. Using splat and optional arguments in Ruby methods
      1. Traditional splat arguments
      2. Keyword-based splat arguments
      3. Optional arguments
    9. Summary
  7. Ruby Iterators and Loops
    1. The while loop guide
      1. The while loop code example
    2. The each iterator method tutorial
      1. The each loop code example
    3. The for...in loops tutorial
    4. Looping over a hash
      1. Nested iterator code example
    5. Using the select method
    6. Using the map method – part 1
      1. The map method code example
    7. Using the map method – part 2
    8. Summing values in an array using the inject method
    9. Summary
  8. Ruby Collections
    1. Using arrays
    2. Deleting items from arrays
    3. Using push and pop for arrays
    4. The Ruby hash tutorial
      1. The hash code example
    5. Deleting elements from a hash
    6. Iterating over a hash for a key or value
    7. Top hash methods in Ruby
      1. Adding elements to a hash
      2. Swapping keys and values
      3. Merging hashes
      4. Converting a hash into an array
      5. Listing all keys
      6. Listing all values
    8. Summary
  9. Ruby Conditionals
    1. The conditionals guide
      1. Real-world use of conditionals
      2. Code example of conditionals
    2. Syntax for the unless conditional
      1. Running Ruby files
      2. The unless conditional code example
    3. Nested if...else conditionals
      1. The if...elsif conditional code example
      2. The conditional workflow
    4. Guide to compound conditionals
      1. Compound conditionals code example
      2. Compounded compound conditionals
      3. Additional conditional operators
    5. Summary
  10. Object-Oriented Programming in Ruby
    1. Introduction to how OOP works
      1. A real-world OOP example
    2. Ruby OOP development – setters, getters, and methods
    3. Working with instantiation in Ruby
      1. What does instantiation mean? – A real-world example
      2. Connecting the dots
      3. Instantiation code example
      4. Implementing instantiation
    4. Creating an initializer method in a Ruby class
      1. Adding an initializer to a Ruby class
      2. Working with optional values
      3. Using named arguments
      4. Overriding default values
    5. The working of OOP inheritance
    6. Overview of private versus public methods
      1. Private versus public methods
      2. Private method code example
    7. The working of polymorphism and usage of super
      1. So how does polymorphism fit in?
    8. Dead simple OOP
      1. My history with OOP
      2. How does OOP work?
    9. SOLID OOP development – the simple responsibility principle
      1. The single responsibility principle
      2. The SRP in the real world
      3. An SRP example
        1. The class that knew too much
        2. Rule of thumb – no ands allowed
        3. A mailer class
        4. The sales tax class
      4. Why the SRP is important
    10. SOLID OOP development – the open/closed principle guide and example
      1. The open/closed principle definition
      2. A surprisingly challenging task
      3. The open/closed principle example
        1. The naive approach
        2. I don't like change!
      4. A better way
    11. SOLID OOP development – the Liskov substitution principle
      1. The LSP definition
      2. Breaking down the LSP
      3. The LSP example
      4. The problem
      5. The LSP violation
      6. The fix
    12. SOLID OOP development – the interface segregation principle
      1. The ISP definition
      2. The ISP code example
      3. Introducing the moderator
      4. A better way
      5. The result
      6. A caveat
    13. SOLID OOP development – the dependency inversion principle
      1. The DIP in the real world
      2. The DIP definition
      3. The DIP code example
      4. Recap
    14. Summary
  11. Working with the Filesystem in Ruby
    1. Creating a file
      1. Ruby File class
        1. Other options you can pass as the second option
    2. Reading files into a program using the File class
    3. Deleting a file
    4. Appending a file
      1. Building a log file
    5. Summary
  12. Error Handling in Ruby
    1. Error handling tutorial
    2. Error handling – the best practices
    3. Developing a custom error logger in Ruby
    4. Summary
  13. Regular Expressions in Ruby
    1. Regular expression code example
      1. Regex code example
    2. How to validate an IP address using regular expressions
    3. How to develop a wheel of fortune game
    4. Summary
  14. Searching with grep in Ruby
    1. How to use grep in Ruby
    2. How to use grep instead of the select and map methods
    3. Summary
  15. Ruby Gems
    1. Introduction to Ruby gems
    2. How to research Ruby gems
      1. How to research gems
    3. Summary
  16. Ruby Metaprogramming
    1. Metaprogramming introduction
    2. How to integrate metaprogramming techniques into a custom class
    3. How to use metaprogramming for opening and customizing the String class
    4. How Rails use metaprogramming for the find_by method
    5. Utilizing metaprogramming with method_missing to create methods on the fly
      1. Why do we need to call super?
        1. Resources
    6. Incorporating respond_to_missing to conform to metaprogramming best practices
      1. Resources
    7. Implementing metaprogramming with define_method
      1. Resources
    8. Summary
  17. Ruby Web Frameworks
    1. Sinatra web framework development
    2. Introduction to the Ruby on Rails framework
    3. Summary
  18. Working with APIs in Ruby
    1. How to work with APIs in Ruby
    2. How to use the httparty Ruby gem
    3. How to create a custom API connector in Ruby
    4. How to parse an API in Ruby
    5. Summary
  19. Ruby Algorithms
    1. Introduction to sorting algorithms
      1. Bubble sort
      2. Quick sort
      3. Merge sort
    2. Implementing the bubble sort algorithm
      1. Bubble sort code example
    3. Implementing the quick sort algorithm
      1. Explanation of quick sort
      2. Quick sort code example
    4. Implementing the merge sort algorithm
    5. Implementing a prime number counting algorithm
    6. How to code the power digit sum algorithm
    7. Implementing a humanize counting algorithm
    8. Implementing a date algorithm
    9. How to code a Fibonacci digit counter
    10. Implementing a permutation algorithm
    11. Implementing an amicable number algorithm
    12. Implementing a factorial algorithm
    13. Implementing an even Fibonacci number algorithm
    14. Implementing the least common multiple
    15. Summary
  20. Machine Learning
    1. Big data analysis
    2. Basic decision tree implementation
      1. Addition of attributes
      2. Addition of training data and its values
      3. Calling the ID3 method
      4. Function of the train method
      5. Addition of the test method
      6. Setting up decision variables
      7. Print the results out
      8. Testing the program
      9. Advantage of decision trees over if...else statements
    3. How to develop a big data analysis application in Ruby
    4. Summary