Reviews![Book Book](/uploads/1/2/5/1/125193737/612734712.png)
Author: Hal Fulton
Pub Date: 2015![Pdf Pdf](/uploads/1/2/5/1/125193737/342189722.png)
ISBN: 978-0321714633
Pages: 832
Language: English
Format: PDF/EPUB
Size: 42 Mb
Download
The Rails 4 Way by Obie Fernandez and Kevin Faustino. Rails Testing Handbook — a free ebook by Semaphore on building Rails apps with BDD using. Learn the Hard Way; About LPTHW. Welcome to Learn Ruby the Hard Way, 3rd Edition. You'll get a professional quality PDF and hours of HD Video, all DRM-free.
![Book Book](/uploads/1/2/5/1/125193737/612734712.png)
Author: Hal Fulton
Pub Date: 2015
![Pdf Pdf](/uploads/1/2/5/1/125193737/342189722.png)
ISBN: 978-0321714633
Pages: 832
Language: English
Format: PDF/EPUB
Size: 42 Mb
Download
For more than a decade, Ruby developers have turned to The Ruby Way for reliable “how-to” guidance on effective Ruby programming. Now, Hal Fulton and André Arko have thoroughly updated this classic guide to cover new language enhancements and developers’ experiences through Ruby 2.1.
The new edition illuminates Ruby 2.1 through 400+ examples, each answering the question: “How do I do this in Ruby?” For each example, they present both a task description and realistic technical constraints. Next, they walk step-by-step through presenting one good solution, offering detailed explanations to promote deeper understanding.
Conveniently organized by topic, The Ruby Way, Third Edition makes it easier than ever to find the specific solution you want—and to write better code by reflecting Ruby’s unique philosophy and spirit.
The new edition illuminates Ruby 2.1 through 400+ examples, each answering the question: “How do I do this in Ruby?” For each example, they present both a task description and realistic technical constraints. Next, they walk step-by-step through presenting one good solution, offering detailed explanations to promote deeper understanding.
Conveniently organized by topic, The Ruby Way, Third Edition makes it easier than ever to find the specific solution you want—and to write better code by reflecting Ruby’s unique philosophy and spirit.
Ruby and Data Formats
…
Parsing JSON
JSON, short for JavaScript Object Notation, was designed as a human-readable format allowing arrays, hashes, numbers, and strings to be serialized as plain text to communicate data between programs, no matter what language they are written in. Designed partially as a reaction to the verboseness of XML, JSON is sparse both in the number of characters required and in the number of data types it supports.
We briefly looked at using JSON as a way to store Ruby data in Section 10.2.5, “Persisting Data with JSON” of Chapter 10, “I/O and Data Storage.” In this section, we’ll
look at how to parse and manipulate the type of JSON data that might be provided by a website API. Before we do that, though, let’ s review how to parse a string or file
containing JSON data into a Ruby hash:
…
What JSON calls an “object” corresponds almost perfectly to what we in Ruby-land know as a hash. Meanwhile, JSON contains several other easily recognizable types:
arrays, strings, and numbers. JSON (including the strings contained inside it) is always encoded as UTF-8, and the only other types it is able to encode are true, false, and
null (or nil in Ruby).
look at how to parse and manipulate the type of JSON data that might be provided by a website API. Before we do that, though, let’ s review how to parse a string or file
containing JSON data into a Ruby hash:
…
What JSON calls an “object” corresponds almost perfectly to what we in Ruby-land know as a hash. Meanwhile, JSON contains several other easily recognizable types:
arrays, strings, and numbers. JSON (including the strings contained inside it) is always encoded as UTF-8, and the only other types it is able to encode are true, false, and
null (or nil in Ruby).
Navigating JSON Data
Once the JSON has been parsed, the nested hashes and arrays that are returned can be navigated to extract the specific data required. T o illustrate this, we’ll use some JSON data provided by the GitHub public API:
…
In the preceding code, we are using the open-uri library for convenience. This is explained in greater detail in Chapter 18, “Network Programming”; for now, just be aware
that it enables us to use the open method on a URI much as if it were a simple file.
…
In the preceding code, we are using the open-uri library for convenience. This is explained in greater detail in Chapter 18, “Network Programming”; for now, just be aware
that it enables us to use the open method on a URI much as if it were a simple file.
Using open-uri, we download a string that contains a JSON array of the GitHub accounts of contributors to the main Ruby interpreter . (Many contributors who lack GitHub accounts are not included in this list.) Parsing the JSON gives us an array of hashes, with each hash containing information about one particular contributor , with keys including login, id, url, and contributions, among others.
Then, we use pp, the “prettyprint” library , to print the hash of attributes for the first contributor . (Every attribute is printed by pp, but we’ve elided some of them here to save space.) Next, we reverse-sort the list by the number of contributions so that the hashes are ordered from most commits to least commits. Finally , we map the sorted hashes of contributor information into just their login (or username) and print them as a commaseparated list.
Most real-world uses of JSON data are similar to this one: Write some code to fetch the data, parse the data into a native Ruby hash or array , and then find the array items or hash keys that contain the data you are interested in.
…
Parsing XML (and HTML)
XML, the eXtensible Markup Language, is a tag-based markup language. HTML, the HyperT ext Markup Language, is very similar (and, in fact, both XML and HTML are
based on an earlier tag-based system called SGML). XML and HTML both rose to massive popularity in the 1990s, and are still used heavily today in development tools,
program data storage and transfer , and all over the W eb.
based on an earlier tag-based system called SGML). XML and HTML both rose to massive popularity in the 1990s, and are still used heavily today in development tools,
program data storage and transfer , and all over the W eb.
In XML and HTML, every element is named, and the entire document is hierarchically structured. Although it is highly verbose, everything is written in plain text and can be read by a human being directly , if needed. Another advantage over formats from the 70s and 80s is that XML allows variable-length data, rather than requiring each data field to fit into a specific number of bytes.
Three or four decades ago, memory constraints would have rendered XML largely impractical. However , if it had been used back then, issues such as the infamous “Y2K”
problem would never have occurred (although even Y2K turned out to be more of a nuisance than a problem). There was a Y2K issue solely because most of our legacy data
was stored and manipulated in fixed-length formats. So, although it has shortcomings, XML also has its uses.
problem would never have occurred (although even Y2K turned out to be more of a nuisance than a problem). There was a Y2K issue solely because most of our legacy data
was stored and manipulated in fixed-length formats. So, although it has shortcomings, XML also has its uses.
In Ruby , the most common way to read, manipulate, and write XML or HTML is with the Nokogiri gem. The gem provides a Ruby interface (also called binding) for the libXML2 library , which is written in C. Nokogiri has two primary APIs, which could be called document based and str eam based. W e’ll look at both approaches.
…
Ruby and Web Applications
…
HTTP Servers
HTTP servers make up a huge proportion of the programs in service today . They provide interfaces to our communications, documents, finances, travel plans, and nearly every other aspect of life. As complex as those web apps can be, though, they are all served over HTTP , which is a very simple protocol. In essence, HTTP is just a few lines of plain text describing each request and response, all sent over a TCP socket.
A Simple HTTP Server
T o demonstrate exactly how simple HTTP is, we will begin by writing an HTTP server in Ruby that can be accessed using any web browser . Try running Listing 19.1 and then opening the URL localhost:3000 in your web browser .
…
In this server , we accept incoming requests for a TCP connection. Then, we print each line of text sent by the web browser until there is an empty line. Next, we send an HTTP Response, which is just text in a specific format that the browser can understand.
…
In this server , we accept incoming requests for a TCP connection. Then, we print each line of text sent by the web browser until there is an empty line. Next, we send an HTTP Response, which is just text in a specific format that the browser can understand.
The response consists of a status code, headers, and body . In our response, we send the status code 200, which indicates a successful request and response. There are many other status codes, including redirection to a different location (301 and 302), no item at that address (404), or even an error on the server (500). For a complete list, refer to a reference online such as Wikipedia or httpstatus.es.
After the status code, we send a single header , indicating to the browser how many bytes there will be in the body we are about to send. Additional headers are optional, and can be used to indicate things such as what we are sending in the body (HTML, JSON, XML, or even a video or audio file), whether the body will be compressed, and other information.
Last, we send an empty line that indicates the headers are over and the body will follow. As you may have noticed, the line breaks are not simple n linefeed characters, but are rn (the carriage return character followed by the linefeed character). A single linefeed is the standard line break on UNIX and Mac OS X machines, whereas the combined carriage return and linefeed is the standard line break on Windows machines and some other systems, as well as HTTP requests and responses.
At this point, if you have run the server and opened localhost:3000 in your browser , you should be able to see a large, bold “Hello from Ruby!” in your web browser. In your terminal, you will be able to see the exact text that the browser sent to your server. For me, the terminal output looked like this: