A Programming Primer for Counting and Other Unconventional Tasks

Table of contents

These are the 33 chapters that I've so far started writing, divided into 4 sections. I've included a factbox for each chapter to indicate how complete it is.

The Fundamentals section is probably best read chronologically. If you already know how to program, the Supplemental chapters and Projects cover a wide range of real-world use cases that might interest you.

Please read the About page for more details on the progress of this book.

  • The Fundamentals

    This section is a very abridged introduction to computer science and contains the bare minimum that I think you need to learn before being able to see programming as "not magic." The first chapter is an installation guide and an introduction to the command line. The chapter after that exist solely as a copy-and-paste exercise, so you can at least experience how easy it is to run code.

    Then it's the basics: numbers, strings, variables, methods, loops and more. At a surface level, Ruby is a language. And just as in French, before you you can converse and flirt in it, you need to learn about conjugation, tenses, and how to form sentences like "Je ne suis pas allé à la bibliothèque pour manger le fromage."

    • Installation

      Install Ruby, learn about the command line, and say Hello to the world
      • • Troubleshoot First
      • • The Command Line
      • • Installing Ruby
      • • Testing Your Ruby Installation
      • • Write and Run Your First Program
      • • Get a Text Editor Designed for Coding
      • • How to Run a Program in Your Text Editor
      • • Executing Ruby scripts from the command-line
      • • Troubleshooting
      Status Decent draft
      Words 3,595
      Lines of code 5
      Exercises 0
      Pictures 29
    • Tweet Fetching

      A walkthrough of the concepts covered in this book with the Twitter API.
      • • Making a "scarf"
      • • What this code does
      • • Setup your workspace
      • • Downloading Wikipedia pages
      • • Variables and methods
      • • Writing and reading to files
      • • Collections and Loops
      • • Tweet Fetching
      • • Parsing the Tweets
      • • On to the theory
      Status Decent draft
      Words 4,754
      Lines of code 792
      Exercises 0
      Pictures 12
    • Style, Conventions, and Debugging

      Some basics on reading, annotating, and debugging code.
      • • General program structure
      • • Comments
      • • Whitespace
      • • Variations in style
      • • Debugging with a deep breath
      Status Draft
      Words 1,795
      Lines of code 206
      Exercises 0
      Pictures 3
    • Numbers

      Ruby's basic data objects for counting and math
      • • Number classes
      • • Operators
      • • Number methods
      Status Draft
      Words 666
      Lines of code 56
      Exercises 1
      Pictures 1
    • Strings

      Letters, words, paragraphs, books. And quotes.
      • • Text, words, characters
      • • String operations and methods
      • • Mixing datatypes
      • • Combining strings with interpolation
      • • "Escape" with backslash
      • • String substitution
      Status Draft
      Words 1,731
      Lines of code 105
      Exercises 3
      Pictures 2
    • Variables

      Labels for pointing to your data.
      • • Explaining variables with analogies
      • • Basic usage of variables
      • • Assignments
      • • References, references, references
      • • A visual guide to variables
      • • The classic variable swap
      Status Draft
      Words 4,624
      Lines of code 310
      Exercises 6
      Pictures 13
    • Methods

      How to do things using other programmers' code.
      • • Hello again, "Hello World"
      • • Anatomy of a method
      • • Scope
      • • Method design
      • • Syntax and shortcuts
      • • The open method
      • • Methods and classes
      Status Draft
      Words 3,186
      Lines of code 533
      Exercises 8
      Pictures 4
    • Methods Part II: RubyGems

      Ruby's convenient distribution system for getting everyone else's convenient code
      • • Installing Gems With RubyGems
      • • Fetch data from the Internet with the Rest-Client gem
      • • Application Programming Interfaces (APIs)
      • • Google's Geocoding API
      • • Crack XML with the Crack Gem
      Status Decent draft
      Words 1,391
      Lines of code 54
      Exercises 2
      Pictures 4
    • If and Else

      Breaking out from a narrow, straight path of program flow.
      • • The if
      • • Booleans: the truth about true and false
      • • Going elsewhere
      • • Variations on if
      • • Logic and flow
      • • The FizzBuzz Game
      Status Draft
      Words 2,316
      Lines of code 243
      Exercises 4
      Pictures 1
    • Loops

      How to do thousands of operations with a few lines of code.
      • • The for Statement
      • • Other loops
      • • Rubyists prefer each
      Status Draft
      Words 1,110
      Lines of code 114
      Exercises 4
      Pictures 1
    • Collections

      How to store lists of data.
      • • Arrays
      • • Other array methods
      • • Working with partial arrays
      • • Hashes
      • • Nested collections
      • • Revisiting the crack gem
      Status Draft
      Words 3,347
      Lines of code 487
      Exercises 11
      Pictures 2
    • Enumerables

      Transforming and sorting collections
      • • The each
      • • Transforming arrays with map
      • • The select and inject methods
      Status Draft
      Words 1,355
      Lines of code 248
      Exercises 7
      Pictures 1
    • File input/output

      Reading from and writing directly to files
      • • Creating a file and writing to it
      • • Reading from a file
      • • Closing files
      • • File existence and properties
      Status Decent draft
      Words 1,778
      Lines of code 198
      Exercises 6
      Pictures 4
    • Reviewing Tweet Fetching

      (An outdated section, but useful for reviewing)
      • • The Code
      • • Initializing gems and variables
      • • Following Twitter's API
      • • GET with RestClient
      • • Use crack to read the XML response
      • • Request the Tweets, Repeat
      • • There's a gem for that
      • • Fundamentals and more
      Status Deprecated
      Words 1,339
      Lines of code 120
      Exercises 0
      Pictures 1
  • Supplementals

    These chapters aren't necessary in understand the foundations of programming, but they're critical to making your programming knowledge useful in real-world applications.

    Some of these concepts have little to do with programming, so it's possible to read their chapters without intending to learn programming. Regular expressions, in particular, are valuable to anyone who has to deal with cleaning and verifying datasets. I've devoted several chapters to web-scraping on the assumption that some readers may have little working knowledge about HTML, Javascript, and the other underlying web technologies.

    • Regular Expressions

      An essential mini-language for text-matching and manipulation
      • • Try Regexes in Your Text Editor
      • • Introduction to regex syntax
      • • Ruby and regexes
      • • More regex patterns
      • • Get a cheat sheet
      Status Incomplete draft
      Words 5,824
      Lines of code 183
      Exercises 7
      Pictures 7
    • An Intro to Web Scraping

      A simplifed overview of how data gets to and from the web browser
      • • Scraping: The big picture
      • • The scraping roadmap
      Status Draft
      Words 2,172
      Lines of code 10
      Exercises 0
      Pictures 6
    • Meet Your Web Inspector

      The built-in tool for to easily examine the structure of webpages.
      • • Your browser's web inspector
      • • Use the Inspector to read HTML
      Status Decent draft
      Words 1,292
      Lines of code 10
      Exercises 0
      Pictures 11
    • Inspecting a Webpage's Traffic

      Using the web inspector to watch a webpage's incoming and outgoing traffic.
      • • Activate your network panel
      • • Inspecting a webpage's assets
      • • Inspecting the requests
      • • Inspecting the server's response
      Status Decent draft
      Words 2,500
      Lines of code 21
      Exercises 0
      Pictures 23
    • Parsing HTML with Nokogiri

      Quickly extract data from raw HTML with the Nokogiri gem
      • • Nokogiri
      • • Nokogiri and CSS selectors
      • • Nokogiri and your web inspector
      Status Decent draft
      Words 1,629
      Lines of code 120
      Exercises 2
      Pictures 3
    • Writing a Web Crawler

      Combining HTML parsing and web inspection to programmatically navigate and scrape websites.
      • • Why not wget?
      • • Move Link-to-Link (Wikipedia)
      • • Change URL parameters (Data.gov)
      • • Detecting redirects (Defense.gov)
      • • POST requests (FEC.gov)
      • • Use the Mechanize gem
      • • Unscrapable sites
      • • Poking around
      Status Draft
      Words 4,601
      Lines of code 460
      Exercises 0
      Pictures 28
    • The Mechanize Gem

      Automate the annoying parts of web-scraping
      • • Simplify the scraping of complex websites
      • • Mechanize in action
      Status Stub
      Words 451
      Lines of code 57
      Exercises 0
      Pictures 1
    • SQL

      An introduction to relational databases and their query languages
      • • Data-crunching without databases
      • • Getting started with SQLite
      • • SQL syntax
      • • Beyond SELECT
      • • Using SQLite3 with Ruby
      Status Draft
      Words 4,810
      Lines of code 257
      Exercises 5
      Pictures 16
    • Using Non-Ruby Programs With Ruby

      Don't throw away the programs you've been using. Ruby can play with non-Ruby applications.
      • • Ruby, Excel, and Google Docs
      • • Ruby and the Command Line
      • • Ruby and Python
      • • Auto-optimized-optical character recognition
      Status Rough draft
      Words 3,282
      Lines of code 194
      Exercises 1
      Pictures 21
    • Image Manipulation

      How to use ImageMagick and the RMagick gem to process and interpret images and photos.
      • • ImageMagick and the RMagick gem
      • • Opening and saving images
      • • Image alterations
      • • Pixel operations
      • • Drawing
      • • Applications
      Status Stub
      Words 1,967
      Lines of code 200
      Exercises 3
      Pictures 20
  • Design and Theory

    This section contains some of the important computer science and software engineering concepts that I largely skipped over in the beginning to soften the learning curve.

    This section is very incomplete and contains few working examples. I'm experimenting with different ways of introducing the topics. The main point is to make you at least aware of them. The topics are deep and complex and can occupy weeks of a class curriculum. Even if this section were more complete, you should seek multiple sources and explanations.

    • Exception and Error Handling

      How a program recovers from unexpected (and expected) errors
      • • Demonstrating exceptions
      • • The Begin...Rescue block
      • • Flow of exception handling
      • • Exception and Error Classes
      • • Further reading
      Status Stub
      Words 1,570
      Lines of code 172
      Exercises 0
      Pictures 5
    • Object-Oriented Concepts

      A primer on object-oriented design and using it to organize your code.
      • • A Cinematic Metaphor
      • • Object-oriented syntax
      • • Fleshing out the OOP with movies
      Status Stub
      Words 1,785
      Lines of code 190
      Exercises 0
      Pictures 4
    • Recursion

      A divide-and-conquer design pattern
      • • The King and his rocks
      • • Applications (under construction)
      Status Stub
      Words 1,312
      Lines of code 41
      Exercises 0
      Pictures 0
  • The Projects

    Even though The Bastards Book of Ruby was going to just be a list of programming projects and snippets, this section is the least complete and consistent. Some portions have been written before I had decided to write all the other sections, so they contain thorough step-by-step instructions. Other portions – because I assume you've gone through the previous sections, or just out of laziness – do little more than describe the point of the code.

    Though many of the Fundamental and Supplementary chapters contain useful code examples, the Projects section attempts to walk through not just the code for specific situations, but the reasons behind exploring a dataset and the possible avenues of analyses and investigation.

    The code is a little more complex – and, at times, obtuse – but is meant to be readable by not-yet accomplished programmers. However, readers who are already skilled programmers may find useful insights or ideas to take on.

    As of the first release of this book, I've only had time to draft just a few of the project walkthroughs that I have so far planned. This section will be the most added-upon over time.

    • Comparing California's Costs of Surgery

      How to collect and analyze the costs of California's reported surgical procedures.
      • • The Concepts
      • • The Context
      • • The Steps
      Status Decent draft
      Words 716
      Lines of code 0
      Exercises 0
      Pictures 0
    • CA Surgeries: Mechanize

      Use Mechanize to navigate and scrape the Common Surgeries Database
      • • Trying out the Common Surgeries website
      • • Inspecting the Common Surgeries website
      • • The Code
      • • The Fetching Code: All Together
      Status Decent draft
      Words 2,367
      Lines of code 251
      Exercises 0
      Pictures 7
    • CA Surgeries: Parsing and Storing

      Using Nokogiri to parse the collected HTML pages and storing into a SQLite database
      • • The Steps
      • • Parsing with Nokogiri
      • • Storing the Data with SQLite
      • • Code Comparison: SQL vs. Ruby
      Status Decent draft
      Words 1,173
      Lines of code 157
      Exercises 0
      Pictures 2
    • CA Surgeries: Visualization

      Using Google Charts to visualize the costs of surgery in California
      • • Hacking Visualizations
      • • Intro to the Google Charts API
      • • Queries and graphing
      • • Why coding makes data easier
      Status Draft
      Words 2,284
      Lines of code 148
      Exercises 0
      Pictures 17
    • Counting the Jail Logs

      Who is in jail? And why?
      • • Scraping with Mechanize
      • • Parsing the data
      • • Data normalization
      • • Sample analyses and visualizations
      • • Limitations of the data
      Status Draft
      Words 3,344
      Lines of code 331
      Exercises 0
      Pictures 30
    • Gathering the Standard & Poor's 500 Stock Listings

      A financially-themed demo of HTML parsing, SQL, and visualization
      • • Getting historical stock data from Yahoo
      • • Storing stock data into SQLite
      • • Visualization with Google Charts
      Status Draft
      Words 105
      Lines of code 45
      Exercises 0
      Pictures 1