Class: Google::Protobuf::RepeatedField

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/google/protobuf/repeated_field.rb

Defined Under Namespace

Classes: ProxyingEnumerator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_array_wrapper_method(method_name) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/google/protobuf/repeated_field.rb', line 114

def define_array_wrapper_method(method_name)
  define_method(method_name) do |*args, &block|
    arr = self.to_a
    result = arr.send(method_name, *args)
    self.replace(arr)
    return result if result
    return block ? block.call : result
  end
end

.define_array_wrapper_with_result_method(method_name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/google/protobuf/repeated_field.rb', line 126

def define_array_wrapper_with_result_method(method_name)
  define_method(method_name) do |*args, &block|
    # result can be an Enumerator, Array, or nil
    # Enumerator can sometimes be returned if a block is an optional argument and it is not passed in
    # nil usually specifies that no change was made
    result = self.to_a.send(method_name, *args, &block)
    if result
      new_arr = result.to_a
      self.replace(new_arr)
      if result.is_a?(Enumerator)
        # generate a fresh enum; rewinding the exiting one, in Ruby 2.2, will
        # reset the enum with the same length, but all the #next calls will
        # return nil
        result = new_arr.to_enum
        # generate a wrapper enum so any changes which occur by a chained
        # enum can be captured
        ie = ProxyingEnumerator.new(self, result)
        result = ie.to_enum
      end
    end
    result
  end
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/google/protobuf/repeated_field.rb', line 102

def empty?
  self.size == 0
end

#first(n = nil) ⇒ Object



81
82
83
# File 'lib/google/protobuf/repeated_field.rb', line 81

def first(n=nil)
  n ? self[0...n] : self[0]
end

#last(n = nil) ⇒ Object



86
87
88
# File 'lib/google/protobuf/repeated_field.rb', line 86

def last(n=nil)
  n ? self[(self.size-n-1)..-1] : self[-1]
end

#pop(n = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/google/protobuf/repeated_field.rb', line 91

def pop(n=nil)
  if n
    results = []
    n.times{ results << pop_one }
    return results
  else
    return pop_one
  end
end