class Array

Public Instance Methods

closest_max(value) click to toggle source

Return the value that is closest to the value in the array, rounded down. [0,5,7,8,11,16].closest_max(6) => 5 [0,5,7,8,11,16].closest_max(7) => 7

# File lib/dm_ruby_extensions/extend_array.rb, line 21
def closest_max(value)
  self.select{|item| item <= value}.max
end
css_join(delimiter = '') click to toggle source

given an array of css classes/styles, join them into one string. only join non-nil/non-empty strings, and return nil if the result is an empty string (rails tag methods will not include the attribute if it is nil, which is desirable for cleaner html)

# File lib/dm_ruby_extensions/extend_array.rb, line 41
def css_join(delimiter = '')
  str = self.flatten.delete_if {|x| x.nil? || x == ''}.join(' ')
  str == '' ? nil : str
end
extract_options!() click to toggle source

Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash.

def options(*args)
  args.extract_options!
end

options(1, 2)           # => {}
options(1, 2, :a => :b) # => {:a=>:b}
# File lib/dm_ruby_extensions/extend_array.rb, line 13
def extract_options!
  last.is_a?(::Hash) ? pop : {}
end
xss_aware_join(delimiter = '') click to toggle source

able to join safe and unsafe strings

# File lib/dm_ruby_extensions/extend_array.rb, line 27
def xss_aware_join(delimiter = '')
  ''.html_safe.tap do |str|
    each_with_index do |element, i|
      str << delimiter if i > 0
      str << element
    end
  end
end