Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/dm_ruby_extensions/extend_array.rb
Overview
Instance Method Summary collapse
-
#closest_max(value) ⇒ Object
Return the value that is closest to the value in the array, rounded down.
-
#css_join(delimiter = '') ⇒ Object
given an array of css classes/styles, join them into one string.
-
#extract_options! ⇒ Object
Extract options from a set of arguments.
-
#xss_aware_join(delimiter = '') ⇒ Object
able to join safe and unsafe strings ——————————————————————————.
Instance Method Details
#closest_max(value) ⇒ Object
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
21 22 23 |
# File 'lib/dm_ruby_extensions/extend_array.rb', line 21 def closest_max(value) self.select{|item| item <= value}.max end |
#css_join(delimiter = '') ⇒ Object
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)
41 42 43 44 |
# 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! ⇒ Object
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 (*args)
args.
end
(1, 2) # => {}
(1, 2, :a => :b) # => {:a=>:b}
13 14 15 |
# File 'lib/dm_ruby_extensions/extend_array.rb', line 13 def last.is_a?(::Hash) ? pop : {} end |
#xss_aware_join(delimiter = '') ⇒ Object
able to join safe and unsafe strings
27 28 29 30 31 32 33 34 |
# 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 |