News for hackers. Stuff that matters.
Re:Rails is not written in Ruby by jonnycat on Friday February 04, 2022 @05:44PM (#30214486)
Elixir (and some other languages as well) solves this with the pipe operator:

some_array |> do_thing() |> do_another_thing() |> do_something_else()

Even though Elixir is functional and aggressively not object oriented, if you squint at this pattern you get the readability of an OO-style method chaining API.

Re:Rails is not written in Ruby by omoikane on Friday February 04, 2022 @06:03PM (#30214724)
Re:Rails is not written in Ruby by leifg on Friday February 04, 2022 @06:41PM (#30215135)
If I understand the Ruby pipeline operator it's essentially an alias of the dot. The example in the feature tracker is:

``` 1.. |> take 10 |> map {|e| e2} |> (x) ```

which is equivalent to

``` (1...).take(10).map{|e| e2} ```

So in order to use it you still need extensions on every single object. The only reason that looks nice is because in this example you continuously call methods on lists that return a list. You can't add `my_custom_list_operation(list_input)` in between.

Re:Rails is not written in Ruby by WallowingInShit on Saturday February 05, 2022 @10:31AM (#30222028)
Same thing, but for JavaScript: https://github.com/tc39/proposal-pipeline-operator

Going forward, the pipe operator is going to be showing up in many new languages. I've used elixir a lot, and the pipe operator is a genius piece of syntactic sugar.

Re:Rails is not written in Ruby by dragonwriter on Friday February 04, 2022 @07:10PM (#30215414)
It's worth noting that that version of pipeline operator was abandoned, while there are still (some newer) open feature requests for a more traditional functional-style pipeline operator.
Re:Rails is not written in Ruby by tylerscott on Friday February 04, 2022 @09:38PM (#30216519)
yield_self is an less elegant way to achieve basically the same thing as |>.
Re:Rails is not written in Ruby by jon1628492 on Friday February 04, 2022 @06:03PM (#30214722)
Or alternatively,

    some_array
    |> do_thing()
    |> do_another_thing()
    |> do_something_else()
Re:Rails is not written in Ruby by amw-zero on Saturday February 05, 2022 @01:54AM (#30218524)
Now replace |> with . And you have the previous code example. The difference is completely superficial.
Re:Rails is not written in Ruby by jonnycat on Saturday February 05, 2022 @07:28AM (#30220380)
The end result is superficially different. That's the point - we like the flow of the end result! But the implementation is completely different, as it's pure functional compared to object oriented.

"string".do_stuff() - do_stuff() is a method on the "string" class, with all of the potential gotchas that might come with an OO implementation. Does the method mutate the internal state of the object? Does the particular class override the default implementation? Is it a monkey-patched implementation (the issue raised by this article)?

"string" |> do_stuff() - do_stuff() is a standalone pure function. The data is immutable and does exactly what it says.

Re:Rails is not written in Ruby by machiaweliczny on Saturday February 05, 2022 @06:40AM (#30220162)
It's not because you can use any function with pipeline and with dots chaining you can use only methods returned by previous function call.
Re:Rails is not written in Ruby by kazinator on Saturday February 05, 2022 @01:17PM (#30223938)
That is false; you cannot chain functions that do not return a value that is suitable as an argument to the next function.

a.this().that() requires that a.this() returns an object on which .that() can be invoked.

(that (this a)) requires that (this a) returns an object that is a suitable argument to that.

Re:Rails is not written in Ruby by kazinator on Saturday February 05, 2022 @01:15PM (#30223914)
Not completely, because the . has a meaning: select a member/field/slot from a class object.