12-01-2022

584 words 3 minutes Ruby OOP

Into to Hashes

an array, where each array has a unique key a set of keys and locks a dictionary, lookup table

Default value for key
h = Hash.new { |hash, key| "Default value for #{key}" }

Chapter 9, Ruby Methods

  • Ruby procedures are wrapped in methods which are within Ruby objects
  • When calling a method in Ruby, you are implicitly calling an object EXAMPLE
1.positive?
returns true

within the 1 object:

Inside 1

if self > 0
  return true
else
  return false
Perhaps 1 is an instance of Integer?

remember an iterator loop defines the closure on line one of the loop

iterableObj.each do |element|
	...logic...
end

Methods (or functions) are the abstraction of logic (to run on n variables of the same type)

Using procedural programmes, the same logic could be defined n times, over pages of code, for n data / variables.

why not abstract the logic, which can take n data as an input?

Ruby Methods are created with the def kword and finished with the end kword

the return keyword returns a value, and breaks out of the code block - anything after the return statement wont run

Ruby, like Rust, has implicit returns The last line of the method will become the return value

  • When a Ruby method contains nothing, it will secretly return nil
  • when calling a method on an object, you will often need to use arguments
object.method(object, object)

METHODS SHOULD DO ONE THING, AND DO IT WELL

  • Single Responsibility Priniciple

Picking the right abstraction

  • can you name your method in a simple way eithout using the word and
  • does your method do one thing, and only one thing well
  • Can you name your method after what it returns instead of what it does
    • average(scores) better than averages_scores(test_scores)
    • score(hand) better than scores_cards(hand)

Chapter 10

  • often we control the programmw in an imperative way
  • we do this using control flow, conditionals, loops, and simple objects like arrays, strings, hashes, and integers.

its thyme 4 OOPs

  • previously, we used methods without declaring the objects
def some_function
	logic
end
  • when a message isn't sent to a specific object is it instead sent to the main programme object
  • in the past lessons we have been defining our procedures on the main-programme-object, and hence, we have only one procedure - a procedural programme
  • remember that the main programme object is, itself, an instance of the Object class
class Object
  def average(scores)
    scores_accumulator = 0

    scores.each do |score|
      scores_accumulator += score
    end

    scores_accumulator.to_f / scores.length
  end
end

average([1, 2, 3])
  • in the above, think of the class declaration as opening up the class to tinker with its inner workings

You can even define further methods on present objects, ( like lisp!)

class String
  def say_hi
    "Hi there!"
  end
end

my_object = "A string!"
my_object.say_hi
output: Hi there!
Let's say we want
[1, 2, 3].average
  • we will need to use a class to do this
  • we will add this method onto the Array class
class Array
  def average
    # Somehow, do the averaging in here
  end
end
  • we then need to access an instances own methods
  • we do this using the self keyword ( javascript this., python self. )

resulting in

class Array
  def average
    accumulator = 0

    self.each do |element|
      accumulator += element
    end

    accumulator.to_f / self.length
  end
end

[1, 2, 3].average
shoutify
class String
  def shoutify
    self.upcase
  end
end

term output hello = "hello world!" puts hello puts hello.shoutify


Dog = Class.new

class Dog
	def bark
		return "Woof!"
	end
	
	def walk
		return "Hey I'm walkin'hereee"
	end
	
	def colour=(colour)
		@colour = colour
	end
	
	def observe
		return "You see a #{@colour} dog"
end

fido = Dog.new
fido.bark
fido.colour = "brown"
fido.observe

if the class doesn't already exist

ruby will create the class with the given name

another example of the Dog class above, might be the Person class below

class Person
	def give_a_name(name)
		@my_name = name
	end
	
	def introduce
		return "Hello, i'm " + @my_name
	end
end

woman = Person.new
woman.give_a_name("Scarlett")
woman.introduce
termoutput = "Hello, i'm Scarlett"

Setters and Getters

  • setters are methods that determine object state
  • getters are method that return object state to the end-user

@methods

  • use within classes to self reference variables
  • use intialize to sset up instances

initializers

  • calling new on a class builds an the class object, and then runs the method initialize on the class-object
  • every time the '.new' method is passed to the initialize function
  • Ruby will automatically run the initialize method, even if it isn't explicitly written in the

EXTRA

Apprenticeship Provider use 'interfaces' where the term is parameters; and this is a GOOD THING!!!