3 ms·
ls | grep file.txt vs ls().grep("file.txt")
by ilvez 4mo ago
ls | grep file.txt
vs
ls().grep("file.txt")
- shevy-java 4mo agoYour comparison is not quite optimised as you use () which is not necessary. But I understand the comparison you make. But, you can write an optimised pipe in ruby too. I actually did that, because I could not want to be bothered to be restricted via ruby's syntax for pipe-like operations. Even aside from that, the original claim was about pipes versus method chaining. To me these are not orthogonal to one another; they are very similar. Just with the pipe focusing on tying together different programs and focusing on input-output functionality. Method chaining in ruby is a bit more flexible, we have blocks, and usually the methods chained occur in one class/object or the toplevel namespace (less frequently though, usually). Even the pipe comparison is not ideal, because traditional UNIX pipes don't support e. g. data manipulation via an object-oriented focus. And I want that (see avisynth, but extend the idea there via a) nicer syntax and b) data manipulation for EVERYTHING). I don't see pipe as being exclusive over method chaining or reverse. One interesting idea was to add |> elixir's pipe-like operator to ruby. I like that, but indeed, the net-gain in ruby is quite minimal since method-chaining + blocks already offer a ton of flexibility, so I am not sure how |> would fit into ruby 1:1. Still I like the idea, but anyone proposing |> needs to come up with really convincing ideas to matz here. Because people WILL ask what the real difference is to method chaining. Even fail-safe method chaining in ruby though I absolutely hate the syntax via ? there ... it reads like garbage to me. Example: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L191 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_threa... t1&.kill&.join (It has moved since then, so the above link no longer works, been some years since I first saw it. Upon seeing it my brain instantly cancelled any use of "&.", even though I understand the rationale. It is just ugly to no ends. I still like the |> syntax in Elixir though, even though I can not really see what this should do in ruby.)
- ilvez 4mo agoNote that I just elaborated what I thought was being asked. Parantheses - see what switching professionally to Ruby wonderland to Python does to a person! Just in about half a year needed..
- adamtulinius 4mo agoThe docs literally says that the () on the first ls is required.
- ifh-hn 4mo agoI'm coming from the point of view of nushell, or powershell, both of which can be used for data manipulation (tables and objects respectively). I love programming in nushell compared to something like this.
- em-bee 4mo agothe difference between method chaining and the pipe operator is that method chaining normally only works on methods that are supported by the object being returned, whereas the pipe operator works on any function that can take that object as input. in other words the pipe operator is a lot more flexible and doesn't require me to patch the objects class in order to add support for the function i want to call with it. method chaining syntax works in rubish because it either defines or pretends that all unix commands are methods of some class that represents the output of a command.
- em-bee 4mo agoyour example is here: https://github.com/ruby/ruby/blob/master/test/ruby/test_thread.rb#L201 https://github.com/ruby/ruby/blob/master/test/ruby/test_thre... although that line could move again, so here is a permalink: https://github.com/ruby/ruby/blob/6c6df00bbcefe/test/ruby/test_thread.rb#L201 https://github.com/ruby/ruby/blob/6c6df00bbcefe/test/ruby/te...
- fhn 4mo agoyeah but chaining is almost ubiquitous - found in js, ruby, python, rust, go, etc
- ifh-hn 4mo agoBut so are pipes, right? Bash, and the many variants, powershell, nushell... Not to mention the languages that implement them with slightly different symbols (|> in R for example).