Skip to content

Instantly share code, notes, and snippets.

View rpearce's full-sized avatar
👶
infant and toddler duty

Robert Pearce rpearce

👶
infant and toddler duty
View GitHub Profile
@rpearce
rpearce / keybase.md
Last active February 27, 2019 20:34

Keybase proof

I hereby claim:

  • I am rpearce on github.
  • I am rpearce (https://keybase.io/rpearce) on keybase.
  • I have a public key whose fingerprint is 6F5D 52E9 DEF3 D210 66AE 97F4 E8DD 20E4 8EFB 46B5

To claim this, I am signing this object:

@rpearce
rpearce / rb-testing-with-minitest.md
Last active May 10, 2022 02:25
Ruby Fundamentals - Testing with MiniTest

Testing with MiniTest

MiniTest is a Ruby testing library that comes built in to Ruby, itself. While there are a number of other testing libraries in the Ruby ecosystem, we will focus on MiniTest because of its inclusion in Ruby by default. In this lesson, we will explore the different functionality provided by MiniTest and how to use it to solidify our code.

Our First Test

Before we can write the world's best test suite, we must first understand how to structure a test and run the tests. First, we create a file called tests.rb, and we place the following inside of it:

require "minitest/autorun"

class TestReality < MiniTest::Test
 def test_the_universe_still_works
@rpearce
rpearce / rb-file-io-csv.md
Last active May 10, 2022 02:25
Ruby Fundamentals - File I/O and CSVs

File Input/Output and Working With CSVs

Ruby makes reading and writing files on your system a breeze! In this lesson, we are going to look at working with text files (.txt extension) and comma separated value files (.csv extension).

Plain Text Files

Text files can contain just about anything from poems to books to lists of countries. This section will teach us about creating text files and reading existing ones.

Creating a Text File

open and close a file

There are a few ways to create new files with Ruby, but we are going to focus on the open option for consistency. What we would like to do is create a new file, countries.txt, and add a few countries to it, each with their own line. We can use Ruby's File class to do this.

@rpearce
rpearce / rb-classes.md
Last active May 10, 2022 02:23
Ruby Fundamentals - Classes

Classes

Classes are a way of defining a blueprint for how a concept, such as a Dinosaur, should be described and what actions it should be able to perform. For example, a Dinosaur might have the descriptors type, gender, and health and might have actions like move and eat. We place these attributes and actions into a class called Dinosaur because they are specific to dinosaurs and not to another concept that exists in our application.

In this lesson, we will discuss how to create and use classes to organize our code in to defined, extendable concepts.

Defining a Class

To begin a class definition, use the class keyword, followed by the CamelCased[1] noun that you are describing. Make sure to include an end to close the class definition.

class Dinosaur
end
@rpearce
rpearce / rb-working-with-collections.md
Last active May 10, 2022 02:24
Ruby Fundamentals - Working With Collections

Working With Collections

Once we have a collection of information, whether in the form of an Array, Hash, or even an Array of Hashes, we often need to perform operations on and ask questions of this data. Writing out the code for every single member of a collection is tedious and does not scale to handle dynamic collection sizes. Instead, we can write our code once and apply it to every member of a collection and keep things DRY[1] (don't repeat yourself!).

In this lesson, we will be introduced to loops and understand the power they give us when working with collections.

while

Before we can apply loops to collections, we first need to know how one works! Here is what a very basic while loop looks like:

while 1 < 2
 # code to be run goes here
@rpearce
rpearce / rb-control-flow.md
Last active May 10, 2022 02:24
Ruby Fundamentals - Control Flow

Control Flow

Computer programs are useful for automating processes because they can be programmed to perform different actions for different scenarios. Take email, for example: if you try to send an email that has no recipient, then the program should ask you to provide one; otherwise, if there is an email but no subject line, then the program should ask if you're sure you don't want to provide a subject; otherwise, if there is an email and a subject (or you're sure you don't want one), then send the email with no questions asked.

In this lesson, we will learn about how we can use logic to control the actions of our program.

if, elsif, else

Imagine that we have been tasked to write an ATM (automated teller machine) program where users have three options:

  1. Check balance
  2. Withdraw funds
@rpearce
rpearce / rb-operators.md
Last active May 10, 2022 02:24
Ruby Fundamentals - Operators

Operators

Ruby makes use of mathematical operators, such as plus and minus signs, to perform many different functions. Plus and minus signs, might seem straightforward when dealing with numbers, but they can have many different applications depending on whether you're working with numbers or arrays, for example. Additionally, equals signs are not always what they seem! In this lesson, we will dive in to a number of different uses of +, -, /, *, =, and more.

Computational

Just like a calculator, Ruby lets you do addition, subtraction, multiplication, division and more. Here are the symbols for each:

  • addition: +
  • subtraction: -
  • multiplication: *
  • division: /
@rpearce
rpearce / rb-arrays-hashes.md
Last active May 10, 2022 02:24
Ruby Fundamentals - Data Types: Arrays and Hashes

Arrays and Hashes

If you have a list of data values, Ruby lets you store these values into a type of list called an Array. Similarly, if you have a list of properties that describe a concept, these properties and their values can be grouped together into what is called a Hash.

In this lesson, we will come to understand why these data types exist and how to use them.

Arrays — [ ]

If you have ever made yourself a grocery list, a reading list, or a travel checklist, then you probably already understand the value of creating a list and naming it separately from the other types of lists you have. With Ruby arrays, we can do the same thing.

Consider that you want to keep a list of your family members' ages, though in no particular order. Using an array and integers, that could look like this:

@rpearce
rpearce / rb-casts-tweet-parser.md
Last active May 10, 2022 02:24
Ruby Screencast - Tweet Parser

Tweet Parser

Accepting Tweet Input

Let's put our new knowledge of Ruby strings to work by accepting username and tweet input and returning a sentence including both.

In your tweets.rb file, write the following into tweets.rb:

print "What is your username? "
username = gets
print "Enter tweet: "
@rpearce
rpearce / rb-strings-numbers-booleans.md
Last active May 10, 2022 02:24
Ruby Fundamentals - Data Types: String, Numbers, and Booleans

Strings, Numbers & Booleans

Programming languages, just like the real world, group different types of information separately. For example, if you want to add together the numbers 1 and 2, you will naturally get back the number 3. If you would rather maintain a list of people's names, then that list is a different type of information than the numbers are.

In this lesson, we will cover most of the Ruby information types and walk through examples of their most commonly used bits of functionality.

Strings — "Hello!"

Ruby strings look a lot like quotations from novels. Here is an example of a quotation from a fictitious novel:

Mom said, "Have fun! Please don't forget to wear your helmet when you're jet packing!"