Comprehensive Ruby Programming
eBook - ePub

Comprehensive Ruby Programming

  1. 330 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Comprehensive Ruby Programming

About this book

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 expressionsWho This Book Is ForThis 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 equationsIn DetailRuby 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 approachThis 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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Object-Oriented Programming in Ruby

One of the defining attributes of the Ruby programming language, in addition to readability, is how it implements object-oriented programming (OOP) techniques. In this chapter, you'll learn OOP for Ruby, including creating classes, instantiating objects, working with inheritance, and polymorphism. Additionally, you'll be able to do the following:
  • Work with Ruby classes
  • Use getter and setter methods in Ruby
  • Demonstrate how to implement OOP techniques such as inheritance and polymorphism
  • Employ the SOLID design patterns to follow OOP best practices

Introduction to how OOP works

In this chapter, we are going to look into a fundamental concept called OOP. If you're coming from other OOP languages, you may be familiar with some of these concepts, but still it's important that you pay attention because Ruby embraces a very specific form of OOP compared with many other languages available today.
Before we begin, a key thing to remember is that pretty much of everything is an object in Ruby. We'll see more of it as we go. Also, I'll show some practical ways of using OOP, which hopefully will be more enlightening than the theoretical approach taught in most computer science books. In fact, teaching a practical application of OOP was one of my biggest motivators to create this course.
When I was studying development in college, I thought I learned OOP. However, what I learned in school did little to help me build real-world programs and I ended up having to relearn a practical approach to OOP as I taught myself how to code.

A real-world OOP example

Let's start by analyzing a Ruby on Rails project that I built for a client:
If you see the first line, I have a class called ApplicationController, which inherits from a class called ActionController::Base. The < symbol denotes inheritance in Ruby.
Make sure not to get the < symbol used for inheritance confused with the < symbol used with conditionals. The Ruby parser is smart enough to differentiate when you want to use one over the other.
What this code essentially means is that, ApplicationController has access to all the methods available in ActionController::Base. In this sense, the attributes and behavior of a class are determined largely by the class from which they inherit.
Now, there can be classes that inherit from ApplicationController. I'll open another file to show you how this works:
Here, the first line tells you that BranchesController is a class that inherits from the ApplicationController. Due to this inheritance, methods present in ApplicationController such as index and new can be accessed by BranchesController. The same applies to the before_action method too. I've not declared this method anywhere in BranchesController, yet I have access to it because it is present in ApplicationController. But wait! There was no mention of these methods inside of ApplicationController, so where did these methods come from? Don't worry, it's not coding black magic. These methods are passed down from ActionController::Base from which ActionController inherits.
If this is still fuzzy, let's think of a real-world example of inheritance. As humans, each of us have biological parents. Our parents passed down a set of genes to us that our bodies have access to. In this analogy, our parents are like the ActionController::Base class and we are the BranchesController class. The genes that our parents pass to us are like the methods that our BranchesController class has access to.
Let's take a look at another code example from the Rails project; let's go to the create method in the BranchController class:
If you see, in line 27, I create a new instance of the class called Branch and pass some custom parameters to it. I can access the methods of this Branch class through the branch instance I created here.
Now, if I go to the Branch class, you can see that it inherited from another class called ActiveRecord::Base:
By inheriting from the ActiveRecord::Base class, our Branch class will have access to a large number of methods that it can call when it needs them. The methods that our Branch class inherit allows it to do the following:
  • Communicate with the database
  • Implement data validations
  • Launch automated processes named callbacks
  • And much more
When you get into the Ruby on Rails application development, you'll see inheritance is used extensively.
If you've never worked with OOP before, this all may seem a bit overwhelming; however, don't let it scare you off. I wanted to start by showing you a practical example of how OOP works because I was tired of instructors who gave contrived OOP examples. I'd prefer for you to see what OOP does in a real application so you will realize how important it is.
In the next few guides, we will be building out our own application, which will be an API connector that can communicate dynamically with applications on the web. This will include walking through how to leverage concepts such as inheritance and object instantiation to make our code more scalable and reusable.

Ruby OOP development – setters, getters, and methods

Ruby utilizes a unique syntax for creating setters and getters in a class. In this guide, we will walk through how to implement these processes.
Creating a class is fairly simple in Ruby. It's so simple that it wasn't even worth dedicating an entire guide to it (I build courses just like I code, I despise wasting my time or yours for dead simple concepts).
To define a class simply type the class word followed by the name you want to give to your class, and end it with the end word. Anything contained between class and end belongs to this class.
Class names in Ruby have a very specific style requirement. They need to start with a letter and if they represent multiple words, each new word needs also to be an uppercase letter.
We'll start by creating a class called ApiConnector:
class ApiConnector
end
Now classes in Ruby can store both data and methods. But how can we define what data should be included? In many traditional OOP languages such as Java, you need to create two methods for each data element you want to be included in the class. One method, the setter, sets the value in the class. The other method, the getter, allows you to retrieve the value.
The process of ...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. www.PacktPub.com
  6. Customer Feedback
  7. Preface
  8. Introduction to the Ruby Programming Language
  9. Ruby Variables
  10. Ruby Strings
  11. Working with Numbers in Ruby
  12. Ruby Methods
  13. Ruby Iterators and Loops
  14. Ruby Collections
  15. Ruby Conditionals
  16. Object-Oriented Programming in Ruby
  17. Working with the Filesystem in Ruby
  18. Error Handling in Ruby
  19. Regular Expressions in Ruby
  20. Searching with grep in Ruby
  21. Ruby Gems
  22. Ruby Metaprogramming
  23. Ruby Web Frameworks
  24. Working with APIs in Ruby
  25. Ruby Algorithms
  26. Machine Learning

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Comprehensive Ruby Programming by Jordan Hudgens in PDF and/or ePUB format, as well as other popular books in Informatik & Objektorientierte Programmierung. We have over one million books available in our catalogue for you to explore.