DEV Community

Discussion on: What is your favourite function()?

Collapse
 
joshcheek profile image
Josh Cheek

In Ruby, backticks are a function call. And what's especially cool is that heredocuments with backticks call the same function, but with multi line arguments. So you can define them to do other, more interesting things, like call out to other languages! In practice, I mostly use it when experimenting with some slightly tedious external resource, I basically learned PostgreSQL by running them through these kinds of functions.

require 'open3'

def `(str)
  Open3.capture3('php', stdin_data: str).first
end

<<~`PHP` # => "hello cruel world\n"
hello \
<?php echo "cruel" ?> \
world
PHP