If key does not exist create default value

If you already have a hash, you can do this: h.fetch(key, “default value”) Or you exploit the fact that a non-existing key will return nil: h[key] || “default value” To create hashes with default values it depends on what you want to do exactly. Independent of key and will not be stored: h = Hash.new(“foo”) … Read more

Group hashes by keys and sum the values

ar = [{“Vegetable”=>10}, {“Vegetable”=>5}, {“Dry Goods”=>3}, {“Dry Goods”=>2}] p ar.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}} #=> {“Vegetable”=>15, “Dry Goods”=>5} Hash.merge with a block runs the block when it finds a duplicate; inject without a initial memo treats the first element of the array as memo, which is fine here.

Create your own MD5 collisions

These following two different 128 byte sequences hash to the same: MD5 Hash: 79054025255fb1a26e4bc422aef54eb4 The differences below are highlighted (bold). Sorry it’s kind of hard to see. d131dd02c5e6eec4693d9a0698aff95c 2fcab58712467eab4004583eb8fb7f89 55ad340609f4b30283e488832571415a 085125e8f7cdc99fd91dbdf280373c5b d8823e3156348f5bae6dacd436c919c6 dd53e2b487da03fd02396306d248cda0 e99f33420f577ee8ce54b67080a80d1e c69821bcb6a8839396f9652b6ff72a70 and d131dd02c5e6eec4693d9a0698aff95c 2fcab50712467eab4004583eb8fb7f89 55ad340609f4b30283e4888325f1415a 085125e8f7cdc99fd91dbd7280373c5b d8823e3156348f5bae6dacd436c919c6 dd53e23487da03fd02396306d248cda0 e99f33420f577ee8ce54b67080280d1e c69821bcb6a8839396f965ab6ff72a70 The visualization of the collision/block1 (Source: Links.Org) The visualization of the … Read more

How does shovel (

You have mixed up the way this works a bit. First off, a Hash doesn’t have a << method, that method in your example exists on the array. The reason your code is not erroring is because you are passing a default value to your hash via the constructor. http://ruby-doc.org/core-1.9.3/Hash.html#method-c-new hash = Hash.new([]) This means … Read more

Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays

Here’s a simple recursive solution: def nested_hash_value(obj,key) if obj.respond_to?(:key?) && obj.key?(key) obj[key] elsif obj.respond_to?(:each) r = nil obj.find{ |*a| r=nested_hash_value(a.last,key) } r end end h = { foo:[1,2,[3,4],{a:{bar:42}}] } p nested_hash_value(h,:bar) #=> 42

Check if string is an MD5 Hash

You can check using the following function: function isValidMd5($md5 =”) { return preg_match(‘/^[a-f0-9]{32}$/’, $md5); } echo isValidMd5(‘5d41402abc4b2a76b9719d911017c592′); The MD5 (Message-digest algorithm) Hash is typically expressed in text format as a 32 digit hexadecimal number. This function checks that: It contains only letters and digits (a-f, 0-9). It’s 32 characters long.

Obtaining the hash of a file using the stream capabilities of crypto module (ie: without hash.update and hash.digest)

From the quoted snippet in the question: [the Hash class] It is a stream that is both readable and writable. The written data is used to compute the hash. Once the writable side of the stream is ended, use the read() method to get the computed hash digest. So what you need to hash some … Read more

Disable hash randomization from within python program

I suspect this isn’t possible, unfortunately. Looking at test_hash.py the HashRandomizationTests class and its descendants were added in the commit that introduced this behavior. They test the hashing behavior by modifying the environment and starting a new process with PYTHONHASHSEED explicitly set. You could try to copy that pattern, perhaps. I also just noticed you … Read more

Convert Ruby Hash into YAML

Here’s how I’d do it: require ‘yaml’ HASH_OF_HASHES = { “hostname1.test.com”=> {“public”=>”51”, “private”=>”10”}, “hostname2.test.com”=> {“public”=>”192”, “private”=>”12”} } ARRAY_OF_HASHES = [ {“hostname1.test.com”=> {“public”=>”51”, “private”=>”10”}}, {“hostname2.test.com”=> {“public”=>”192”, “private”=>”12”}} ] puts HASH_OF_HASHES.to_yaml puts puts ARRAY_OF_HASHES.to_yaml Which outputs: — hostname1.test.com: public: ’51’ private: ’10’ hostname2.test.com: public: ‘192’ private: ’12’ — – hostname1.test.com: public: ’51’ private: ’10’ – hostname2.test.com: public: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)