Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...

Twitter:
@daniel_moralesp

2019-09-06

Ruby OOP Challenges

If you have been following the previous blog post, you now have enough theoretical knowledge to understand Classes and Objects in Ruby. If you don't, we have here the order to study this subject

  1. 1. Introduction to Object-Oriented Programming in Ruby
  2. 2. Methods and Attributes in Object-oriented programming in Ruby
  3. 3. Ruby Getters and Setters in Object-oriented Programming

Just to recap, let's see these essential concepts:

  • * A Class is a "template" to create Objects
  • * An Object is an "instance" of a Class
  • * Classes can have the "initialize" method (commonly known as "The Constructor"), which is called each time an object is created
  • * The Constructor is commonly used to "initialize" object attributes
  • * Inside the Class, we can define Methods (the behavior) and Attributes (the information) that Objects will have. Methods are usually called "Instance Methods," and Attributes are commonly called "Instance Variables."
  • * Attributes are info that is associated with the Object
  • * Attributes are "private" by default and can just be read and modified inside the Object unless you create methods to Read (getters) and Write (setters) outside the Object
  • * You can use the "accessors" called "attr_accesor" (to read and write), attr_reader (to read), attr_writter (to write), and Ruby will create, for us, the methods to read and write

As you can see, we've advanced a lot in OOP, and you should have the above concepts at hand, to solve the following challenges. 

Challenges


Challenge #1
Write a class called Dog. The class must have a single "bark" method that returns "woof-woof."

Challenge #2
  • * Create a class called "Square."
  • * Add a constructor to "Square" who receives an argument called "length" and assign it to an attribute with the same name
  • * Add methods to read and write the "length" attribute
  • * Add a method called "area" that returns the area of square
  • * Add a method called "perimeter" that returns the perimeter of the square
  • * Create 2 "Square" instances, the first with a longitude of 5 and the second with a longitude of 10. Check that it returns the correct data

Challenge #3
Your mission in this challenge is to write a class called Car that behaves like a simplified car that allows us to accelerate, brake, and obtain its current speed. The Car class must be used as follows:

car = Car.new
car.velocity # => 0

car.accelerate
car.velocity # => 1

car.accelerate(2)
car.velocity # => 3

car.brake
car.velocity # => 2

car.brake(2)
car.velocity # => 0

Note: It should not be possible to assign the Car's speed directly.

car.velocity = 100 # Should fail.

Instructions
  • * Create the Car class (we suggest you do this on your machine first to test it).
  • * Add the necessary methods and attributes to implement the class.


Challenge #4
  • * Create a class called GoodDog
  • * Pass a parameter called "name" to the initializer and store it in an instance variable.
  • * The instance variable can be read and modified from outside the class.
  • * Create a class method that is called "speaks" and prints "(name) says arf!"
  • * Instantiate the class, pass it the parameter name, and use the variable name in the peaks method
  • * Modify the name outside the class and call it again by the new name. 


Following the Framework to Solve Coding Challenges

In one of our last blog posts, we talked about the framework to solve coding challenges, so now is the time to put that into practice. Remember the steps

  1. 1. Use an online stopwatch
  2. 2. Read the problem carefully
  3. 3. Never try to see the result until you try it yourself.
  4. 4. What's the expected output from the challenge? 
  5. 5. Did I solve a similar problem before? How?
  6. 6. Start your own documentation or cheatsheet
  7. 7. Solve the problem first on paper or a whiteboard
  8. 8. Start using the Ruby Interactive Console (IRB) or the text editor
  9. 9. Solve it first via "brute force." 
  10. 10. Review the time & space complexity


Please take your time to do it by yourself now...
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------


Our solution

Remember that there are always different ways to solve the same problem. So you probably solved it with a different approach, and that's ok. Actually, what matters is: "you solve it." Now let's check this solution

Challenge #1

class Dog
  def bark
    "woof-woof"
  end
end

d = Dog.new
puts d.bark

Print

$ ruby oop-challenge-1.rb
"woof-woof"

Challenge #2

class Square
  attr_accessor :length

  def initialize(length)
    @length = length
  end

  def area
    @length ** 2
  end

  def perimeter
    @length * 4
  end
end

s1 = Square.new(5)
s2 = Square.new(10)

puts s1.length
puts s1.area
puts s1.perimeter


puts s2.length
puts s2.area
puts s2.perimeter

Print

$ ruby oop-challenge-2.rb
5
25
20
10
100
40

Challenge #3

This challenge is a little bit more complicated. As the first step, we need to consider understanding the expected output. For example, the method "accelerate" needs to receive a parameter, which should be as default. Please refer to the functions blog post to understand default parameters.

class Car
  attr_reader :velocity

  def initialize(velocity=0)
    @velocity = velocity
  end

  def velocity
    @velocity
  end

  def accelerate(n=1)
    @velocity += n
  end

  def brake(n=1)
    @velocity -= n
  end
end

car = Car.new
puts car.velocity # => 0

car.accelerate
puts car.velocity # => 1

car.accelerate(2)
puts car.velocity # => 3

car.brake
puts car.velocity # => 2

car.brake(2)
puts car.velocity # => 0

Prints

$ ruby oop-chellenge-3.rb
0
1
3
2
0


Challenge #4

class GoodDog
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def speaks
    "#{@name} says arf!"
  end
end

d1 = GoodDog.new("Odin")
puts d1.speaks

d1.name = "Paco"
puts d1.speaks
I hope you learned a lot.

Prints

$ ruby oop-challenge-4.rb
Odin says arf!
Paco says arf!

I hope you solved all challenges!

Thanks for reading,
Daniel Morales