How can I make ruby read a string of characters per three characters?

I want to input a string of characters, and have it be read by every
three characters so I can translate it per three characters.

Sam Thorpe wrote in post #1114567:

I want to input a string of characters, and have it be read by every
three characters so I can translate it per three characters.

Hi Sam, assuming you’re talking about a command-line script that reads
from the keyboard and acts after every third character, you’ll have to
get your ruby script to interact with the command-line environment (the
console). Usually consoles buffer all keyboard input, and only send it
to an inner program when a particular trigger fires (usually the user
hitting Enter).

In Windows, talking to the COMMAND console (command.com, cmd.exe, etc.)
you’d usually have to use a library like conio, although I’m not sure if
there are any ruby bindings for conio.

In a Unix-like environment, talking to a VT-type console, you
have options. The most common would be something like curses, which I
think did have ruby bindings, and probably still does.

I’ve not used either from ruby, but it’s hopefully a starting point.

However if you’re actually just talking about breaking a string up into
three-character chunks, you could do something like this:

print "Type something: "
s = gets.chomp
s.scan(/.{1,3}/).each do |chunk|
  p chunk
end

Here’s an option:

“a testy string of testyness”.split(’’).each_slice(3) do |s|
puts s.join
end

need to loop it as well and do the test on input keeping count if I’m
reading this correct.

while is a good option or ruby has an abstraction of the construct
called
interestingly enough loop{ }

~Stu

Here’s an example which will let you put in sets of 3 characters and
rollaround anything which doesn’t fall into a group of 3.

This particular script just puts the ASCII character, so you’re limited
to passing character groups between “000” and “255” as input (or a fatal
error occurs).


leftover = ‘’

loop do
puts ‘Enter Numbers:’
input = gets.chomp

unless leftover.empty?
input = leftover + input
leftover = ‘’
end

input.scan(/.{1,3}/) do |s|

if s.length == 3
  puts s.to_i.chr
else
  leftover = s
end

end #input scan

end #loop