From: "Jesús Gabriel y Galán" Date: 2011-11-30T03:10:07+09:00 Subject: Re: Ruby and ASCII matrix On Tue, Nov 29, 2011 at 6:59 PM, Adam Mulan wrote: > Hello, > I have prepared the following code (it almost works for even size > strings). I wanna create 2-chars vectors from ASCII codes and push them > on the stack, then proceed by algebra functions (Hill cipher). I don't > want use encryption library, because it's academic exercise (I can use > any language). > > #!/usr/bin/env ruby > > require 'matrix' > > print "Message: " > msg = gets.chomp > > # stack with matrices > stack = [] > # make matrix with column > i = 0 > column = [0, 0] > msg.each_byte do |char| >  # create two-element vector >  column[0] = char if i == 0 >  if i == 1 >    column[1] = char >    # put vector on stack >    puts "Input data: #{column[0]}, #{column[1]}" >    tmp = Matrix[column[0], column[1]] I get an error here, cause Matrix#[] is expecting an array. >    puts "Output data: #{tmp[0,0]} #{tmp[0,1]}" >    stack.push(tmp) >    i = 0 >    next >  end >  i += 1 > end > > The output data is invalid, i don't store input data in tmp Matrix class > variable. Why? Your algorithm can be made simpler, using each_slice: #!/usr/bin/env ruby require 'matrix' print "Message: " msg = gets.chomp # stack with matrices stack = [] msg.each_byte.each_slice(2) do |two_char_array| stack.push Matrix[two_char_array] end p stack Typing "test" as the message I get: [Matrix[[116, 101]], Matrix[[115, 116]]] Hope this helps. Jesus.