Class: Hash

encoding: utf-8

Public Visibility

Public Instance Method Summary

#deep_symbolize_keys

Return duplication of self with all keys recursively converted to symbols.

Returns: Hash

#deep_symbolize_keys!

Recursively replace keys in self by coresponding symbols.

Returns: Hash

#extract!(*args)

Returns the value of self for each argument and deletes those entries.

Returns: Array<Object>

#get(*keys)

Simplier syntax for code such as params[:post] && params[:post][:title].

Returns: Object

#reverse_merge(another)

Returns: String

#reverse_merge!(another)

Returns: String

#symbolize_keys

Return duplication of self with all keys non-recursively converted to symbols.

Returns: Hash

#symbolize_keys!

Replace keys in self by coresponding symbols.

Returns: Hash

#to_html_attrs

Returns: String

#to_native
#to_url_attrs

Returns: String

Public Instance Methods Inherited from Object

define_instance_method, not_nil?, try

Public Instance Method Details

deep_symbolize_keys

public Hash deep_symbolize_keys

Return duplication of self with all keys recursively converted to symbols

Meta Tags

Parameters:

Returns:

[Hash]

A hash with all keys transformed into symbols even in inner hashes

Author:

Botanicus

Since:

0.0.2

[View source]


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rango/ext/hash.rb', line 30

def deep_symbolize_keys
  self.inject(Hash.new) do |result, array|
    key, value = array.first, array.last
    if value.respond_to?(:symbolize_keys)
      result[key.to_sym] = value.symbolize_keys
    else
      result[key.to_sym] = value
    end
    result
  end
end

deep_symbolize_keys!

public Hash deep_symbolize_keys!

Recursively replace keys in self by coresponding symbols

Meta Tags

Parameters:

Returns:

[Hash]

A hash with all keys transformed into symbols even in inner hashes

Author:

Botanicus

Since:

0.0.2

[View source]


47
48
49
# File 'lib/rango/ext/hash.rb', line 47

def deep_symbolize_keys!
  self.replace(self.deep_symbolize_keys)
end

extract!

public Array<Object> extract!(*args)

Returns the value of self for each argument and deletes those entries.

Meta Tags

Parameters:

Returns:

[Array<Object>]

The values of the provided arguments in corresponding order.

Author:

Botanicus

Since:

0.0.2

[View source]


62
63
64
65
66
# File 'lib/rango/ext/hash.rb', line 62

def extract!(*args)
  args.map do |arg|
    self.delete(arg)
  end
end

get

public Object get(*keys)

Simplier syntax for code such as params[:post] && params[:post][:title]

Meta Tags

Parameters:

Returns:

[Object, nil]

The value of the most inner hash if found or nil.

Raises:

[ArgumentError]

If you don’t specify keys.

[IndexError]

If you work with final result as with hash, so basically you are trying to call fetch method on string or whatever.

Author:

Jakub Stastny aka Botanicus

Since:

0.0.3

[View source]


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rango/ext/hash.rb', line 130

def get(*keys)
  raise ArgumentError, "You must specify at least one key" if keys.empty?
  keys.inject(self) do |object, key|
    # Hash#fetch works similar as Hash#[], but [] method is
    # defined for too many objects, also strings and even numbers
    if object.respond_to?(:fetch)
      begin
        object.fetch(key)
      # Hash#fetch raise IndexError if key doesn't exist
      rescue IndexError
        return nil
      end
    else
      raise IndexError, "Object #{object.inspect} isn't hash-like collection"
    end
  end
end

reverse_merge

public String reverse_merge(another)

Meta Tags

Parameters:

Returns:

[String]

Merge self into hash given as argument

Author:

Botanicus

Since:

0.0.2

[View source]


89
90
91
# File 'lib/rango/ext/hash.rb', line 89

def reverse_merge(another)
  another.merge(self)
end

reverse_merge!

public String reverse_merge!(another)

Meta Tags

Parameters:

Returns:

[String]

Replace self by result of merge self into hash given as argument

Author:

Botanicus

Since:

0.0.2

[View source]


96
97
98
# File 'lib/rango/ext/hash.rb', line 96

def reverse_merge!(another)
  self.replace(self.reverse_merge(another))
end

symbolize_keys

public Hash symbolize_keys

Return duplication of self with all keys non-recursively converted to symbols

Meta Tags

Parameters:

Returns:

[Hash]

A hash with all keys transformed into symbols

Author:

Botanicus

Since:

0.0.2

[View source]


9
10
11
12
13
14
# File 'lib/rango/ext/hash.rb', line 9

def symbolize_keys
  self.inject(Hash.new) do |result, array|
    result[array.first.to_sym] = array.last
    result
  end
end

symbolize_keys!

public Hash symbolize_keys!

Replace keys in self by coresponding symbols

Meta Tags

Parameters:

Returns:

[Hash]

A hash with all keys transformed into symbols

Author:

Botanicus

Since:

0.0.2

[View source]


21
22
23
# File 'lib/rango/ext/hash.rb', line 21

def symbolize_keys!
  self.replace(self.symbolize_keys)
end

to_html_attrs

public String to_html_attrs

Meta Tags

Parameters:

Returns:

[String]

A string formatted for HTML attributes

Author:

Botanicus

Since:

0.0.2

[View source]


73
74
75
# File 'lib/rango/ext/hash.rb', line 73

def to_html_attrs
  self.map { |key, value| "#{key}='#{value}'" }.join(" ")
end

to_native

public to_native

Deprecated.

Meta Tags

Parameters:

Author:

Botanicus

Since:

0.0.2

[View source]


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rango/ext/hash.rb', line 103

def to_native
  self.each do |key, value|
    value = case value
    when "true" then true
    when "false" then false
    when "nil" then nil
    when /^\d+$/ then value.to_i
    when /^\d+\.\d+$/ then value.to_f
    else value end
    self[key.to_sym] = value
  end
  return self
end

to_url_attrs

public String to_url_attrs

Meta Tags

Parameters:

Returns:

[String]

A string formatted for URL

Author:

Botanicus

Since:

0.0.2

[View source]


82
83
84
# File 'lib/rango/ext/hash.rb', line 82

def to_url_attrs
  self.map { |key, value| "#{key}=#{value}" }.join("&")
end