Skip to content

Instantly share code, notes, and snippets.

@StephanieSunshine
Created June 17, 2015 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephanieSunshine/46a223317ae3f0858606 to your computer and use it in GitHub Desktop.
Save StephanieSunshine/46a223317ae3f0858606 to your computer and use it in GitHub Desktop.
An example of a hash made from just arrays
#!/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