Skip to content

Instantly share code, notes, and snippets.

@iHiD
Created March 31, 2020 11:58
Show Gist options
  • Save iHiD/e027388aae0615b4f4d78c17b33b53b8 to your computer and use it in GitHub Desktop.
Save iHiD/e027388aae0615b4f4d78c17b33b53b8 to your computer and use it in GitHub Desktop.
Ruby Basics
class Lasagna
EXPECTED_MINUTES_IN_OVEN = 40
PREPERATION_MINUTES_PER_LAYER = 2
def remaining_minutes_in_oven(actual_minutes_in_oven)
expected_minutes_in_oven - actual_minutes_in_oven
end
def preperation_time_in_minutes(number_of_layers)
number_of_layers * PREPERATION_MINUTES_PER_LAYER
end
def total_time_in_minutes(number_of_layers:, actual_minutes_in_oven:)
preparation_time_in_minutes(number_of_layers) + actual_minutes_in_oven
end
end

1. Define the expected oven time in minutes

  • You need to define a [constant][constant] which should contain the [integer][integers] value specified in the recipe.

2. Calculate the remaining oven time in minutes

3. Calculate the preparation time in minutes

4. Calculate the total working time in minutes

In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.

You have four tasks, all related to the time spent cooking the lasasgna.

1. Define the expected oven time in minutes

Define the Lasagna::EXPECTED_MINUTES_IN_OVEN constant that returns how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:

Lasagna::EXPECTED_MINUTES_IN_OVEN
# => 40

2. Calculate the remaining oven time in minutes

Define the Lasagna#remaining_minutes_in_oven method that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.

lasagna = Lasagna.new
lasagna.remaining_minutes_in_oven(30)
# => 10

3. Calculate the preparation time in minutes

Define the Lasagna.preperation_time_in_minutes method that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.

lasagna = Lasagna.new
lasagna.preperation_time_in_minutes(2)
# => 4

4. Calculate the total working time in minutes

Define the Lasagna#total_time_in_minutes method that takes two named parameters: the first parameter is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven. The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.

lasagna = Lasagna.new
lasagna.total_time_in_minutes(number_of_layers: 3, actual_minutes_in_oven: 20);
# => 26

Ruby is a dynamic [object-oriented language]. Everything in Ruby is an [object].

There are two ways to assign objects to names in Ruby - assigning variables or constants. Variables always start with lower-case letters and use "snake case" for their formatting. A variable can be redefined to different objects over its lifetime. For example, my_first_variable can be defined and redefined many times using the = operator:

my_first_variable = 1
my_first_variable = "Some string"
my_first_variable = SomeComplexObject.new

Constants, however, can only be assigned once. The must start with captial letters and are normally written in block capitals with words seperated by underscores. For example:

MY_FIRST_CONSTANT = 10

# Redefining not allowed
# MY_FIRST_CONSTANT = "Some String"

Ruby is organised into classes. Classes are defined using the class keyword followed by the name of the class. Classes are initialized using the .new method.

# Define the class
class Calculator
  #...
end

# Create an instance of it and assign it to a variable
my_first_calc = Calculator.new

A function within a class is referred to as a method. Each method can have zero or more parameters. Objects are returned from methods using the return keyword. If no return value is specified, the final object in the method is returned instead. Methods can also have named parameters which are defined and called using the : syntax. Methods are invoked using . syntax.

class Calculator

  # Unnamed params
  def add(num1, num2)
    return num1 + num2 # Explicit return
  end

  # Named params
  def multiply(num1:, num2:)
    num1 * num2 # Implicit return
  end
end

calc = Calculator.new
calc.add(1, 3)
calc.multiply(num1: 2, num_2: 5)
class Lasagna
# TODO: Define the 'EXPECTED_MINUTES_IN_OVEN' constant
# TODO: Define the 'remaining_minutes_in_oven' method
# TODO: Define the 'preperation_time_in_minutes' method
# TODO: Define the 'total_time_in_minutes' method
end
# frozen_string_literal: true
require 'minitest/autorun'
require_relative 'lasagna'
class LasagnaTest < Minitest::Test
def test_expected_minutes_in_oven
assert_equal 40, Lasagna::EXPECTED_MINUTES_IN_OVEN
end
def test_expected_minutes_in_oven
assert_equal 15, Lasagna.new.remaining_minutes_in_oven(25)
end
def preperation_time_in_minutes_with_one_layer
assert_equal 2, Lasagna.new.preperation_time_in_minutes(1)
end
def preperation_time_in_minutes_with_multiple_layers
assert_equal 8, Lasagna.new.preperation_time_in_minutes(4)
end
def test_total_time_in_minutes_for_one_layer
assert_equal 32, Lasagna.new.total_time_in_minutes(
number_of_layers: 1,
actual_minutes_in_oven: 30
)
end
def test_total_time_in_minutes_for_multiple_layer
assert_equal 16, Lasagna.new.total_time_in_minutes(
number_of_layers: 4,
actual_minutes_in_oven: 8
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment