Created
June 17, 2015 20:13
-
-
Save StephanieSunshine/46a223317ae3f0858606 to your computer and use it in GitHub Desktop.
An example of a hash made from just arrays
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
#!/usr/bin/env ruby | |
require 'pp' | |
class BastardHashing | |
#Two arrays, index is the linking column | |
def initialize() | |
@keys = Array.new | |
@values = Array.new | |
end | |
def add ( key, value ) | |
@keys.push(key) | |
@values.push(value) | |
end | |
def delete ( key ) | |
return nil if @keys.index(key).nil? | |
@keys.delete_at(@keys.index(key)) | |
return @values.delete_at(@keys.index(key)) | |
end | |
def update ( key, value ) | |
return nil if @keys.index(key).nil? | |
return @values[@keys.index(key)] = value | |
end | |
def value ( key ) | |
return nil if @keys.index(key).nil? | |
return @values[@keys.index(key)] | |
end | |
def list_keys | |
return @keys | |
end | |
def list_values | |
return @values | |
end | |
end | |
myHash = BastardHashing.new | |
myHash.add("Peaches", 23) | |
myHash.add("Oranges", 35) | |
myHash.add("Mangos", 36) | |
myHash.add("Pears", 21) | |
myHash.add("Plums", 11) | |
pp myHash.list_keys | |
pp myHash.list_values | |
pp myHash.value("Peaches") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment