Created
March 25, 2022 16:16
-
-
Save okuramasafumi/146cea1e32fcb61ca3836e7e6dd71737 to your computer and use it in GitHub Desktop.
Mixable method where we can sort method call sequence
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
class Module | |
def define_mixable_method(name:, calls:, main:) | |
define_method(name) do |&block| | |
method_candidates = calls.map {|name| method(name) } | |
block.call(main, method_candidates).each do |m| | |
m.call | |
end | |
end | |
end | |
end | |
class Foo | |
def a | |
puts 'a' | |
end | |
def b | |
puts 'b' | |
end | |
define_mixable_method(name: :y, calls: [:a, :b], main: -> { puts 'y' }) | |
end | |
foo = Foo.new | |
foo.y do |main, candidates| | |
[main] + candidates.reverse | |
end | |
# => y, b, a | |
foo.y do |main, candidates| | |
candidates + [main] | |
end | |
# => a, b, y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment