"Meino Christian Cramer" <Meino.Cramer / gmx.de> schrieb im Newsbeitrag
news:20040817.184616.92576207.Meino.Cramer / gmx.de...
> From: "Ara.T.Howard" <ahoward / noaa.gov>
> Subject: Re: Q: Shifting a hash anf and array
> Date: Tue, 17 Aug 2004 08:33:24 -0600 (MDT)
>
> Hi Ara and the other problem solvers,
>
>  WHOW!  (<- that's describing the best... ;)
>
>  There was ever a difficulty for me and will ever be:
>  I dont know, what I like most: Ruby or its mailing list people ! :O)

:-))

>  One of the basic concepts of the Enigma machine are the wheels. Those
>  wheels had 26 input terminals on the one side and 26 output terminals
>  on the other side. Each terminal was representing one letter. The
>  connnection between the front and back terminals were "crossed wired"
>  -- or irregular (right word?). What was put as an "A" on the front
>  came out as (fro example) "Q" on the back.
>
>  Three or four wheels were packed on one axis. Which each letter input
>  the wheels were rotated one position (I haven't figured out, how
>  exactly this works until now...). That means: "AAA" came out as (for
>  example) "QZF" and not as "QQQ".
>
>  I thought of implementing the wheels as such
>  not-integer-but-ordered-indexed "arrays". And my question about all
>  this rotating and shiofting stuff was to figure out how to "rotate"
>  my wheel-arrays.

IMHO rotating is much too inefficient.  I'd rather use a changing offset
like this:

module Enigma
  class Wheel
    def initialize(mapping)
      @code = mapping
      @offset = 0
    end

    def rotate
      @offset = (@offset + 1) % @code.size
    end

    def encode(char)
      @code[(normalize(char) + @offset) % @code.size]
    end

private
    def normalize(char)
      case char
        when String
          char.upcase[0] - ?A
        when ?a..?z
          char - ?a
        when ?A..?Z
          char - ?A
        else
          raise ArgumentError, "Dunno what to do with #{char.inspect}"
      end
    end
  end
end


wheel = Enigma::Wheel.new [?x, ?z, ?e]
p( wheel.encode( ?A ).chr )
wheel.rotate
p( wheel.encode( ?A ).chr )
p( wheel.encode( "A" ).chr )

Kind regards

    robert