Class: Array

Inherits:
Object show all
Defined in:
lib/garcon/core_ext/array.rb,
lib/garcon/utility/faker/extensions/array.rb

Overview

Author: Stefano Harding <[email protected]> License: Apache License, Version 2.0 Copyright: © 2014-2015 Stefano Harding

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Method Summary collapse

Instance Method Details

#object_state(data = nil) ⇒ Object

Get or set state of object. You can think of #object_state as an in-code form of marshalling.



24
25
26
# File 'lib/garcon/core_ext/array.rb', line 24

def object_state(data = nil)
  data ? replace(data) : dup
end

#sample(n = nil) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/garcon/utility/faker/extensions/array.rb', line 3

def sample(n = nil)
  #based on code from https://github.com/marcandre/backports
  size = self.length
  return self[Kernel.rand(size)] if n.nil?

  n = n.to_int
  raise ArgumentError, "negative array size" if n < 0

  n = size if n > size

  result = Array.new(self)
  n.times do |i|
    r = i + Kernel.rand(size - i)
    result[i], result[r] = result[r], result[i]
  end
  result[n..size] = []
  result
end