Object-Oriented Warmups I

Ruby practice, Cloning Enumerable, Rock Paper Scissors and More

Scroll down...

Content

Resources

Comments

We'll start with a few short Ruby problems to get you warmed up before diving into the OOP.

Getting Started

  1. Fork and clone the Github Repo to get started.

Warmup 1: Ruby Methods

The following methods don't even require OOP, so just write them out as individual methods.

If you find yourself unsure of what's going on inside a method, don't forget to use binding.pry to pause its execution and examine its variables. This is a critical tool to get familiar with!

My_reverse

Build a method that reverses any string you put into it, and don't use the built-in reverse.

puts my_reverse("Blah!")
#=> !halB

Fibonacci Sequence

Write a method that takes an integer and spits out an array of that many elements of the Fibonacci Sequence, which starts with 0 and 1 and computes the next element by adding the most recent two elements together.

fibs(3)
#=> [0,1,1]

fibs(8)
#=> [0,1,1,2,3,5,8,13]

Warmup 2: Benchmarker

Write a method, my_benchmark, that takes a parameter number_of_times and a block and returns how long that block took to run that block, that many times. Remember, you can get the current time using Time.now, and you can subtract one time from another to get a duration in seconds.

my_benchmark(10000) { puts "hi" }
#=> 0.056918

Warmup 3: Rebuild the Enumerable Methods

The Enumerable module is often misunderstood, so you'll get to build it from the ground up. After this, you'll never wonder what the heck these things are actually doing.

My_each

Create a method my_each which extends the Array class and has identical functionality to Ruby's native each method (obviously don't use each in the implementation). Make it take either a block or a proc.

> [1,2,5].my_each{ |item| puts item }
# 1
# 2
# 5
#=> [1,2,5]
> my_proc = Proc.new{|item| puts item**2}
#=> #<Proc:0x007fbb6b9c1cc8@(irb):2>
> [1,2,5].my_each(my_proc)
# 1
# 4
# 25
#=> [1,2,5]

My_map

Create a method my_map which extends the Array class and has identical functionality to Ruby's native map method. You can use your my_each method to implement this. Make it take either a block or a proc.

> [1,2,5].my_map do |item|
>   item ** 2
> end
#=> [1,4,25]

My_select

Create a method my_select which extends the Array class and has identical functionality to Ruby's native select method. You can use your my_each method to implement this. Make it take either a block or a proc.

> my_proc = Proc.new{|item| item.even?}
#=> #<Proc:0x007fbb6b9c1ff8@(irb):2>
> [1,2,5].my_select(my_proc)
#=> [2]

My_all?

Create a method my_all? which extends the Array class and works the same as Ruby's native all? method. You can use your my_each method to implement this. Make it take either a block or a proc.

> my_proc = Proc.new{|item| item.even?}
#=> #<Proc:0x007fbb6b9c1ff8@(irb):2>
> [1,2,5].my_all?(my_proc)
#=> false

My_inject

Create a method my_inject which extends the Array class and works the same as Ruby's native inject method. You can use your my_each method to implement this. Make it take either a block or a proc.

> [1,2,5].my_inject(0) do |memo, item|
>   memo + item
> end
#=> 8

OOP 1: Rock Paper Scissors

Rock Paper Scissors is as simple as object-oriented programming challenges get, so it'll be another good warmup.

Your Task

Create a command line Rock-Paper-Scissors game, starting with one player against the computer. Think through what you need beforehand and write it down. What classes do you need? What methods? Then get started.

Start with one player against the computer and then add 2-player mode.

OOP 2: Tower of Hanoi

Towers of Hanoi Animation

Hopefully, you're pretty familiar with the game Tower of Hanoi by this point. For this assignment, you'll need to adapt your Tower of Hanoi game to OOP. Since last time you built this using a single giant script, there is plenty of room for improvement.

Rebuild Tower of Hanoi using good object-orientation. That means separating out your functionality into logical objects with methods and instance variables.

Wrapping Up

You've built a lot of stuff this assignment. Great work!

The final step is to submit the assignment back to the upstream repo on Github as you have with the others.

  1. Push your latest version up to your origin fork
  2. Submit your fork as a pull request to the upstream repo.
Icon pivotaltracker 300 Pull request 300 Octocat 300


Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Classes vs Modules