Use case to deal with special key : each_byte « String « Ruby






Use case to deal with special key



def snoop_on_keylog(input)
  input.each_byte  do |b|
    case b
      when ?\C-c; puts 'Control-C: stopped a process?'
      when ?\C-z; puts 'Control-Z: suspended a process?'
      when ?\n;   puts 'Newline.'
      when ?\M-x; puts 'Meta-x: using Emacs?'
    end
  end
end

snoop_on_keylog("ls -ltR\003emacsHello\012\370rot13-\012\032")

 








Related examples in the same category

1.each_byte takes a string apart byte by byte, returning the decimal value for the character at each index location.
2.Convert each decimal to its character equivalent with Integer's chr method
3.Append the output to an array
4.each_byte block logics
5.Loop through each byte from a string