Created
June 4, 2011 18:50
-
-
Save rhysd/1008195 to your computer and use it in GitHub Desktop.
practice for Ruby.
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/local/bin/ruby | |
class OpenHash | |
def initialize elems,f = ->(ob){ return ob.hash} | |
@hashf = f | |
@val = { } | |
elems.each do |elem| | |
insert elem | |
end | |
end | |
def insert elem | |
v = @hashf.call elem | |
if @val[@hashf.call elem] == nil | |
@val[@hashf.call elem] = [elem] | |
else | |
@val[@hashf.call elem].push elem | |
end | |
end | |
def contain? elem | |
v = @hashf.call elem | |
return (@val[v] != nil and @val[v].index elem !=nil) | |
end | |
def remove elem | |
v = @hashf.call elem | |
if @val[v] !=nil and @val[v].index elem != nil | |
@val[v].delete elem | |
end | |
end | |
def showall | |
@val.each do |key,list| | |
puts "#{key}: #{list}" | |
end | |
end | |
def getall | |
retval = [] | |
@val.each do |key,list| | |
list.each do |e| | |
retval.push e | |
end | |
end | |
return retval | |
end | |
end | |
sample = "alskjfak","jfks","dddd","soso",123213,22.55,"dkfdskjkfdla","a","alkdkdkdkdkdkdkdkd" | |
puts "oh" | |
oh = OpenHash.new sample | |
oh.showall | |
puts "oh2" | |
oh2 = OpenHash.new [],->(e){ return e.to_s.length} | |
oh2.insert "aaa" | |
oh2.insert "ddd" | |
oh2.insert "aaaa" | |
oh2.showall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment