Ruby core and the standard library offer a tremendous amount of functionality right out of the box. But how is that functionality implemented?
In this series of challenges, let's try to re-implement some common methods.
For these array exercises, start with the following template:
class SimpleArray
attr_reader :data
def initialize(the_data)
@data = the_data
end
end
Which can be used like...
an_array = SimpleArray.new(['a', 'b', 'c'])
an_array.some_method
Presuming that you've added a method like some_method
per the descriptions
below.
To warmup, can you implement first
and last
methods on SimpleArray
which
do not use the corresponding first
and last
of Array
?
an_array = SimpleArray.new(['a', 'b', 'c'])
an_array.first # => "a"
an_array.last # => "c"
Can you implement a method that mimics join
without using the join
offered
by Array
? Your interaction might look like:
an_array = SimpleArray.new(['a', 'b', 'c'])
an_array.join # => "abc"
an_array.join('-') # => "a-b-c"
How about an implementation of count
without using the count
from Array
?
an_array = SimpleArray.new(['a', 'b', 'c'])
an_array.count # => 3
other_array = SimpleArray.new
other_array.count # => 0
For these string exercises, start with the following template:
class SimpleString
attr_reader :data
def initialize(the_data)
@data = the_data
end
end
Which can be used like...
an_array = SimpleString.new("hello")
an_array.some_method
Presuming that you've added a method like some_method
per the descriptions
below. Remember that strings are basically arrays of characters.
How many characters are in your SimpleString
?
a_string = SimpleString.new("hello")
a_string.length # => 5
second_string = SimpleString.new("")
second_string.length # => 0
Don't use the length
built into String
.
You might not be familiar with the end_with?
method. It works like this:
a_string = SimpleString.new("hello")
a_string.end_with?("o") # => true
a_string.end_with?("x") # => false
a_string.end_with?("ello") # => true
a_string.end_with?("hllo") # => false
Don't use the methods end_with?
or start_with?
built-in to String
to create
your implementation.
But, as an extra exercise, think about a solution that does rely on the start_with?
in String
.
Implement an upcase
method that capitalizes all the letters in the SimpleString
:
a_string = SimpleString.new("hello")
a_string.upcase # => "HELLO"
second_string = SimpleString.new("BYE")
second_string.upcase # => "BYE"
third_string = SimpleString.new("")
third_string.upcase # => ""
There are at least two approaches: one using a hash to map characters and the second
that involves math. Neither need's the upcase
from String
.
Reimplement +
? Seriously?
a_string = SimpleString.new("hello")
second_string = SimpleString.new("BYE")
a_string + second_string # => "helloBYE"
Don't use +
, <<
, or concat
from String
.