Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Blocks in the Wild: Strings and Integers 4:58
- Block Method Practice: Strings 1 objective
- Blocks in the Wild: Arrays and Hashes 7:06
- Block Method Practice: Arrays 1 objective
- Build a Simple Benchmarker 6:31
- Block Method Practice: Custom Classes 2 objectives
- Implementing Block Methods 3:30
- Implementing Block Methods 1 objective

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Blocks are easy to use in our classes. In this video, we'll practice writing a class that mimics arrays.
Code Samples
class MyArray
attr_reader :array
def initialize
@array = []
end
def push(item)
array.push(item)
end
def each(&block)
i = 0
while i < array.length
block.call(array[i])
i += 1
end
array
end
end
my_array = MyArray.new
my_array.push(1)
my_array.push(2)
my_array.push(3)
my_array.each{ |item| puts "item: #{item}" }
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Hajun Lee
895 Points1 Answer
-
dotz
6,733 Points1 Answer
-
Ieva Salte
15,356 Pointsdoes array in the class MyArray needs not be denoted by @array throughout the class?
Posted by Ieva SalteIeva Salte
15,356 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up