class MorseCodeInteractive

Public Class Methods

new(dash: 'D', dot: 'C', separator: 'B', terminator: "\r", debug: false) click to toggle source
# File lib/morsecode.rb, line 38
def initialize(dash: 'D', dot: 'C', separator: 'B', terminator: "\r", 
               debug: false)

  @keys = {
    dash: dash, 
    dot: dot, 
    separator: separator, 
    terminator: terminator
  }

  @mc = ''
  @debug = debug

end

Public Instance Methods

input(c) click to toggle source
# File lib/morsecode.rb, line 53
def input(c)

  h = {
    dash: :dash, 
    dot: :dot, 
    separator: :separator, 
    terminator: :submit
  }

  if @debug then
    puts 'c: ' + c.inspect
    puts '@keys.invert: ' + @keys.invert.inspect
  end

  key = @keys.invert[c]
  puts 'key: ' + key.inspect if @debug
  found = h[key]
  puts 'found: ' + found.inspect if @debug
  method(found).call if found

end
on_dash() click to toggle source
# File lib/morsecode.rb, line 75
def on_dash() end
on_dot() click to toggle source
# File lib/morsecode.rb, line 76
def on_dot() end
on_separator(c) click to toggle source
# File lib/morsecode.rb, line 78
def on_separator(c)
  puts 'c: ' + c.inspect
end
on_submit(s) click to toggle source
# File lib/morsecode.rb, line 82
def on_submit(s)
  puts 's: ' + s.inspect    
end

Private Instance Methods

dash() click to toggle source
# File lib/morsecode.rb, line 88
def dash()
  @mc += '1'
  on_dash()
end
dot() click to toggle source
# File lib/morsecode.rb, line 93
def dot()
  @mc += '2'
  on_dot()
end
separator() click to toggle source
# File lib/morsecode.rb, line 98
def separator()
  
  @mc += @mc[-1] =='4' ? '5' : '4'
  s = MorseCode.new(@mc).to_s[-1]
  on_separator(s)

end
submit() click to toggle source
# File lib/morsecode.rb, line 106
def submit()

  s = MorseCode.new(@mc).to_s 
  on_submit(s)
  @mc = ''  

end