Module: MIDICommunications::Device::ClassMethods
Overview
Instance Method Summary collapse
-
#at(index) ⇒ Input, Output
(also: #[])
Returns the device at the given index without opening it.
-
#each {|device| ... } ⇒ Enumerator
Iterates over all devices of this type.
-
#find_by_name(name) ⇒ Input, ...
Finds a device by its name.
-
#first {|device| ... } ⇒ Input, Output
Selects and opens the first available device.
-
#gets {|device| ... } ⇒ Input, Output
Interactive console prompt for device selection.
-
#last {|device| ... } ⇒ Input, Output
Selects and opens the last available device.
-
#list ⇒ Array<String>
Prints the ID and name of each device to the console.
-
#use(index) {|device| ... } ⇒ Input, Output
(also: #open)
Selects and opens the device at the given index.
Instance Method Details
#at(index) ⇒ Input, Output Also known as: []
Returns the device at the given index without opening it.
140 141 142 |
# File 'lib/midi-communications/device.rb', line 140 def at(index) all[index] end |
#each {|device| ... } ⇒ Enumerator
Iterates over all devices of this type.
26 27 28 |
# File 'lib/midi-communications/device.rb', line 26 def each(&block) all.each(&block) end |
#find_by_name(name) ⇒ Input, ...
Finds a device by its name.
53 54 55 |
# File 'lib/midi-communications/device.rb', line 53 def find_by_name(name) all.find { |device| name.to_s == device.name } end |
#first {|device| ... } ⇒ Input, Output
Selects and opens the first available device.
97 98 99 |
# File 'lib/midi-communications/device.rb', line 97 def first(&block) use_device(all.first, &block) end |
#gets {|device| ... } ⇒ Input, Output
Interactive console prompt for device selection.
Displays available devices and waits for user input. When a valid selection is received, the device is opened and returned.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/midi-communications/device.rb', line 71 def gets(&block) device = nil direction = get_direction puts '' puts "Select a MIDI #{direction}..." while device.nil? list print '> ' selection = $stdin.gets.chomp if selection != '' selection = Integer(selection) rescue nil device = all.find { |d| d.id == selection } unless selection.nil? end end device.open(&block) device end |
#last {|device| ... } ⇒ Input, Output
Selects and opens the last available device.
109 110 111 |
# File 'lib/midi-communications/device.rb', line 109 def last(&block) use_device(all.last, &block) end |
#list ⇒ Array<String>
Prints the ID and name of each device to the console.
38 39 40 41 42 43 44 |
# File 'lib/midi-communications/device.rb', line 38 def list all.map do |device| name = "#{device.id}) #{device.display_name}" puts(name) name end end |
#use(index) {|device| ... } ⇒ Input, Output Also known as: open
Selects and opens the device at the given index.
122 123 124 125 126 127 128 129 |
# File 'lib/midi-communications/device.rb', line 122 def use(index, &block) index = case index when :first then 0 when :last then all.count - 1 else index end use_device(at(index), &block) end |