Language challenge

So far examples from 20 Languages are listed at:

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 :slight_smile:

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

2006/5/7, C Erler [email protected]:

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

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.

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.

2006/5/8, C Erler [email protected]:

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