# Language challenge

**URL:** <https://www.ruby-forum.com/t/language-challenge/59822>\
**Category:** Ruby\
**Created:** [May 7, 2006, 2:28am UTC](https://www.ruby-forum.com/t/language-challenge/59822 "2006-05-07T02:28:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TrueGrit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@TrueGrit](https://www.ruby-forum.com/u/TrueGrit)\
**Post date:** [May 7, 2006, 2:28am UTC](https://www.ruby-forum.com/t/language-challenge/59822/1 "2006-05-07T02:28:59Z")

</div>

So far examples from 20 Languages are listed at:

[http://dcpp.net/wiki/index.php/LockToKey](http://dcpp.net/wiki/index.php/LockToKey)

The challenge is to add a Ruby version before a Squirrel version is  
added. The same message has been posted to the Squirrel forum. See if  
you can make the Ruby version more elegant and compact 🙂

---

<div class="post-metadata">

**Author:** ![TrueGrit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@TrueGrit](https://www.ruby-forum.com/u/TrueGrit)\
**Post date:** [May 7, 2006, 4:05am UTC](https://www.ruby-forum.com/t/language-challenge/59822/2 "2006-05-07T04:05:46Z")

</div>

I’ve added a Ruby version at the bottom. Feel free to make it more  
elegant or compact.

---

<div class="post-metadata">

**Author:** ![TrueGrit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@TrueGrit](https://www.ruby-forum.com/u/TrueGrit)\
**Post date:** [May 7, 2006, 11:45am UTC](https://www.ruby-forum.com/t/language-challenge/59822/3 "2006-05-07T11:45:04Z")

</div>

2006/5/7, C Erler [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’ve added a Ruby version at the bottom. Feel free to make it more  
> elegant or compact.

Suggestions for some changes attached - you decide whether you think  
they are improvements.

- remove explicit throw as you will get an exception from to\_str  
anyway if it’s not there
- use concat over += for efficiency
- use case for discrimination

Cheers

robert

---

<div class="post-metadata">

**Author:** ![TrueGrit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@TrueGrit](https://www.ruby-forum.com/u/TrueGrit)\
**Post date:** [May 8, 2006, 12:06am UTC](https://www.ruby-forum.com/t/language-challenge/59822/4 "2006-05-08T00:06:09Z")

</div>

Robert K. wrote:

> - remove explicit throw as you will get an exception from to\_str  
> anyway if it’s not there

I wanted the method to have a behavior consistent with others that don’t  
get the right type to take advantage of programmer debugging habits.  
I’ll have no problems if anyone changes it, though.

> - use concat over += for efficiency
> - use case for discrimination

I like both of these and have added them. I just learned about case  
with comma separation yesterday, so this is an excellent opportunity to  
use it.

Thanks for the suggestions.

---

<div class="post-metadata">

**Author:** ![TrueGrit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@TrueGrit](https://www.ruby-forum.com/u/TrueGrit)\
**Post date:** [May 8, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/language-challenge/59822/5 "2006-05-08T16:29:16Z")

</div>

Robert K. wrote:

> You’re welcome. I have another one: you have two calls to map! - you  
> could do that in one go.

Another good suggestion :). I also made the combination of neighboring  
characters much more elegant by copying how the Python code did it to  
the Ruby style.

---

<div class="post-metadata">

**Author:** ![TrueGrit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@TrueGrit](https://www.ruby-forum.com/u/TrueGrit)\
**Post date:** [May 8, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/language-challenge/59822/6 "2006-05-08T14:30:13Z")

</div>

2006/5/8, C Erler [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I like both of these and have added them. I just learned about case  
> with comma separation yesterday, so this is an excellent opportunity to  
> use it.
> 
> Thanks for the suggestions.

You’re welcome. I have another one: you have two calls to map! - you  
could do that in one go.

result.map! do |value|  
# Rotate each byte by four bits.  
value = ((value \<\< 4) | (value \>\> 4)) & 0b11111111

```
 # Put the output in the correct format.
 case value
   when 0, 5, 36, 96, 124, 126
     '/%%DCN%03d%%/' % value
   else
     value.chr
 end

```

end

Kind regards

robert
