Class: Cod::Select
- Inherits:
-
Object
- Object
- Cod::Select
- Defined in:
- lib/cod/select.rb
Overview
Performs an IO.select on a list of file descriptors and Cod channels. Construct this like so:
Select.new(
0.1, # timeout
foo: single_fd, # a single named FD
bar: [one, two, three], # a group of FDs.
)
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#do ⇒ Hash, ...
Performs the IO.select and returns a thinned out version of that initial groups, containing only FDs and channels that are ready for reading.
-
#initialize(timeout, groups) ⇒ Select
constructor
A new instance of Select.
Constructor Details
#initialize(timeout, groups) ⇒ Select
Returns a new instance of Select.
27 28 29 30 |
# File 'lib/cod/select.rb', line 27 def initialize(timeout, groups) @timeout = timeout @groups = SelectGroup.new(groups) end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
25 26 27 |
# File 'lib/cod/select.rb', line 25 def groups @groups end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
24 25 26 |
# File 'lib/cod/select.rb', line 24 def timeout @timeout end |
Instance Method Details
#do ⇒ Hash, ...
Performs the IO.select and returns a thinned out version of that initial groups, containing only FDs and channels that are ready for reading.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cod/select.rb', line 37 def do fds = groups.values { |e| to_read_fds(e) }.compact # Perform select r,w,e = IO.select(fds, nil, nil, timeout) # Nothing is ready if r is nil return groups.empty unless r # Prepare a return value: The original hash, where the fds are ready. groups. keep_if { |e| to_read_fds(e). any? { |fd| r.include?(fd) } }. unpack end |