Skip to content

Instantly share code, notes, and snippets.

@SethHorsley
Created September 24, 2024 15:06
Show Gist options
  • Save SethHorsley/bc58c208f67ff1e5be63955836dea8d1 to your computer and use it in GitHub Desktop.
Save SethHorsley/bc58c208f67ff1e5be63955836dea8d1 to your computer and use it in GitHub Desktop.
Function Piping
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