Skip to content

Instantly share code, notes, and snippets.

@sferik
Created June 4, 2013 23:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/5710600 to your computer and use it in GitHub Desktop.
Save sferik/5710600 to your computer and use it in GitHub Desktop.
# [X] length/size
# [X] push
# [X] [] (slice)
# [X] pop
# [X] first
# [X] last
# [X] shift
# [X] unshift
# [X] each
# [X] select/reject/collect/inject
# [ ] inspect
class EriksArray
include Enumerable
def initialize
@size = 0
end
def inspect
"[#{map(&:inspect).join(', ')}]"
end
def push(object)
instance_variable_set("@var#{@size}".to_sym, object)
@size += 1
self
end
alias :<< push
def pop
@size -= 1
remove_instance_variable("@var#{@size}".to_sym)
end
def slice(index)
instance_variable_get("@var#{index}".to_sym) unless index < 0 # || index >= @size
end
alias :[] slice
def size
@size
end
alias length size
def last
# instance_variable_get("@var#{@size - 1}".to_sym)
# self.slice(@size - 1)
self[@size - 1]
end
def first
self[0]
end
def shift
# before: ["a", "b", "c", "d"]
# shift
# after: ["b", "c", "d"]
val = first
@size -= 1
@size.times do |i|
next_val = instance_variable_get("@var#{i + 1}".to_sym)
instance_variable_set("@var#{i}".to_sym, next_val)
end
remove_instance_variable("@var#{@size}".to_sym)
val
end
def unshift(object)
# before: ["b", "c", "d"]
# unshift("a")
# after: ["a", "b", "c", "d"]
@size.times do |i|
val = instance_variable_get("@var#{@size - i - 1}".to_sym)
instance_variable_set("@var#{@size - i}".to_sym, val)
end
instance_variable_set("@var0".to_sym, object)
@size += 1
self
end
def each(&block)
@size.times do |i|
block.yield self[i]
end
end
end
require 'rspec'
describe EriksArray do
describe ".new" do
it "initalizes with a size of zero" do
array = EriksArray.new
expect(array.size).to eq 0
end
end
describe "#push" do
it "pushes an object on to the end of the array" do
array = EriksArray.new
array.push(5)
expect(array.size).to eq 1
expect(array.slice(0)).to eq 5
expect(array[0]).to eq 5
end
end
describe "#pop" do
it "pops an object off the end of the array" do
array = EriksArray.new
array.push(5)
expect(array.pop).to eq 5
expect(array.length).to eq 0
end
end
describe "#first" do
it "returns the first element" do
array = EriksArray.new
array.push(5)
array.push(6)
expect(array.first).to eq 5
end
end
describe "#last" do
it "returns the last element" do
array = EriksArray.new
array.push(5)
array.push(6)
expect(array.last).to eq 6
expect(array.size).to eq 2
end
end
describe "#shift" do
it "shift the first element off the array" do
array = EriksArray.new
array.push(5)
array.push(6)
before_size = array.size
object = array.shift
expect(object).to eq 5
expect(array.size).to eq before_size - 1
expect(array[0]).to eq 6
expect(array[1]).to be_nil
end
end
describe "#unshift" do
it "adds an element to the front of the array" do
array = EriksArray.new
array.push(5)
before_size = array.size
expect(array[0]).to eq 5
expect(array[1]).to be_nil
object = array.unshift(4)
expect(object).to be_an EriksArray
expect(array.size).to eq before_size + 1
expect(array[0]).to eq 4
expect(array[1]).to eq 5
end
end
describe "#each" do
it "iterates through each element in the array" do
array = EriksArray.new
array.push(5)
array.push(10)
array.push(15)
num = 0
array.each{|i| num += i}
expect(num).to eq 30
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment