File: to_h.rb

package info (click to toggle)
ruby-facets 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,824 kB
  • sloc: ruby: 25,483; xml: 90; makefile: 20
file content (22 lines) | stat: -rw-r--r-- 314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Proc

  # Build a hash out of a Proc.
  #
  #   l = lambda { |s|
  #     s.a = 1
  #     s.b = 2
  #     s.c = 3
  #   }
  #   l.to_h  #=> {:a=>1, :b=>2, :c=>3}
  #
  #   CREDIT: Trans

  def to_h
    h = {}
    f = Functor.new{ |op, arg| h[op.to_s.chomp('=').to_sym] = arg }
    call( f )
    h
  end

end