Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created April 18, 2011 04:38
Show Gist options
  • Save RyanScottLewis/924824 to your computer and use it in GitHub Desktop.
Save RyanScottLewis/924824 to your computer and use it in GitHub Desktop.
trying to make an Array without.. well.. an Array
class Node
attr_accessor :index, :next, :value
def initialize(index, value)
@index, @value, @next = index, value, nil
end
end
class NodeList
include Enumerable
def [](index); find { |node| node.index == index }; end
def []=(index, value)
self[index].value = value
end
def each(&blk)
current_node = first
loop do
yield(current_node)
current_node = current_node.next
break if current_node.nil?
end
end
def to_s
(0...count).inject("") do |result, index|
result << self[index].value << "#{',' unless index == count-1} "
end
end
def <<(value)
node = Node.new(@count, value)
@first = node if @first.nil?
@last.next = node unless @last.nil?
@last = node
end
end
list = NodeList.new
list << "wtf"
list << "omg"
list << "lol"
# p list[2].value # => "lol"
# list[2] = "BLArb!"
# p list[2].value # => "BLArb!"
puts list.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment