Skip to content

Instantly share code, notes, and snippets.

@M0119
Last active March 19, 2019 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0119/7f96bae4dbe8010fe5836afbf86023b6 to your computer and use it in GitHub Desktop.
Save M0119/7f96bae4dbe8010fe5836afbf86023b6 to your computer and use it in GitHub Desktop.

Lecture ✍️

A scope is where you can refer to a variable. A block defines a block scope. A variable defined inside a block will be defined only inside that block and you can't reference it after the end of block. The scope of a local-variable declaration is from the point at which the declaration appears to the end of that block.

This is a great explanation of the difference between scope and block.

Block parameters are always scoped local to the block. The variable item is created on the first line of the loop and exists only inside the loop.

The following code will cause the error "undefined local variable or method item".

(1..5).each do |item|
    item = item + 1
end
puts item

Block parameters are always scoped local to the block.

In this example the variable current created before the loop. In the first line of the loop another variable called current is created and exists only inside the loop. It will not change the variable outside of the loop. It will puts 2019.

current = 2019
(1..10).each do |current|
  current = current + 1
end
puts current 

The variable name is created outside of the loop and changed inside of the loop.

name = "Bob"
counter = 0 
('Ted'..'Zed').each do |n|
  name = n
end
puts name

The variable name is created outside of the if and changed inside of the if with a true condition.

name = "Ted"
if true
  name = "Bob"
end
puts name 

The variable name is created outside of the if and NOT changed inside of the if with a false condition.

name = "Ted"
if false
  name = "Bob"
end
puts name 

Class and objects review

Methods that act on the data (like drive and is_vintage) are inside the class and hide the implementation.

require 'date'

class Car
    def initialize(make, year, mileage, color)
        @make = make 
        @year = year 
        @mileage = mileage 
        @color = color
    end

    def drive(distance)
        @mileage += distance
    end

    def is_vintage?()
      if (Date.today.year - @year > 30)
        return true
      end
      return false
    end

    def description()
        return "Make: #{@make}, Year: #{@year}, Mileage: #{@mileage}, Vintage: #{is_vintage?()}"
    end
end

Instantiating a Car object

We use .new to create an instance of a class.

work_car = Car.new('Toyota', 2018, 5000)

Accessing methods on the car object

When we create an instance we can call instance methods on it.

work_car = Car.new('Toyota', 2018, 5000)
puts ("The work car is #{work_car.description()}")
work_car.drive(100)
puts ("The work car is #{work_car.description()}")

Accessing attributes

A getter simply returns the instance variable we want to read.

class Hamburger
    def initialize(patty, bun)
      @patty = patty
      @bun = bun  
    end
  
    def get_patty()
      return @patty
    end
    
    def get_bun()
      return @bun
    end

    def to_string()
      "#{@patty} patty on a #{@bun} bun"
    end
  end
  
  hamburger1 = Hamburger.new('beef', 'brioche') 

  puts (hamburger1.to_string())
  puts ("This burger contains #{hamburger1.get_patty()}")

Accessors prevent us from having to define getter methods.

class Hamburger
    attr_reader(:patty, :bun)

    def initialize(patty, bun)
      @patty = patty
      @bun = bun  
    end

    def to_string()
      "#{@patty} patty on a #{@bun} bun"
    end
  end
  
  hamburger1 = Hamburger.new('beef', 'brioche') 

  puts (hamburger1.to_string())
  puts ("This burgger contains #{hamburger1.patty}")
  puts hamburger1.bun
# Build a Cat class.
# Each cat should have a name
# Implement a speak method to say meow.
# Add each cat's name to the speak method (Pixie says meow).
# class Dog
# def initialize(dog_name, dog_age)
# @name = dog_name
# @age = dog_age
# puts "I've been initialized!"
# end
# def speak
# puts "#{@name} says woof! I am #{@age} years old."
# end
# end
# doggo = Dog.new('Rover', 2)
# doggo.speak
# pupper = Dog.new('Lassie', 5)
# pupper.speak
# Using the above code as a starting point:
# We've got name and age, let's give each dog a location too. Test your location works by printing the dog's location.
# doggo = Dog.new('Rover', 2, 'Brisbane')
# puts doggo.location # -> Brisbane (You might get a no method error, remember attr_accessor?)
# Create a walk method. This should say "I have been for X walks.". Every time you call walk, the number should increase by one E.g:
# doggo = Dog.new(...)
# doggo.walk # -> I have been for 1 walks today
# doggo.walk # -> I have been for 2 walks today
# Update the walk method so it only increases the walk count. Create a new method to display walks. E.g.: doggo = Dog.new(...)
# doggo.walk # -> Shouldn't print anything on screen
# doggo.walk # -> Shouldn't print anything on screen
# doggo.display_walks # -> I have been for 2 walks today
# Update the walk method so you can chain walk commans. E.g.: doggo = Dog.new(...)
# doggo.walk.walk.display_walks # -> I have been for 2 walks today
# Optional
# Improve the walk method. Accept a location and a distance. e.g. doggo.walk('Brisbane', 20)
# Update your display_walks method to show all of the walk details.
# Implement a total_distance method to calculate the total distance of all the walks.
# Track the time each walk is created. Update display_walks to show it back to your user in a readable format.
# Research ruby gems. Use a gem to geocode a location and store it's latitude + longitude. E.g. "Brisbane" stores -27.44888,153.00531
# Implement a BankAccount class into your bank app from earlier in the week. You should be able to create and manage accounts & balances from the command line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment