# Smoking hash-ish: Letter mapping

**URL:** <https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534>\
**Category:** Ruby\
**Created:** [August 5, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534 "2006-08-05T20:51:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [August 5, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534/1 "2006-08-05T20:51:28Z")

</div>

Hi,

for a _very_ simplistic encryption program I want to build a  
hash, which should be dynamically build (not hardcoded).

The input of the method which should do the job will be a range of  
any meaningful input, say: parameter 1: A and parameter 2: Z

Then the method should create a Hash with that mapping  
“A” -\> “Z”  
“B” -\> “G”  
“C” -\> “T”  
…

or in other words:  
The key should follow the given range, and the values should  
represent all values of that range but in random order.

For arrays I learned a nice trick from this list:

```
 encmap=(A..Z).to_a.sort{rand(2)}

```

but…how can I accomplish similiar for a hash ???

And: I sthere a simple way to store a hash in binary format and later  
load that hash again into memory ?

Nice weekend!  
mcc

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [August 5, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534/2 "2006-08-05T20:57:28Z")

</div>

Maybe: encmap=Hash[\*(‘A’…‘Z’).to\_a.sort{rand(2)}]

and Marshal.dump and Marshal.load for storing.

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [August 5, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534/3 "2006-08-05T21:06:29Z")

</div>

From: Joey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Smoking hash-ish: Letter mapping…  
Date: Sun, 6 Aug 2006 03:55:14 +0900

Looks good! May be I am too newbie here, but…  
When I do this in irb and look at encmap, both - key and value -  
seem to be randomnized…

(and what means the “\*” right at the start of the expression?)

But this is not really an issue as long there is no identical mapping  
like “A”-\>“A”.

Ah, by the way: With an even number of items/mapping in such an  
Hash…there must be zero or an even number of identical mappings like  
described above. Is this logical correct or nonsense…?

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [August 5, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534/4 "2006-08-05T21:24:29Z")

</div>

On Aug 5, 2006, at 3:07 PM, Meino Christian C. wrote:

> Oh…there is another issue:
> 
> With encmap=Hash[\*(‘A’…‘Z’).to\_a.sort{rand(2)}] I only will get 13  
> instead of 26 mappings! How can I fix /that/ ?
> 
> Keep encoding! 🙂  
> mcc

Actually its generally considered better to do sort\_by{rand} that sort  
{rand(2)}

If you only want to randomize key or value you can do

first = (“A”…“Z”).to\_a  
second = (“A”…“Z”).sort\_by{rand}

encmap = Hash[\*first.zip(second).flatten]

---

<div class="post-metadata">

**Author:** ![Meino\_Christian\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@Meino\_Christian\_C](https://www.ruby-forum.com/u/Meino_Christian_C)\
**Post date:** [August 5, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/smoking-hash-ish-letter-mapping/101534/5 "2006-08-05T21:09:29Z")

</div>

Oh…there is another issue:

With encmap=Hash[\*(‘A’…‘Z’).to\_a.sort{rand(2)}] I only will get 13  
instead of 26 mappings! How can I fix /that/ ?

Keep encoding! 🙂  
mcc
