File: binding_proc.rb

package info (click to toggle)
vagrant-libvirt 0.12.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,756 kB
  • sloc: ruby: 12,865; xml: 2,465; sh: 373; javascript: 235; makefile: 13
file content (26 lines) | stat: -rw-r--r-- 585 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

##
# A simple extension of the Proc class that supports setting a custom binding
# and evaluates everything in the Proc using the new binding.

class ProcWithBinding < Proc
  ##
  # Set the binding for this instance

  def apply_binding(bind, *args)
    @binding = bind
    instance_exec(*args, &self)
  end

  def method_missing(method, *args)
    begin
      method_from_binding = eval("method(#{method.inspect})", @binding)
      return method_from_binding.call(*args)
    rescue NameError
      # fall through on purpose
    end

    super
  end
end