# How to generate a collection?

**URL:** <https://www.ruby-forum.com/t/how-to-generate-a-collection/181081>\
**Category:** Ruby\
**Created:** [January 5, 2010, 6:07am UTC](https://www.ruby-forum.com/t/how-to-generate-a-collection/181081 "2010-01-05T06:07:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 5, 2010, 6:07am UTC](https://www.ruby-forum.com/t/how-to-generate-a-collection/181081/1 "2010-01-05T06:07:58Z")

</div>

Given any four letter word, for example: “card”  
Replace one to three letters with “~”,  
How can I get the result collection ( ["~ard", “c~rd”, …, “~~r~”,  
“~~~d”])?  
I know ruby, but I am bad at any algorithms, help please.

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 5, 2010, 6:54am UTC](https://www.ruby-forum.com/t/how-to-generate-a-collection/181081/2 "2010-01-05T06:54:29Z")

</div>

Nanyang Z. wrote:

> Given any four letter word, for example: “card”  
> Replace one to three letters with “~”,  
> How can I get the result collection ( ["~ard", “c~rd”, …, “~~r~”,  
> “~~~d”])?

One idea:

irb\> 1.upto(14) {|i| printf("%04b\n", i)}  
0001  
0010  
0011  
0100  
0101  
0110  
0111  
1000  
1001  
1010  
1011  
1100  
1101  
1110

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![xain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/82dd89/32.png) [@xain](https://www.ruby-forum.com/u/xain)\
**Post date:** [January 6, 2010, 5:08am UTC](https://www.ruby-forum.com/t/how-to-generate-a-collection/181081/3 "2010-01-06T05:08:43Z")

</div>

Bill K. wrote:

> Nanyang Z. wrote:
> 
> > Given any four letter word, for example: “card”  
> > Replace one to three letters with “~”,  
> > How can I get the result collection ( ["~ard", “c~rd”, …, “~~r~”,  
> > “~~~d”])?
> 
> One idea:
> 
> irb\> 1.upto(14) {|i| printf("%04b\n", i)}  
> 0001  
> 0010  
> 0011  
> 0100  
> 0101  
> 0110  
> 0111  
> 1000  
> 1001  
> 1010  
> 1011  
> 1100  
> 1101  
> 1110
> 
> Regards,
> 
> Bill

Thanks.  
Given I have the 0 and 1 array,  
Replacing all the 0s with “~”, and replacing all the 1s with the right  
letters, I think it should work.
