hello, i'm working in a text based game, and i need to create a new method
to extract the oposite value from a given hash:
# create a mapping of a scene to text
eng = Language.new()
Language.set(eng, 'sc_enter', 'The Gothons of Planet Percal #25 have
invaded your ship and destroyed\nyour entire crew. You are the last
survivieng member and your last\nmission is to get the neutron destruct
bomb from the Weapon Armory,\nput it in the bridge, and blow the ship up
after getting into an \nescape pod.\n')
eng = is the hash.
'sc_enter' = is the key of a text
'blabla' = is the txt that i want to get printed in my_game.rb with :
puts "#{Language.get(eng, 'sc_enter')}"
I'm not sure how to implement the method. The code below, is the original
codo to manipulate
the hash. But i need another method to get the txt working
module Dict def Dict.new(num_buckets=256) # Initializes a Dict with the given number of buckets. aDict = [] (0...num_buckets).each do |i| aDict.push([]) end return aDict end def Dict.hash_key(aDict, key) # Given a key this will create a number and then convert it to # an index for the aDict's buckets. return key.hash % aDict.length end def Dict.get_bucket(aDict, key) # Given a key, find the bucket where it would go. bucket_id = Dict.hash_key(aDict, key) return aDict[bucket_id] end def Dict.get_slot(aDict, key, default=nil) # Returns the index, key, and value of a slot found in a bucket. bucket = Dict.get_bucket(aDict, key) bucket.each_with_index do |kv, i| k, v = kv if key == k return i, k, v end end return -1, key, default end def Dict.get(aDict, key, default=nil) # Gets the value in a bucket for the given key, or the default. i, k, v = Dict.get_slot(aDict, key, default=default) return v end def Dict.set(aDict, key, value) # Sets the key to the value, replacing any existing value. bucket = Dict.get_bucket(aDict, key) i, k, v = Dict.get_slot(aDict, key) if i >= 0 bucket[i] = [key, value] else bucket.push([key, value]) end end def Dict.delete(aDict, key) # Deletes the given key from the Dict. bucket = Dict.get_bucket(aDict, key) (0...bucket.length).each do |i| k, v = bucket[i] if key == k bucket.delete_at(i) break end end end def Dict.list(aDict) # Prints out what's in the Dict. aDict.each do |bucket| if bucket bucket.each {|k, v| puts k, v} end end endend
source from: Zed Shaw - Learn Ruby, the hard way
<http://learnrubythehardway.org/book/ex39.html>