Class: M0rse::Morse

Inherits:
Object
  • Object
show all
Defined in:
lib/m0rse.rb

Class Method Summary collapse

Class Method Details

.binary_to_morse(binary_) ⇒ Object



45
46
47
48
49
# File 'lib/m0rse.rb', line 45

def self.binary_to_morse(binary_)
  text_ = Array(binary_).pack('B*')
  result_ = self.text_to_morse(text_)
  return result_
end


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/m0rse.rb', line 72

def self.blink(text_)
  morse_ = self.text_to_morse(text_)
    for char in morse_.split(//)
      if char == '.'
        system('color 70')
        sleep(0.01)
        system('color 07')
      elsif char == '-'
        system('color 70')
        sleep(0.2)
        system('color 07')
      elsif char == ' '
        sleep(0.3)
      elsif char == '\\'
        sleep(0.7)
      end
    end; "completed"
end

.file_to_morse(filename_) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/m0rse.rb', line 63

def self.file_to_morse(filename_)
  file_ = File.open(filename_, 'r+')
  text_ = file_.readlines.map(&:chomp).join()
  result_ = self.text_to_morse(text_)
  file_.close()
  new_file = File.open("#{filename_}-Morsed", "w")
  File.write(new_file, result_)
end

.hex_to_morse(hex_) ⇒ Object



57
58
59
60
61
# File 'lib/m0rse.rb', line 57

def self.hex_to_morse(hex_)
  text_ = Array(hex_).pack('H*')
  result_ = self.text_to_morse(text_)
  return result_
end

.morse_to_binary(morse_) ⇒ Object



39
40
41
42
43
# File 'lib/m0rse.rb', line 39

def self.morse_to_binary(morse_)
  text_ = self.morse_to_text(morse_)
  result_ = text_.unpack('B*')[0]
  return result_
end

.morse_to_hex(morse_) ⇒ Object



51
52
53
54
55
# File 'lib/m0rse.rb', line 51

def self.morse_to_hex(morse_)
  text_ = self.morse_to_text(morse_)
  result_ = text_.unpack('H*')[0]
  return result_
end

.morse_to_text(morse_) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/m0rse.rb', line 29

def self.morse_to_text(morse_)
  text_ = []
  for char in morse_.split(/ /)
    text_.append @@morsetoascii[char]
  end
  result_ = text_.join('') 
  return result_
end

.play(text_, speed = 1) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/m0rse.rb', line 92

def self.play(text_, speed=1)
  morse_ = self.text_to_morse(text_)
  begin
    for char in morse_.split(//)
      if char == '.'
        Beeper.dit
      elsif char == '-'
        Beeper.dah
      elsif char == ' '
        sleep(0.375*speed)
      elsif char == '\\'
        sleep(0.875*speed)
      end
    end; "completed"
  rescue
    puts "something went wrong..."
  end
end

.teacherObject



111
112
113
# File 'lib/m0rse.rb', line 111

def self.teacher
  @@teacher
end

.text_to_morse(text_) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/m0rse.rb', line 18

def self.text_to_morse(text_)
  morse_ = []
  text_.downcase!
  for char in text_.split(//)
    morse_.append @@asciitomorse[char]
  end
  result_ = morse_.join(' ')
  return result_
end