Class: Hash

Inherits:
Object show all
Defined in:
lib/droiuby/support/to_query.rb,
lib/droiuby/support/object/blank.rb,
lib/droiuby/support/object/deep_dup.rb,
lib/droiuby/support/object/to_param.rb,
lib/droiuby/support/object/to_query.rb

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

Returns a deep copy of hash.

hash = { a: { b: 'b' } }
dup  = hash.deep_dup
dup[:a][:c] = 'c'

hash[:a][:c] #=> nil
dup[:a][:c]  #=> "c"


41
42
43
44
45
# File 'lib/droiuby/support/object/deep_dup.rb', line 41

def deep_dup
  each_with_object(dup) do |(key, value), hash|
    hash[key.deep_dup] = value.deep_dup
  end
end

#to_param(namespace = nil) ⇒ Object Also known as: to_query

Returns a string representation of the receiver suitable for use as a URL query string:

{name: 'David', nationality: 'Danish'}.to_param
# => "name=David&nationality=Danish"

An optional namespace can be passed to enclose the param names:

{name: 'David', nationality: 'Danish'}.to_param('user')
# => "user[name]=David&user[nationality]=Danish"

The string pairs “key=value” that conform the query string are sorted lexicographically in ascending order.

This method is also aliased as to_query.



53
54
55
56
57
# File 'lib/droiuby/support/object/to_param.rb', line 53

def to_param(namespace = nil)
  collect do |key, value|
    value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  end.sort * '&'
end