Intro to Ruby

For Non-Programmers

RailsBridge Boston

slides.com/aniketschneider/intro-ruby

Ruby is a Programming Language

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

Argument (input)

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


puts "My name is #{my_name}"
# My name is Aniket
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
neighborhood_cat = cat_living_at("1337 Mass Ave")
puts neighborhood_cat
# Paco

other_neighborhood_cat = cat_living_at("123 Frost St")
puts other_neighborhood_cat
#

puts other_neighborhood_cat.nil?
# true

puts neighborhood_cat.nil?
# false

Nil

Actually Running Ruby Code

From a Text File

Interactively

irb, rails console

Making Decisions

"Conditionals"

guest_name = "Sarah"

if guest_name == "Aniket"
  puts "Welcome home! Feed me."
elsif guest_name == "Ravi"
  puts "Aaah! Hide!"
else
  puts "Hello #{my_name}! Pet me."
end

# "Hello Sarah! Pet me."
guest_name = "Aniket"

puts "Welcome stranger! Pet me." if guest_name != "Aniket"
#

guest_name = "Sarah"

puts "Welcome stranger! Pet me." unless guest_name == "Aniket"
# Welcome stranger! Pet me.

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.

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 cream
    ...
  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

superclass

subclass

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

What Does Rails Have To Do With This?

Thanks For Listening!

Questions?

Made with Slides.com