Comprehensive Ruby Programming
eBook - ePub

Comprehensive Ruby Programming

Jordan Hudgens

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

Comprehensive Ruby Programming

Jordan Hudgens

Book details
Book preview
Table of contents
Citations

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
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 here.
Is Comprehensive Ruby Programming an online PDF/ePUB?
Yes, you can access Comprehensive Ruby Programming by Jordan Hudgens in PDF and/or ePUB format, as well as other popular books in Informatik & Programmierung in Ruby. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781787287990

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