Class: Array

Inherits:
Object show all
Defined in:
lib/extra/array.rb

Instance Method Summary collapse

Instance Method Details

#contains?(klass) ⇒ Boolean

Check if the array contains any instances of a specific class.

Example: ['foo', 1, :bar].contains? Symbol #=> true

Returns:

  • (Boolean)


43
44
45
# File 'lib/extra/array.rb', line 43

def contains?(klass)
	map { |obj| obj.class }.include? klass
end

#flipflopObject

Thanks to manveru for this fun code :) All it does is flip the first and last elements. Pretty cool, eh? :)

Example: [1, 2, 3, 4].flipflop #=> [4, 2, 3, 1]

Returns: Array



9
10
11
12
13
14
15
# File 'lib/extra/array.rb', line 9

def flipflop
	if size > 1
		[last] + self[1...-1] + [first]
	else
		self
	end
end

#flipflop!Object

Destructive version of Array#flipflop.

Returns: Array or nil



21
22
23
24
25
26
# File 'lib/extra/array.rb', line 21

def flipflop!
	if size > 1
		a, b = shift, pop
		unshift(b); push(a)
	end
end

#nothing?Boolean

Similar to String#nothing?, except it joins all the elements first and does the same check.

Example: <tt>[“ ”, “ ”, “”].nothing? #=> true<tt>

Returns: True or false.

Returns:

  • (Boolean)


35
36
37
# File 'lib/extra/array.rb', line 35

def nothing?
	join('').strip.empty?
end