Ruby Data Processing: Using Map, Reduce, and Select 1st Edition Jay Godse 2024 Scribd Download
Ruby Data Processing: Using Map, Reduce, and Select 1st Edition Jay Godse 2024 Scribd Download
com
https://textbookfull.com/product/ruby-data-
processing-using-map-reduce-and-select-1st-
edition-jay-godse/
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textbookfull.com
https://textbookfull.com/product/earth-systems-data-processing-and-
visualization-using-matlab-zekai-sen/
textbookfull.com
https://textbookfull.com/product/marikana-a-people-s-history-julian-
brown/
textbookfull.com
https://textbookfull.com/product/amnh-birds-of-north-america-2nd-
edition-francois-vuilleumier/
textbookfull.com
https://textbookfull.com/product/female-olympians-a-mediated-socio-
cultural-and-political-economic-timeline-1st-edition-linda-k-fuller-
auth/
textbookfull.com
https://textbookfull.com/product/regenerative-engineering-and-
developmental-biology-principles-and-applications-1st-edition-david-m-
gardiner/
textbookfull.com
https://textbookfull.com/product/advances-in-pet-the-latest-in-
instrumentation-technology-and-clinical-practice-jun-zhang/
textbookfull.com
Fitzpatrick's Color Atlas and Synopsis of Clinical
Dermatology, 8th Edition Klaus Wolff
https://textbookfull.com/product/fitzpatricks-color-atlas-and-
synopsis-of-clinical-dermatology-8th-edition-klaus-wolff/
textbookfull.com
Ruby Data
Processing
Using Map, Reduce, and Select
—
Jay Godse
Ruby Data Processing
Using Map, Reduce, and Select
Jay Godse
Ruby Data Processing: Using Map, Reduce, and Select
Jay Godse
Kanata, Ontario, Canada
Index���������������������������������������������������������������������������������������������������97
iv
About the Author
Jay Godse is an active software and web applications developer with
expertise in Ruby, Rails, various databases, and Ansible. He also is
active on Stack Overflow as an active contributor. He graduated with an
engineering degree and then went to work as a digital circuit designer.
After a year of that, he switched to software development, and he has
been there ever since in some form. His early work was mostly real-time
telecommunication device control and provisioning using languages
such as C and Protel. He then transitioned into designing distributed
computing systems using languages such as C++. After that, he moved into
web applications. Along the way, he did stints as a software development
manager and a software architect. But for the last nine years, he has
written web applications in Ruby and DevOps applications in Ansible and
Powershell.
v
About the Technical Reviewer
Massimo Nardone has more than 24 years
of experience in security, web/mobile
development, cloud, and IT architecture. His
true IT passions are security and Android.
He has been programming and teaching
how to program with Android, Perl, PHP, Java,
VB, Python, C/C++, and MySQL for more than
20 years.
He holds a Master of Science in Computing
Science from the University of Salerno, Italy.
He has worked as a project manager, software engineer, research
engineer, chief security architect, information security manager, PCI/SCADA
auditor, and senior lead IT security/cloud/SCADA architect for many years.
Technical skills include security, Android, cloud, Java, MySQL, Drupal,
Cobol, Perl, web and mobile development, MongoDB, D3, Joomla,
Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll,
Scratch, and more.
He worked as visiting lecturer and supervisor for exercises at the
Networking Laboratory of the Helsinki University of Technology (Aalto
University). He holds four international patents (PKI, SIP, SAML, and Proxy
areas).
He currently works as chief information security officer (CISO) for
Cargotec Oyj and is member of ISACA Finland Chapter Board.
Massimo has reviewed more than 45 IT books for different publishers,
and in addition to reviewing this book he is also the coauthor of
Pro Android Games (Apress, 2015).
vii
Acknowledgments
I would like to acknowledge a few people who helped make me a better
programmer and a better writer. Thanks go to Kevin Szabo and Ronnie
Taylor, both of whom helped me be a better programmer, and to Christina
Hardy, who helped me to become a better writer. And thanks also go to
Mark Powers and the publication team at Apress who helped produce this
book.
ix
Introduction
I wrote mostly reactive software for many years, but there was always
a user interface or reporting component that had string manipulation,
data synthesis, or data formatting. Since I was not trained as a computer
scientist, I did not learn of higher-order functions such as map(), reduce(),
and select() that were found in languages like Lisp or Smalltalk. Also,
Ruby, Python, C++, and JavaScript were not around when I was in school.
As a result, I struggled with cumbersome, error-prone imperative code for
some tasks.
A few years after learning Ruby, but while still using the imperative
programming style for data processing, I discovered the Ruby Enumerable
library and started using its higher-order functions, such as map(),
reduce(), and select(). What happened to my data-processing code?
However, I couldn’t find any but the most trivial examples of how to
use these functions to solve data-processing problems. It took me a lot
xi
Introduction
of time-consuming trial and error with map, reduce, and select to solve
these kinds of problems.
I decided to write this book to
I’ll also admit that I was forced to learn many of the programming
nuances of these functions as I wrote the examples.
xii
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Introduction
xiii
Prerequisites
You should be familiar with some Ruby programming or at least some
programming in another language. If you don’t know Ruby, search out
the book Learn Ruby the Hard Way1 and go through it as prescribed by its
author, Zed Shaw.
If you are fluent with Python, you might benefit from this book if you
go slowly and look up Ruby information online as you move through
this book.
An internet connection helps if you want to search for online help
using Google, Bing, or another search engine.
You should have a computer with Ruby 2.2.x installed. Windows works,
as do Linux and Mac OSX.
You should have a good syntax-highlighting text editor. I recommend
Notepad++ on Windows or gedit on Linux or Macintosh, both of which are
free. Sublime works on Windows, Linux, or Macintosh and is slightly better,
but costs about $70 at the time of this writing.
1
http://learnrubythehardway.org/book/
xv
CHAPTER 1
Basic Ruby
This section will acquaint or refresh you with basic ways to use the Ruby
command line, as well as some relevant Ruby coding.
If you are comfortable programming in Ruby and understand the Ruby
Enumerable Library reasonably well, you can skip this section.
C:\> irb
irb(main):001:0>
For brevity, I won’t write the full irb prompt every time.
You can execute Ruby statements line by line. The value returned by
each expression is preceded by ⇒.
irb> a = 4
=> 4
irb> a
=> 4
irb> a + 7
=> 11
You can put the code into a file called sample.rb, located in the same
directory or folder from which you ran irb.
1 [2,3,4,5].each do |n|
2 if n%2 == 0
3 puts "even"
4 else
5 puts "odd"
6 end
7 end
2
Chapter 1 Basic Ruby
You could also copy the code block from your text editor and paste it
directly into your command line and get the same result, as long as you use
only spaces for indentation. (Using tabs for indentation will work fine if
you load the file from the command prompt, but if you paste tabs directly
into irb, it will generate errors).
You can either type the code samples from this book into irb directly
or type them into a text editor and then load the file as just shown.
Object Scope
When you are in a Ruby program, the general method of executing a
method f on an object obj is as follows:
obj.f
3
Chapter 1 Basic Ruby
String
Strings are basic constructs in all languages. Let’s look at a few basic
operations used in this book. Try them out on the irb command line for
yourself.
length or size
This yields the size of the string.
downcase
This converts all letters to lowercase.
upcase
This converts all letters to uppercase.
capitalize
This capitalizes the first letter of a string.
split()
This searches a substring for the argument string
and splits the string into a array comprising
substrings on both sides of the split argument,
while the substring of the split argument is
discarded. If there are no matches, an array is
returned with the whole string:
irb> base_string = "abc def ghi"
irb> base_string.split(" ")
=> ["abc","def","ghi"]
irb> base_string.split(" ")
=> ["abc def","ghi"]
irb> base_string.split(" d")
=> ["abc","ef ghi"]
irb> base_string.split("efghi")
=> ["abc def ghi"]
4
Chapter 1 Basic Ruby
join()
string interpolation
5
Chapter 1 Basic Ruby
Array
Arrays in Ruby are like arrays in other languages. They are a collection of
things indexed by a whole number (e.g., 0,1,2,…). In Ruby, an array can
contain any Ruby object at an index. Array indices in Ruby start at 0.
Arrays implement the Ruby Enumerable interface, so they will have
key methods such as each, map, reduce, select, and others.
Special Methods
compact()
irb> [1,nil,2,nil,nil,3,nil,[]].compact
=> [1,2,3,[]]
flatten()
irb> [[1,[2,3]],4,[5,6]].flatten
=> [1,2,3,4,5,6]
push()
irb> [1,2,3,4,5].push(666)
=> [1,2,3,4,5,666]
6
Chapter 1 Basic Ruby
pop
unshift()
shift
7
Chapter 1 Basic Ruby
Hash
Another term for hash is an “associative array,” or even a “dictionary”,
Hashes are indexed by a key object, and there is a value (another object)
for each key object.
These are two ways of creating a new hash. The key of a hash can be
any object or symbol.
8
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Chapter 1 Basic Ruby
=> ["The key is a and the value is 1", "The key is b and the
value is 2", "The key is c and the value is 3"]
Block-passing Syntax
Ruby is one of many languages that allow lexical closures, otherwise
known (kind of ) as anonymous functions or blocks. These functions are
dynamically created. The function can take the form of a defined function.
For example:
9
Chapter 1 Basic Ruby
Now, suppose you want to print each element of a range. You will call
each on the array, which will run the block passed to each for each element
of the array.
irb> (1..5).each(&printit)
1
2
3
4
5
=> (1..5)
You could also pass an actual block of code to run. The block is
surrounded by do and end or { and }. Objects between the vertical bars are
optionally passed to the block (depending on the definition of the block),
and they are used by the block to execute. The return value of a block is the
value of the last expression in the block.
10
Chapter 1 Basic Ruby
You will get the same output as earlier. Ditto for the following:
In Ruby, map, reduce, and select all take a code block as a parameter,
and the code block is executed as defined by the function. In this book,
I will use the do-end syntax most often, and sometimes the { } syntax.
1,John
2,Jack
3,Jim
4,Jared
5,John
irb> names.split("\n")
or
11
Chapter 1 Basic Ruby
irb> "John\r".chomp
=> "John"
irb> "John".chomp
=> "John"
So, for our array, just to ensure that we don’t pick up stray line feeds,
we do the following:
I assume that the examples in this book don’t use “\r\n”, but rather
just “\n”. If that doesn’t work on your operating system, use the chomp()
method as shown before working with the array.
12
CHAPTER 2
Function Overview
and Simple Examples
The Ruby library includes the module Enumerable. This library contains
map(), reduce(), select(), and other functions. This section will outline
the syntax and meanings of the different parts of code that use these three
functions.
If you can get through this section comfortably, both typing the code
into irb and understanding the results, then you will be in a good position
to deepen your understanding with the complex examples and reverse
engineering that follow in the next chapters.
M
ap
This function of the Ruby Enumerable library is simple but profound. The
map() method is applied to an array or a hash. The job of map() is to apply
a function or block to each member of the array and return a new array.
So, when you see
def f(x)
x*x
end