Skip to content

Commit

Permalink
fix: find and detect to return unit instead of tag-unit tupple
Browse files Browse the repository at this point in the history
  • Loading branch information
dysonreturns committed Mar 19, 2024
1 parent ef86bfb commit b1a2e8b
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/sc2ai/unit_group.rb
Expand Up @@ -83,14 +83,7 @@ def initialize(units = nil)
def_delegator :@units, :to_hash # Returns {UnitGroup#units}
def_delegator :@units, :to_proc # Returns a proc that maps a given key to its value.Returns a proc that maps a given key to its value.

# Methods which return unit
# @!macro [attach] def_delegators
# @!method $2
# Forwards to hash of #units.
# @see Hash#$2
# @return [Api::Unit]
def_delegator :@units, :find # Returns an element selected by the block.
def_delegator :@units, :detect # Returns an element selected by the block.


# Gets the Unit at an index
# @return [Api::Unit]
Expand All @@ -106,23 +99,22 @@ def at(index)

# Calls the given block with each Api::Unit value
# @example
# unit_group.each {|unit| puts unit.tag } #=> 1234 ...
#
# unit_group.each do |unit|
# puts unit.tag #=> 1234 ...
# end
# @yieldparam unit [Api::Unit]
def each(&)
@units.each_value(&)
end

# Calls the given block with each key-value pair
# @return [self] a new unit group with items merged
# @example
# unit_group.each {|tag, unit| puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
#
# unit_group.each do |tag, unit|
# unit_group.each_with_tag do |tag, unit|
# puts "#{tag}: #{unit}"} #=> "12345: #<Api::Unit ...>"
# end
# @yieldparam tag [Integer]
# @yieldparam unit [Api::Unit]
def each_with_tag(&)
@units.each(&)
self
Expand Down Expand Up @@ -250,6 +242,13 @@ def sample(...)
end
alias_method :random, :sample

# Returns the first Unit for which the block returns a truthy value
# @return [Api::Unit]
def detect(...)
@units.values.find(...)
end
alias_method :find, :detect

# def select_or(*procs)
# result = UnitGroup.new
# procs.each do |proc|
Expand Down

0 comments on commit b1a2e8b

Please sign in to comment.