class Hash::Sorted

Attributes

direction[R]
sort_criteria[R]

Private Class Methods

asc(hash={}, &sort_criteria)
Alias for: ascending
ascending(hash={}, &sort_criteria) click to toggle source
# File lib/hash_ext/sorted.rb, line 60
def ascending(hash={}, &sort_criteria)
  new hash, :asc, &sort_criteria
end
Also aliased as: asc
desc(hash={}, &sort_criteria)
Alias for: descending
descending(hash={}, &sort_criteria) click to toggle source
# File lib/hash_ext/sorted.rb, line 65
def descending(hash={}, &sort_criteria)
  new hash, :desc, &sort_criteria
end
Also aliased as: desc
new(hash={}, direction=:asc, &sort_criteria) click to toggle source
# File lib/hash_ext/sorted.rb, line 19
def initialize(hash={}, direction=:asc, &sort_criteria)
  @hash = hash
  @direction = direction
  @sort_criteria = sort_criteria
end

Public Instance Methods

each() { |k, self| ... } click to toggle source
# File lib/hash_ext/sorted.rb, line 34
def each
  return enum_for(:each) unless block_given?
  keys.each { |k| yield k, self[k]}
end
Also aliased as: each_pair
each_key() { |k| ... } click to toggle source
# File lib/hash_ext/sorted.rb, line 40
def each_key
  return enum_for(:each_key) unless block_given?
  keys.each { |k| yield k }
end
each_pair()
Alias for: each
each_value() { |self| ... } click to toggle source
# File lib/hash_ext/sorted.rb, line 45
def each_value
  return enum_for(:each_value) unless block_given?
  keys.each { |k| yield self[k] }
end
keys() click to toggle source
# File lib/hash_ext/sorted.rb, line 25
def keys
  sorted_keys = @hash.sort_by(&sort_criteria).map { |k,v| k }
  direction == :asc ? sorted_keys : sorted_keys.reverse
end
to_h() click to toggle source
# File lib/hash_ext/sorted.rb, line 50
def to_h
  each_with_object({}) { |(k,v),h| h[k] = v }
end
values() click to toggle source
# File lib/hash_ext/sorted.rb, line 30
def values
  keys.map { |k| self[k] }
end