Class: RBS::UnitTest::WithAliases::WithEnum

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Enumerable[T]
Defined in:
lib/rbs/unit_test/with_aliases.rb,
sig/unit_test/with_aliases.rbs

Instance Method Summary collapse

Methods included from Enumerable

#each_slice

Constructor Details

#initialize(enum) ⇒ WithEnum

Returns a new instance of WithEnum.

Parameters:

  • enum (_Each[T])


11
# File 'lib/rbs/unit_test/with_aliases.rb', line 11

def initialize(enum) = @enum = enum

Instance Method Details

#andvoid #andvoid

Yield objects in args in addition to original enum elements

e = WithEnum.new([1, 2])
e.and(3) do |i|
  pp i     # => 1, 2, and 3
end

Overloads:

  • #andvoid

    This method returns an undefined value.

  • #andvoid

    This method returns an undefined value.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'sig/unit_test/with_aliases.rbs', line 62

def and(*args, &block)
  return WithEnum.new to_enum(__method__ || raise, *args) unless block

  each(&block)
  args.each do |arg|
    if WithEnum === arg # use `===` as `arg` might not have `.is_a?` on it
      arg.each(&block)
    else
      block.call(_ = arg)
    end
  end
end

#and_nilvoid #and_nilWithEnum[T?]

Yield nil in addition to original enum elements

e = WithEnum.new([1, 2])
e.and_nil do |i|
  pp i     # => 1, 2, and nil
end

Overloads:

  • #and_nilvoid

    This method returns an undefined value.

  • #and_nilWithEnum[T?]

    Returns:

Yields:

Yield Parameters:

  • arg0 (T, nil)

Yield Returns:

  • (void)


38
39
40
# File 'sig/unit_test/with_aliases.rbs', line 38

def and_nil(&block)
  self.and(nil, &_ = block)
end

#but(cases) ⇒ void #but(cases) ⇒ WithEnum[T]

Skip yielding objects in cases

e = WithEnum.new([1, 2, 3])
e.but(2) do |i|
  pp i     # => 1 and 3
end

Overloads:

  • #but(cases) ⇒ void

    This method returns an undefined value.

    Parameters:

    • cases (T)
  • #but(cases) ⇒ WithEnum[T]

    Parameters:

    • cases (T)

    Returns:

Yields:

Yield Parameters:

  • arg0 (T)

Yield Returns:

  • (void)


50
51
52
53
54
55
56
# File 'sig/unit_test/with_aliases.rbs', line 50

def but(*cases, &block)
  return WithEnum.new to_enum(__method__ || raise, *cases) unless block

  each do |arg|
    yield arg unless cases.any? { (_ = _1) === arg }
  end
end

#each {|arg0| ... } ⇒ void

This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (T)

Yield Returns:

  • (void)


13
# File 'lib/rbs/unit_test/with_aliases.rb', line 13

def each(&block) = @enum.each(&block)