Files
news.gitea.happylizard.me/bin/gems/facets-3.1.0/lib/standard/facets/binding/block_exec.rb
T
pizza2d1 4cee170d66
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s
Add bin and edit workflow
2026-09-16 13:11:16 -06:00

45 lines
988 B
Ruby

class Binding
# Allows the evaluation of blocks by a Binding in the same way that strings
# can be evaluated.
#
# x = 5
# $my_binding = binding
#
# class Test # just here to provide a scope gate
# $my_binding.block_exec { x }
# end
#
# # => 5
#
# NOTE: The implementation of this method uses a method_missing trick.
# Consequently it is a bit of a hack.
#
def block_exec(*args, &block)
BlockEnvironment.new(self, *args, &block).call
end
# This class is used for evaluating blocks in a particular context.
# It is not really meant to be used independently; instead it is used
# with Binding#block_exec method.
class BlockEnvironment < BasicObject
def initialize(context, *args, &block)
@context = context
@args = args
@block = block
end
def call
instance_exec(*@args, &@block)
end
def method_missing(name)
eval(name.to_s, @context) rescue super
end
end
end