Last active
April 20, 2023 21:35
-
-
Save casperisfine/86341b48c5b89566928c1e5456d843e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test(name) | |
module_eval("private; def #{name}; end") | |
end | |
def test2(name, private: nil) | |
module_eval("private; def #{name}; end") | |
end | |
module Foo | |
puts '-'* 40 | |
test("foo") | |
puts '-'* 40 | |
test2("bar", private: nil) | |
puts '-'* 40 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$VERBOSE = true | |
def delegate(*methods, to: nil, private: nil) | |
location = caller_locations(1, 1).first | |
file, line = location.path, location.lineno | |
receiver = to.to_s | |
method_def = [] | |
method_names = [] | |
method_def << "private" if private | |
methods.each do |method| | |
method_name = method | |
method_names << method_name.to_sym | |
method = method.to_s | |
method_name = method_name.to_s | |
method_def << | |
"def #{method_name}" << | |
"end" | |
end | |
source = method_def.join("\n") | |
puts "------ eval -------" | |
puts source | |
puts "------ end-eval -------" | |
module_eval(source, file, line) | |
method_names | |
end | |
module Foo | |
puts "this warns:" | |
delegate(:street, to: :@place, private: true) | |
end | |
module Bar | |
puts "this doesn't:" | |
module_eval <<~RUBY, __FILE__, __LINE__ + 1 | |
private | |
def street | |
end | |
RUBY | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment