Intro to Ruby

For Non-Programmers

RailsBridge Boston

slides linked at bit.ly/RBB-jan2017

Ruby is a Programming Language

Actually Running Ruby Code

From a Text File

From IRB

Values

my_name = "Aniket"
my_age = 32
age_of_my_cat = my_age - 22

Variable Names

Values

my_name = "Aniket"
my_age = 32
age_of_my_cat = my_age - 22


puts "My name is #{my_name}"
# My name is Aniket

Method

my_name = "Aniket"
my_age = 32
age_of_my_cat = my_age - 22


puts "My name is #{my_name}"
# My name is Aniket


puts "I am " + my_age + " and my cat is #{age_of_my_cat}"
# I am 32 and my cat is 10

Types of Data

  • Strings (of characters)
  • Numbers
  • Booleans
  • Collections
  • Others!
type_of_cat = "Tuxedo"

number_of_cats = 2

is_black = true
is_white = true
is_orange = false

Basic Types

cats_in_my_house = ["Charlotte", "Ivan"]

puts cats_in_my_house[0]
# Charlotte

puts cats_in_my_house[1]
# Ivan

Collections

Array

Hash

cat_ages = {"Charlotte" => 10, "Ivan" => 8}

puts cat_ages["Charlotte"]
# 10

puts cat_ages["Ivan"]
# 8
current_time = Time.now
puts current_time
# 2016-06-23 18:03:39 -0400

one_to_five = 1..5
puts one_to_five
# 1..5

no_value = nil
puts no_value
#

Others

Making Decisions

"Conditionals"

guest_name = "Sarah"

if guest_name == "Aniket"
  puts "Welcome home!"
elsif guest_name == "Sam"
  puts "Stay away!"
else
  puts "Hello #{my_name}!"
end

# "Hello Sarah!"
guest_name = "Sarah"

puts "Hey Ted" unless guest_name != "Ted"
#

guest_name = "Aniket"

puts "Welcome home!" if guest_name == "Aniket"
# Welcome home!

Repeating Yourself

Looping and Arrays

puts "I would like to pet Ivan."
# I would like to pet Ivan.

puts "I would like to pet Charlotte."
# I would like to pet Charlotte.

puts "I would like to pet Bri."
# I would like to pet Bri.

puts "I would like to pet Jade."
# I would like to pet Jade.
cats = ["Ivan", "Charlotte", "Bri", "Jade"]

cats.each do |cat|
  puts "I would like to pet #{cat}."
end

# I would like to pet Ivan.
# I would like to pet Charlotte.
# I would like to pet Bri.
# I would like to pet Jade.

A method attached to every Array

A "block"

cats = ["Ivan", "Charlotte", "Bri", "Jade"]

cats.each do |cat|
  puts "I would like to pet #{cat}." unless cat == "Bri"
end

# I would like to pet Ivan.
# I would like to pet Charlotte.
# I would like to pet Jade.
one_to_five = 1..5

one_to_five.each do |n|
  puts n * n
end

# 1
# 4
# 9
# 16
# 25

Questions?

Some Advice

Code As Cooking

"Objects"

Programming is like writing a recipe

Ingredients

Instructions

Recipes are all based on the same template which includes:

  • A name
  • One or more ingredients
  • One or more instructions

In Programming Terms...

Recipes are all based on the same class which includes:

  • A name
  • One or more attributes
  • One or more methods

This is an instance based on the cookie recipe class

In Ruby Code...

class ChocolateChipCookiesRecipe
  attr_accessor :butter, :sugar, :chocolate_bits, ...

  def cream
    ...
  end

  def beat
    ...
  end

  def stir
    ...
  end

  def bake(how_long)
    ...
  end
  ...
end

Name

Attributes

Methods

class, attr_accessor, def and end are all ruby keywords - part of the language

Parameter

class ChocolateChipCookiesRecipe
  attr_accessor :butter, :sugar, :chocolate_bits, ...

  def cream
    ...
  end

  def beat
    ...
  end

  def stir
    ...
  end

  def bake(how_long)
    ...
  end
  ...
end

my_recipe = ChocolateChipCookiesRecipe.new

Now we can store an instance of the ChocolateChipCookiesRecipe class in a variable

But what about the Sugar Cookies recipe?  Do I have to write the whole recipe over again?

Shared Behaviors

class CookiesRecipe
  def combine
    ...
  end

  def beat
    ...
  end

  def bake(how_long)
    ...
  end
end
class ChocolateChipCookiesRecipe < CookiesRecipe
  attr_accessor :butter, :sugar, :chocolate_bits, ...

  def stir
    ...
  end
end

chocolate_chip_recipe = ChocolateChipCookiesRecipe.new

ChocolateChipCookiesRecipe inherits from CookiesRecipe

"parent"

"child"

class ChocolateChipCookiesRecipe < CookiesRecipe
  attr_accessor :butter, :sugar, :chocolate_bits, ...

  def stir
    ...
  end
end

class SugarCookiesRecipe < CookiesRecipe
  attr_accessor :butter, :white_sugar, :baking_powder,...

  def blend
    ...
  end

  def decorate
    ...
  end
end

sugar_cookies = SugarCookiesRecipe.new

You've learned the basics of "Object-Oriented Programming"

A little more advice

Thanks For Listening!

Questions?

Made with Slides.com