Google


   


You are here: CodeIdol.com > Other > Ruby Cookbook > Hashes

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

Ruby Cookbook



Recipe 5.1. Using Symbols as Hash Keys Credit: Ben Giddings Problem When using a hash, you want the slight optimization you can get by using symbols as keys instead of strings. Solution Whenever you would otherwise use a quoted stri...



Recipe 5.2. Creating a Hash with a Default Value Credit: Ben Giddings Problem You're using a hash, and you don't want to get nil as a value when you look up a key that isn't present in the hash. You want to get some more convenient value ...



Recipe 5.3. Adding Elements to a Hash Problem You have some items, loose or in some other data structure, which you want to put into an existing hash. Solution To add a single key-value pair, assign the value to the element lookup ex...



Recipe 5.4. Removing Elements from a Hash Problem Certain elements of your hash have got to go! Solution Most of the time you want to remove a specific element of a hash. To do that, pass the key into Hash#delete. h = {} h[1] =...



Recipe 5.5. Using an Array or Other Modifiable Object as a Hash Key Problem You want to use a modifiable built-in object (an array or a hash, but not a string) as a key in a hash, even while you modify the object in place. A naive solution tend...



Recipe 5.6. Keeping Multiple Values for the Same Hash Key Problem You want to build a hash that might have duplicate values for some keys. Solution The simplest way is to create a hash that initializes missing values to empty arrays. Y...



Recipe 5.7. Iterating Over a Hash Problem You want to iterate over a hash's key-value pairs as though it were an array. Solution Most likely, the iterator you want is Hash#each_pair or Hash#each. These methods yield every key-value ...



Recipe 5.8. Iterating Over a Hash in Insertion Order Problem Iterations over a hash happen in a seemingly random order. Sorting the keys or values only works if the keys or values are all mutually comparable. You'd like to iterate over a h...



Recipe 5.9. Printing a Hash Credit: Ben Giddings Problem You want to print out the contents of a Hash, but Kernel#puts doesn't give very useful results. h = {} h[:name] = "Robert" h[:nickname] = "Bob" h[:age] = 43 h[:email_...

read more: Printing a Hash


Recipe 5.10. Inverting a Hash Problem Given a hash, you want to switch the keys and values. That is, you want to create a new hash whose keys are the values of the old hash, and whose values are the keys of the old hash. If the old hash ma...

read more: Inverting a Hash


Recipe 5.11. Choosing Randomly from a Weighted List Problem You want to pick a random element from a collection, where each element in the collection has a different probability of being chosen. Solution Store the elements in a hash,...



Recipe 5.12. Building a Histogram Problem You have an array that contains a lot of references to relatively few objects. You want to create a histogram, or frequency map: something you can use to see how often a given object shows up in th...



Recipe 5.13. Remapping the Keys and Values of a Hash Problem You have two hashes with common keys but differing values. You want to create a new hash that maps the values of one hash to the values of another. Solution class Has...



Recipe 5.14. Extracting Portions of Hashes Problem You have a hash that contains a lot of values, but only a few of them are interesting. You want to select the interesting values and ignore the rest. Solution You can use the ...


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > Other > Ruby Cookbook > Hashes


ADBRITE ads links
   
Related tags







Popular Categories
Unix books and guides

AJAX popular information
C# language guides
Windows books and cookbooks

.......








Business Key Top Sites

be number one
rate your site


Поздравления дочки. Поздравления рождение. поздравления

    С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.

    Русский Polski Francais Deutsch
    support sitemap terms

© CodeIdol Labs, 2007 - 2009