Talks

Micro Talk: Pipe Operator for Ruby

Pipe Operator for Ruby by Fabio Akita

Elixir is one modern language that is introducing many Rubyists to the world of highly scalable, highly distributed, functional programming-based programming. In a more narrow scope, one language feature that many people liked is the now famous Pipe Operator . There is nothing like this in Ruby. But could there be such an operator? And if it could be done, would it be useful? I started a pet project called Chainable Methods to address just that.

Help us caption & translate this video!

http://amara.org/v/Prkf/

GoRuCo 2016

00:00:16.080 Good afternoon, everybody! Thank you for having me here. Thanks to the organizers for the opportunity. My name is Fabio Akita, most well known as Akiras on Twitter and other social networks. I'm also the co-founder of a software outsourcing company in Brazil called Miner.
00:00:24.240 We have offices in six different locations across Brazil, and we work for several U.S. companies, such as VTS here in New York and others in California. Additionally, I organize the RubyConf Brazil, which welcomes about a thousand developers from all over the country every year. You're all, of course, invited to join us, and this year will mark our ninth conference.
00:00:40.320 If you're interested, please follow RubyConf BR on Twitter. Now, let's talk about the Pipe Operator. For those of you who have tried Elixir, it has this new syntax and uses cool technologies from Erlang. I think you should definitely give it a try. The first noticeable feature is the unique syntax, especially the Pipe Operator.
00:01:07.439 This operator is displayed differently in official books and looks something like this. Since Elixir is not an object-oriented language, it does not utilize dot notation like we do in Ruby. Instead, it employs this Pipe Operator to chain functions together, where the last result becomes the first argument for the next function call.
00:01:20.439 This isn’t the first language to implement such a feature; if you're familiar with other languages, you might encounter similar concepts. The idea behind this pipe is to avoid writing ugly code that is hard to read. Using the pipe operator allows for cleaner, more maintainable code.
00:01:36.720 Languages like F#, Haskell, and other functional languages have their variations of function composition. In Ruby, we often experience feature envy and wonder why we can’t have something equally neat. However, we can let that go and remember that we already have something similar, which is dot notation with collections.
00:01:53.880 To present an alternative, I want to demonstrate a first example using Linux piping and redirection. We are all familiar with bash programming. In the Shell, you can create one-liners that get results through pipelines, as demonstrated with the `netstat` command.
00:02:05.600 In this example, a simple command gets processed through various stages. When we use network utility commands like `netstat`, the output can be piped to another command to filter results systematically. This flexibility allows for quick experimentation with data manipulation.
00:02:40.159 Now, if we consider how to approach this in Elixir, it allows us to filter and map over the output efficiently. For instance, we can start with the raw output and apply transformations like `split`, `filter`, and `map` to parse the information we want. While Elixir is functional, I created a helper function called `run_cmd` to streamline this.
00:03:04.640 This example illustrates a quick prototype, although it’s not beautiful code. My argument is that while we don't need to implement code like this in production, it's useful for rapid prototyping. It allows us to clarify the workflow before making abstractions or over-engineering the solution.
00:03:51.240 Elixir lets us create elegant data flow through chained commands, but this raises the question of how we could implement a similar structure in Ruby. Despite Ruby's limitations, we can still draft a prototype to emulate the pipe operation.
00:04:28.279 In Ruby, we often find ourselves with temporary variables holding state, which can lead to cluttered and difficult-to-read code. We begin abstracting functionality into classes, resulting in unnecessary complexity. However, what if there was a cleaner approach?
00:04:50.560 I propose a framework utilizing chainable methods that allows for smooth function chaining. This method can streamline data transformations, minimizing the need for your messy temporary variables. You can use the `chainable_methods` gem that I published to create elegant workflows.
00:05:38.640 In this approach, you design a workflow where data is transformed step-by-step. This avoids the pitfalls of temporary variables and messy middle steps. For instance, we can parse text and chain functions that perform specific transformations directly.
00:06:01.760 When you create chains like this, you can minimize the clutter of multiple variables while achieving the desired transformations in a cleaner fashion. This can help us reevaluate our code while implementing changes efficiently.
00:06:51.360 The `chainable_methods` gem allows you to use a string as the initial input. You can then chain your methods directly. If the objects return the required methods, you can effortlessly keep adding transformations through the pipe syntax.
00:07:33.720 I hope this gives you insight into creating more streamlined code in Ruby and improves your interface discovery without unnecessary abstractions. After today’s presentation, you will get to see the new Crystal version of these chainable methods, so you can explore both languages.