Table Of Content(cid:36)(cid:80)(cid:78)(cid:81)(cid:83)(cid:70)(cid:73)(cid:70)(cid:79)(cid:84)(cid:74)(cid:87)(cid:70)(cid:2)(cid:51)(cid:86)(cid:67)(cid:90)
(cid:49)(cid:83)(cid:80)(cid:72)(cid:83)(cid:66)(cid:78)(cid:78)(cid:74)(cid:79)(cid:72)
(cid:40)(cid:80)(cid:2)(cid:71)(cid:83)(cid:80)(cid:78)(cid:2)(cid:67)(cid:70)(cid:72)(cid:74)(cid:79)(cid:79)(cid:70)(cid:83)(cid:2)(cid:85)(cid:80)(cid:2)(cid:68)(cid:80)(cid:79)(cid:96)(cid:69)(cid:70)(cid:79)(cid:85)(cid:2)(cid:81)(cid:83)(cid:80)(cid:72)(cid:83)(cid:66)(cid:78)(cid:78)(cid:70)(cid:83)
Jordan Hudgens
BIRMINGHAM - MUMBAI
Comprehensive Ruby Programming
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its
dealers and distributors will be held liable for any damages caused or alleged to be caused
directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2017
Production reference: 1280617
(cid:49)(cid:86)(cid:67)(cid:77)(cid:74)(cid:84)(cid:73)(cid:70)(cid:69)(cid:2)(cid:67)(cid:90)(cid:2)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:2)(cid:49)(cid:86)(cid:67)(cid:77)(cid:74)(cid:84)(cid:73)(cid:74)(cid:79)(cid:72)(cid:2)(cid:45)(cid:85)(cid:69)(cid:16)
(cid:45)(cid:74)(cid:87)(cid:70)(cid:83)(cid:90)(cid:2)(cid:49)(cid:77)(cid:66)(cid:68)(cid:70)
(cid:21)(cid:23)(cid:2)(cid:45)(cid:74)(cid:87)(cid:70)(cid:83)(cid:90)(cid:2)(cid:52)(cid:85)(cid:83)(cid:70)(cid:70)(cid:85)
(cid:35)(cid:74)(cid:83)(cid:78)(cid:74)(cid:79)(cid:72)(cid:73)(cid:66)(cid:78)
(cid:35)(cid:21)(cid:2)(cid:20)(cid:49)(cid:35)(cid:14)(cid:2)(cid:54)(cid:44)(cid:16)
ISBN 978-1-78728-064-9
Credits
Author Copy Editor
Jordan Hudgens Tom Jacob
Project Coordinator Proofreader
Suzanne Coutinho Safis Editing
Acquisition Editor Indexer
Ben Renow-Clarke Aishwarya Gangawane
Content Development Editor Graphics
Radhika Atitkar Kirk D'Penha
Technical Editor Production Coordinator
Bhagyashree Rai Arvindkumar Gupta
About the Author
Jordan Hudgens is the CTO and founder of DevCamp, where he leads instruction and
curriculum development for all the DevCamp and Bottega code schools around the US.
As a developer over the past decade, Jordan has traveled the world building applications
and training individuals on a wide variety of topics, including Ruby development, big data
analysis, and software engineering.
Jordan focuses on project-driven education as opposed to theory-based development. This
style of teaching is conducive to learning how to build real-world products that adhere to
industry best practices.
Additionally, Jordan has published multiple books on programming and computer science
along with developing training curriculum for Learn.co, DevCamp, and AppDev on the
topics, namely Ruby on Rails, Java, AngularJS, NoSQL, API development, and algorithms.
www.PacktPub.com
For support files and downloads related to your book, please visit (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78).
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78)and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at (cid:84)(cid:70)(cid:83)(cid:87)(cid:74)(cid:68)(cid:70)(cid:33)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78) for more details.
At (cid:88)(cid:88)(cid:88)(cid:16)(cid:49)(cid:66)(cid:68)(cid:76)(cid:85)(cid:49)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78), you can also read a collection of free technical articles, sign up for a
range of free newsletters and receive exclusive discounts and offers on Packt books and
eBooks.
(cid:73)(cid:85)(cid:85)(cid:81)(cid:84)(cid:28)(cid:17)(cid:17)(cid:88)(cid:88)(cid:88)(cid:16)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78)(cid:17)(cid:78)(cid:66)(cid:81)(cid:85)
(cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2)
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt
books and video courses, as well as industry-leading tools to help you plan your personal
development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial
process. To help us improve, please leave us an honest review on this book's Amazon page
at (cid:73)(cid:85)(cid:85)(cid:81)(cid:84)(cid:28)(cid:17)(cid:17)(cid:88)(cid:88)(cid:88)(cid:16)(cid:66)(cid:78)(cid:66)(cid:91)(cid:80)(cid:79)(cid:16)(cid:68)(cid:80)(cid:78)(cid:17)(cid:69)(cid:81)(cid:17)(cid:19)(cid:25)(cid:26)(cid:25)(cid:20)(cid:26)(cid:18)(cid:24)(cid:22)(cid:18).
(cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2)
If you'd like to join our team of regular reviewers, you can e-mail us at
(cid:68)(cid:86)(cid:84)(cid:85)(cid:80)(cid:78)(cid:70)(cid:83)(cid:83)(cid:70)(cid:87)(cid:74)(cid:70)(cid:88)(cid:84)(cid:33)(cid:81)(cid:66)(cid:68)(cid:76)(cid:85)(cid:81)(cid:86)(cid:67)(cid:16)(cid:68)(cid:80)(cid:78). We award our regular reviewers with free eBooks and
videos in exchange for their valuable feedback. Help us be relentless in improving our
products!
Table of Contents
Preface
1
Chapter 1: Introduction to the Ruby Programming Language
8
What is Ruby? 9
Popular sites that use Ruby 9
Installing Ruby on a computer 9
Ruby language dashboard 10
Installing different Ruby versions with RVM 16
Summary 18
Chapter 2: Ruby Variables
19
Using variables in Ruby 19
Variable code implementation 20
Printing to the Ruby console 22
Using puts 22
Using p 22
Getting input from the Ruby console using gets and chomp 25
Code implementation 26
Variable scope and variable types 28
Local variables 29
Global variables 30
Instance variables 31
Constants 32
Class variables 32
Summary 33
Chapter 3: Ruby Strings
34
Using strings in Ruby 34
Missing quotation marks 35
Printing strings 36
Storing strings in variables 37
String interpolation guide for Ruby 37
What is string interpolation? 37
String manipulation guide 40
What is string manipulation? 40
String manipulation code examples 41
Method chaining 41
A practical implementation 42
Give it a bang! 42
String substitution guide 43
Adding a bang 45
Becoming a block head 46
Split, strip, and join guides for strings 47
Using the strip method 47
Using the split method 48
Using the join method 51
Summary 52
Chapter 4: Working with Numbers in Ruby
53
Integer arithmetic guide 53
Arithmetic order of operations 54
Difference between integers and floats 55
Summary 56
Chapter 5: Ruby Methods
57
Creating methods in Ruby 57
What does a Ruby method return? 60
What are the differences between class and instance methods in Ruby? 62
Ruby proc tutorial 67
Proc code example 67
What is a block? 70
The Ruby lambda tutorial 71
Stabby lambdas 72
What is the difference between procs and lambdas in Ruby? 72
Argument count 72
Return behavior 74
Guide to method arguments in Ruby 76
What are method arguments? 77
Method argument syntax 79
Method argument code examples 79
Named arguments 80
Default argument values 83
Using splat and optional arguments in Ruby methods 85
Traditional splat arguments 85
Keyword-based splat arguments 86
Optional arguments 88
Summary 90
[ ii ]
Chapter 6: Ruby Iterators and Loops
91
The while loop guide 91
The while loop code example 91
The each iterator method tutorial 94
The each loop code example 94
The for...in loops tutorial 97
Looping over a hash 98
Nested iterator code example 98
Using the select method 102
Using the map method – part 1 106
The map method code example 107
Using the map method – part 2 110
Summing values in an array using the inject method 112
Summary 114
Chapter 7: Ruby Collections
115
Using arrays 115
Deleting items from arrays 117
Using push and pop for arrays 119
The Ruby hash tutorial 121
The hash code example 122
Deleting elements from a hash 125
Iterating over a hash for a key or value 126
Top hash methods in Ruby 128
Adding elements to a hash 129
Swapping keys and values 129
Merging hashes 130
Converting a hash into an array 131
Listing all keys 132
Listing all values 132
Summary 132
Chapter 8: Ruby Conditionals
134
The conditionals guide 134
Real-world use of conditionals 134
Code example of conditionals 135
Syntax for the unless conditional 135
Running Ruby files 135
The unless conditional code example 136
Nested if...else conditionals 139
[ iii ]