Forum: Ruby Language challenge

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
TrueGrit (Guest)
on 2006-05-07 04:28
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 :)
C Erler (Guest)
on 2006-05-07 06:05
I've added a Ruby version at the bottom.  Feel free to make it more
elegant or compact.
Robert K. (Guest)
on 2006-05-07 13:45
(Received via mailing list)
2006/5/7, C Erler <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
C Erler (Guest)
on 2006-05-08 02:06
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. (Guest)
on 2006-05-08 16:30
(Received via mailing list)
2006/5/8, C Erler <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
C Erler (Guest)
on 2006-05-08 18:29
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.
This topic is locked and can not be replied to.