These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
Scroll down...
We'll start with a few short Ruby problems to get you warmed up before diving into the OOP.
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!
Build a method that reverses any string you put into it, and don't use the built-in reverse
.
puts my_reverse("Blah!")
#=> !halB
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]
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
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.
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]
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]
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]
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
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
Rock Paper Scissors is as simple as object-oriented programming challenges get, so it'll be another good warmup.
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.
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.
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.
origin
fork