Created
September 24, 2024 15:06
-
-
Save SethHorsley/bc58c208f67ff1e5be63955836dea8d1 to your computer and use it in GitHub Desktop.
Function Piping
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 Object | |
def pt(next_step) | |
case next_step | |
when Symbol | |
Kernel.send(next_step, self) | |
when String | |
method_name, *args = next_step.split(/[()]/).reject(&:empty?) | |
Kernel.send(method_name, self, *args) | |
else | |
raise TypeError, "Unsupported type for piping. Use Symbol for methods or String for methods with args." | |
end | |
end | |
end | |
# Define the methods for our example | |
def makecap(str) | |
str.upcase | |
end | |
def validate(str, validator) | |
str.include?(validator) | |
end | |
# Usage with the new syntax | |
result = "#my String".pt("makecap").pt("validate(#)") | |
puts result # Should print: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment