class Array
Author: Stefano Harding <riddopic@gmail.com> 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.
Public Instance Methods
object_state(data = nil)
click to toggle source
Get or set state of object. You can think of object_state
as an in-code form of marshalling.
# File lib/garcon/core_ext/array.rb, line 24 def object_state(data = nil) data ? replace(data) : dup end
sample(n = nil)
click to toggle source
# 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