# Substitution with Hash

**URL:** <https://rubytalk.org/t/substitution-with-hash/40539>\
**Category:** ruby-talk\
**Created:** [11 September 2007 10:41 UTC](https://rubytalk.org/t/substitution-with-hash/40539 "2007-09-11T10:41:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [11 September 2007 10:41 UTC](https://rubytalk.org/t/substitution-with-hash/40539/1 "2007-09-11T10:41:46Z")

</div>

Ok i'll try to explain what i mean as well as i can

Lets say i have a hash like this

hash { 'a' =\> '1' } #just as example, its actually far bigger

and if a user inputs abcdabcd i was it to sub all of the a's with 1's..

As i said, the hash is far larger which is why i can't just do it with  
gsub..

Any ideas?

Thanks in advance..

Lee

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [11 September 2007 10:48 UTC](https://rubytalk.org/t/substitution-with-hash/40539/2 "2007-09-11T10:48:13Z")

</div>

Lee Jarvis wrote the following on 11.09.2007 12:41 :

> Ok i'll try to explain what i mean as well as i can
> 
> Lets say i have a hash like this
> 
> hash { 'a' =\> '1' } #just as example, its actually far bigger
> 
> and if a user inputs abcdabcd i was it to sub all of the a's with 1's..
> 
> As i said, the hash is far larger which is why i can't just do it with  
> gsub..
> 
> Any ideas?
> 
> Thanks in advance..
> 
> Lee  
> &nbsp;&nbsp;  
> yourstring.split(//).map{|c| hash[c] || c}.join

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [11 September 2007 10:57 UTC](https://rubytalk.org/t/substitution-with-hash/40539/3 "2007-09-11T10:57:34Z")

</div>

Lionel Bouton wrote the following on 11.09.2007 12:48 :

> Lee Jarvis wrote the following on 11.09.2007 12:41 :  
> &nbsp;&nbsp;
> 
> > Ok i'll try to explain what i mean as well as i can
> > 
> > Lets say i have a hash like this
> > 
> > hash { 'a' =\> '1' } #just as example, its actually far bigger
> > 
> > and if a user inputs abcdabcd i was it to sub all of the a's with 1's..
> > 
> > As i said, the hash is far larger which is why i can't just do it with  
> > gsub..
> > 
> > Any ideas?
> > 
> > Thanks in advance..
> > 
> > Lee  
> > &nbsp;&nbsp;  
> > yourstring.split(//).map{|c| hash[c] || c}.join

Note that if your hash is only used to convert single characters to  
single characters, you can use String#tr (or tr!). If you are after  
performance, as you must prepare the strings used by String#tr from your  
hash, you'll have to bench it to see if it's worth it in your use case  
even if String#tr is faster in itself.  
If you are processing UTF-8 content, String#tr is probably not safe  
(there are libraries out there for fixing this though IIRC), but my  
first answer probably is (assuming $KCODE='u'; require 'jcode'...) as  
the regexp processing is utf-8 aware, so the String#split should be safe.

Lionel

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@Lee\_Jarvis](https://rubytalk.org/u/Lee_Jarvis)\
**Post date:** [11 September 2007 11:51 UTC](https://rubytalk.org/t/substitution-with-hash/40539/4 "2007-09-11T11:51:27Z")

</div>

Thanks that worked well, And no its not single chars, Which is the only  
reason i'm doing it this way..

I have to split on whitespace (/ /) because spliting on characters would  
obviously split the text i want to transform, which means it wont match  
if the characters are trailing another word, HTML special chars for  
example

h = {"&#126;" =\> "~"}

"hmm &#126;'.split(/ /).map{|c| h[c] || c}.join(' ')

Outputs hmm ~, but obviously doing things like question marks wont work,  
Maybe i'll have to use loops and string#tr

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [11 September 2007 13:35 UTC](https://rubytalk.org/t/substitution-with-hash/40539/5 "2007-09-11T13:35:50Z")

</div>

I'd rather not do the split step, IMHO direct replacement will be faster:

h = {"#126" =\> "~"}  
s.gsub(/&([^;]+);/) {|c| h[c] || "&#{c};"}

Btw, I believe there are standard classes that do this type of  
replacement (entities in HTML documents) - maybe it's in CGI.

Kind regards

robert

> **···**
>
> 2007/9/11, Lee Jarvis \<jarvo88@gmail.com\>:
> 
> > Thanks that worked well, And no its not single chars, Which is the only  
> > reason i'm doing it this way..
> > 
> > I have to split on whitespace (/ /) because spliting on characters would  
> > obviously split the text i want to transform, which means it wont match  
> > if the characters are trailing another word, HTML special chars for  
> > example
> > 
> > h = {"&#126;" =\> "~"}
> > 
> > "hmm &#126;'.split(/ /).map{|c| h[c] || c}.join(' ')
> > 
> > Outputs hmm ~, but obviously doing things like question marks wont work,  
> > Maybe i'll have to use loops and string#tr

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://rubytalk.org/u/Daniel_DeLorme)\
**Post date:** [11 September 2007 23:53 UTC](https://rubytalk.org/t/substitution-with-hash/40539/6 "2007-09-11T23:53:08Z")

</div>

Lee Jarvis wrote:

> Thanks that worked well, And no its not single chars, Which is the only reason i'm doing it this way..
> 
> I have to split on whitespace (/ /) because spliting on characters would obviously split the text i want to transform, which means it wont match if the characters are trailing another word, HTML special chars for example
> 
> h = {"&#126;" =\> "~"}

If you're just trying to translate numeric html entities it's easy:  
&nbsp;&nbsp;&nbsp;str.gsub(/&#(\d+);/){ [$1.to\_i].pack('U') }  
If you also want named entities I suggest the htmlentities gems.  
If it's for a more general case, how about:  
&nbsp;&nbsp;&nbsp;rx = Regexp.new(hash.keys.map{|k|Regexp.escape(k)}.join("|"))  
&nbsp;&nbsp;&nbsp;str.gsub(rx){ hash[$&] }

Daniel

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [11 September 2007 14:18 UTC](https://rubytalk.org/t/substitution-with-hash/40539/7 "2007-09-11T14:18:43Z")

</div>

Robert Klemme wrote:

> > h = {"&#126;" =\> "~"}
> > 
> > "hmm &#126;'.split(/ /).map{|c| h[c] || c}.join(' ')
> > 
> > Outputs hmm ~, but obviously doing things like question marks wont work,  
> > Maybe i'll have to use loops and string#tr  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > I'd rather not do the split step, IMHO direct replacement will be faster:  
> > &nbsp;&nbsp;  
> > If it's all for html entities yes. I'm not sure of what the actual use  
> > case is though.

> h = {"#126" =\> "~"}  
> s.gsub(/&([^;]+);/) {|c| h[c] || "&#{c};"}
> 
> Btw, I believe there are standard classes that do this type of  
> replacement (entities in HTML documents) - maybe it's in CGI.  
> &nbsp;&nbsp;  
> The htmlentities gem (more robust than CGI with UTF-8...) is quite good.
