File: array.rb

package info (click to toggle)
ruby-extlib 0.9.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 576 kB
  • sloc: ruby: 7,014; makefile: 5
file content (38 lines) | stat: -rw-r--r-- 847 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'extlib/mash'

class Array

  ##
  # Transforms an Array of key/value pairs into a Hash
  #
  # This is a better idiom than using Hash[*array.flatten] in Ruby 1.8.6
  # because it is not possible to limit the flattening to a single
  # level.
  #
  # @return [Hash]
  #   A Hash where each entry in the Array is turned into a key/value
  #
  # @api public
  def to_hash
    h = {}
    each { |k,v| h[k] = v }
    h
  end

  ##
  # Transforms an Array of key/value pairs into a Mash
  #
  # This is a better idiom than using Mash[*array.flatten] in Ruby 1.8.6
  # because it is not possible to limit the flattening to a single
  # level.
  #
  # @return [Mash]
  #   A Hash where each entry in the Array is turned into a key/value
  #
  # @api public
  def to_mash
    m = Mash.new
    each { |k,v| m[k] = v }
    m
  end
end # class Array