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:

ยทยทยท

--
Posted via http://www.ruby-forum.com/.

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

ยทยทยท

--
Posted via http://www.ruby-forum.com/.

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

key.rb (692 Bytes)

ยทยทยท

2006/5/7, C Erler <not.quite.ready@not.quite.ready>:

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

--
Have a look: Robert K. | Flickr

Robert Klemme 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.

ยทยทยท

--
Posted via http://www.ruby-forum.com/\.

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

ยทยทยท

2006/5/8, C Erler <not.quite.ready@not.quite.ready>:

Robert Klemme 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.

--
Have a look: Robert K. | Flickr

Robert Klemme 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.

ยทยทยท

--
Posted via http://www.ruby-forum.com/\.