Forked from JoshCheek/the_object_model_is_just_a_linked_list
Created
October 22, 2015 16:52
-
-
Save imwithsam/16ad145d123dbf15db3d to your computer and use it in GitHub Desktop.
Object Model as Hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----- Classes ----- | |
class Node | |
attr_accessor :data, :next_node | |
def initialize(data, next_node) | |
@data = data | |
@next_node = next_node | |
end | |
end | |
class List | |
attr_accessor :head | |
def initialize(head) | |
@head = head | |
end | |
end | |
node3 = Node.new({c: 394}, nil) | |
node2 = Node.new({a: 2, b: 10}, node3) | |
node1 = Node.new({a: 1}, node2) | |
list = List.new(node1) | |
list.head.data[:a] # => 1 | |
list.head.next_node.data[:a] # => 2 | |
list.head.next_node.next_node.data[:c] # => 394 | |
# ----- Hashes ----- | |
basic_object = {methods: {equal?: 394}, superclass: nil} | |
object = {methods: {to_s: 2}, superclass: basic_object} | |
sting_ray = {methods: {flap: 1}, superclass: object} | |
marla = {class: sting_ray} | |
marla[:class][:methods][:flap] # => 1 | |
marla[:class][:superclass][:methods][:to_s] # => 2 | |
marla[:class][:superclass][:superclass][:methods][:equal?] # => 394 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment