Class: Array

Inherits:
Object show all
Defined in:
lib/ruby_smart/support/core_ext/ruby/array.rb,
lib/ruby_smart/support/core_ext/ruby/array.rb

Instance Method Summary collapse

Instance Method Details

#only(*values) ⇒ Array

returns a new array with only given values (if exists)

Examples:

ary = [:foo, :bar, :bat]
ary.only(:bar, :bat, :moon)
> [:bar, :bat]

Parameters:

Returns:



32
33
34
# File 'lib/ruby_smart/support/core_ext/ruby/array.rb', line 32

def only(*values)
  dup.only!(*values)
end

#only!(*values) ⇒ Array

refactors the same array with only given values (if exists)

Examples:

ary = [:foo, :bar, :bat]
ary.only!(:bar, :moon)
> ary == [:bar]

Parameters:

Returns:



14
15
16
17
# File 'lib/ruby_smart/support/core_ext/ruby/array.rb', line 14

def only!(*values)
  reject! { |value| !values.include?(value) }
  self
end