Class: Array

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

Overview

Borrowed from Rails source

Instance Method Summary collapse

Instance Method Details

#to_paramObject

Calls to_param on all its elements and joins the result with slashes. This is used by url_for in Action Pack.


6
7
8
# File 'lib/extend/array.rb', line 6

def to_param
  collect { |e| e.to_param }.join '/'
end

#to_query(key) ⇒ Object

Converts an array into a string suitable for use as a URL query string, using the given key as the param name.

['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"

14
15
16
17
18
19
20
21
22
# File 'lib/extend/array.rb', line 14

def to_query(key)
  prefix = "#{key}[]"

  if empty?
    nil.to_query(prefix)
  else
    collect { |value| value.to_query(prefix) }.join '&'
  end
end