Ruby Programming for Beginners

What we'll cover

Programming

Operating System (OS)

Talk to a computer's hardware.

Application

The software. It sends input (commands, data, etc) to the operating system and receive output after the operating system has acted on it.

Language

A set of code that can be used to create an application.

Many others.

Q: How is a computer language similar to a human language, like English or Spanish? How is it different?

Library

A collection of reusable code to accomplish a generic activity.

Framework

Collection of reusable code to facilitate development of a particular product or solution.

Ruby vs. Rails

Ruby is a language

Gems are Ruby libraries

Rails is a framework

Ruby vs. Rails

The rest of this tutorial isn't about Rails. You're learning how to do any kind of programming with Ruby.

Ruby Philosophy

I believe people want to express themselves when they program.

They don't want to fight with the language.

Programming languages must feel natural to programmers.

I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby.

-- Matz (Yukihiro Matsumoto), Ruby creator

Ruby is a scripting language

Python, Perl, and JavaScript are scripting languages too.

Java and C++ are examples of compiled languages.

Let's start coding!

Open Your Terminal

You may also hear it called "command Line", "console", "shell", or "Bash"

Prompt

Whenever instructions start with "$ ", type the rest of the line into terminal.

Let's give the terminal a command, to open Interactive Ruby (IRB)

  $ irb

irb: Interactive Ruby

IRB has its own prompt, which customarily ends with >

  $ irb
  >

You can use Control-C to exit IRB any time. Or type exit on its own line.

  > exit
  $ 

Now you're back to the terminal's prompt.

Windows Users! Some people have experienced trouble with backspace, delete, and arrow keys working properly in irb - what a pain! If you run into this problem, use this command instead to launch irb.

$ irb --noreadline

Variables

A variable holds information.

  $ irb
  > my_variable = 5
  => 5
  > another_variable = "hi"
  => "hi"
  > my_variable = 10
  => 10

What is happening on the lines beginning with => ?

Variable

Variable Assignment

Variables are assigned using a single equals sign (=).

The right side of the equals sign is evaluated first, then the value is assigned to the variable named on the left side of the equals.

  apples = 5
  bananas = 10 + 5
  fruits = 2 + apples + bananas
  bananas = fruits - apples

What happened on each line? Is it what you expected?

What could you do to see each's return value for confirmation?

Variable

Variable Naming

Create a variable whose name has:

What did you learn?

Common types of information

String

A string is text. It must be wrapped in a matched pair of quotation marks.

  $ irb
  > 'Single quotes work'
  => "Single quotes work"
  > "Double quotes work"
  => "Double quotes work"
  > "Start and end have to match'
  ">

What is happening on the last two lines? How would you solve it?

exercise

Numbers

exercises

Collection

Collection types: Array, Hash

Collection

Array

An array is a list.

Each array must be surrounded by square braces aka square brackets. A comma separates each member.

> fruits = ["kiwi", "strawberry", "plum"]
=> ["kiwi", "strawberry", "plum"]

exercises

Collection

Array

Indexing

Members are stored in order. Each can be accessed by its index. Ruby starts counting at zero.

> fruits[0]
=> "kiwi"
> fruits[1]
=> "strawberry"
> fruits[2]
=> "plum"

exercises

Collection

Hash

In a hash we can refer to a member by a keyword instead of a number. Each member is a pair:

A hash may also be known as a dictionary, associative array, or map.

Collection

Hash

Hash Syntax

A hash is surrounded by curly braces aka curly brackets. A comma separates each member pair. A key uses => (the rocket) to point to its value.

> states = {"CA" => "California",
"DE" => "Delaware"}
=> {"CA"=>"California", "DE"=>"Delaware"}

In real life, what lists do we make in key/value pairs?

exercises

Collection

Hash

Hash Indexing

Member pairs can be accessed by their key. So each hash key has to be unique.

Values don't have to be unique.

> states["CA"]
=> "California"

exercises

Methods

things that do stuff.

exercises

Boolean

A boolean is one of only two possible values: true or false.

  > 1 + 1 == 2
  => true
  > 1 + 1 == 0
  => false

( == means "is equal to". More on that later.)

exercises

Operators

Do stuff with objects

  > my_variable + 2
  => 7
  > my_variable * 3
  => 15
  > my_fruits = fruits + ["lychee"]
  => ["kiwi", "strawberry", "plum", "lychee"]
  > my_fruits = my_fruits - ["plum"]
  => ["kiwi", "strawberry", "lychee"]

exercises

Loop

Does something repeatedly

  > fruits.each do |fruit|
  ?> puts fruit
  > end
  kiwi
  strawberry
  plum
  => ["kiwi", "strawberry", "plum"]

On the second line, what does ?> indicate?

exercises

"I would like to visit Barcelona"
"I would like to visit Antigua"
"I would like to visit Alaska"
"I would like to visit New Orleans"

Conditional

Do something only if a condition is true

  > fruits.each do |fruit|
  ?> puts fruit if fruit == "plum"
  > end
  plum
  => ["kiwi", "strawberry", "plum"]

exercises

Running Your Code

Interpreter

Ruby is an interpreted language. Its code can't run by the computer directly. It first must go through a Ruby interpreter.

The most common interpreter is Matz's Ruby Interpreter ("MRI"). There are many others.

There are various ways to run code through a Ruby interpreter. We were using IRB earlier and now we will use a file.

Running code from a file

Create the file

Note which folder your terminal is currently in, this is your working directory

In your text editor, create a file named my_program.rb inside your working directory.

class Sample
  def hello
    puts "Hello World!"
  end
end

s = Sample.new
s.hello

Passing code from a file

Run the saved code

  $ ruby my_program.rb
  Hello World!

Passing code from a file

We can even load that file's code into IRB!

  $ irb
  > load 'my_program.rb'
  > second_time=Sample.new
  > second_time.hello

When might it be useful to do this?

Your Own Command Line Program

Hello World

hello.rb

puts "Hello, World!"

Arguments (ARGV)

hello.rb

puts "Hello, #{ARGV.first}!"

terminal

$ ruby hello.rb Alice
Hello, Alice!

Conditionals

hello.rb

if ARGV.empty?
puts "Hello, World!"
else
puts "Hello, #{ARGV.first}!"
end

terminal

$ ruby hello.rb
Hello, World!
$ ruby hello.rb Alice
Hello, Alice!

Object Oriented Programming (OOP)

Ruby is very object oriented

Nearly everything in Ruby is an object.

Class

Describes the generic characteristics of a single type of object.

What things of this type are.

e.g. Dog, Vehicle, Baby

Method

Defines behavioral characteristic.

What the things of the class's type do.

e.g. Chase, Drive, Talk

Variable

Defines attribute characteristic.

What things of the class's type have.

e.g. Breed, Model Year, Favorite Ice Cream

Instance

A specific incarnation of the class.

e.g. Rin Tin Tin, garbage truck, the neighbor's kid

Let's Create Projects!

Project 1:

Personal Chef Lab

(start at "4. Objects, Attributes, and Methods")

Topics:

Project 2:

Encryptor Lab

Topics:

Project 3:

Event Manager Lab

Topics:

Project 4:

Testing & More

A follow-up to EventManager focusing more on Ruby object decomposition and working with Command Line Interfaces and program control flow.

  1. Testing Topics: TDD, BDD, Rspec (stop at "Exceptions")
  2. Event Reporter Lab Topics: Object decomposition, working with Command Line Interfaces, and program control flow. Continues project created in Event Manager lab.

  3. Rspec

[contents]

deck.rb presentation

/

#