News for hackers. Stuff that matters.
Re:From Rails to Clojure, Then to Java, Then Back to Rails by kureikain on Wednesday March 21, 2018 @03:19PM (#16641405)
I may sounds weird but Ruby is what make me look more into functional programming. I get done a lot with Ruby due to all method it has on Array/Object/Enumerable.

The beauty of `&` is awesome and I discover that Elixir/Clojure has that and bring it to next level. I think Ruby has inspired many other language to take that path too.

One of the thing about Ruby is that it optimize for happiness, which sound weird. But I really feel happy to write Ruby code. I feel like the language really care about me.

Who don't want to have stuff like this:

``` 10.seconds.from_now ```

Or just call method without `()`, and without even wrapping hash in `{}`.

Of course, lots of these are do-able in Lisp and I like Clojure. But I think the flexibility Ruby gives us is under estimated.

Re:From Rails to Clojure, Then to Java, Then Back to Rails by pavlov on Wednesday March 21, 2018 @06:50PM (#16643184)

  *> 10.seconds.from_now*
I don't understand this kind of weird "urban sprawl OOP" where classes are extended with anything.

Why is 'seconds' a method on numbers? Is there also a 'pixels' method so that I could draw a line like this:

  10.pixels.from.top_left_corner.to.southeast.using.red.stroke
If this doesn't seem like a sensible way of programming graphics, why does the 10.seconds thing get a pass?
Re:From Rails to Clojure, Then to Java, Then Back to Rails by tinco on Wednesday March 21, 2018 @10:28PM (#16645098)
It's not just 'anything' that objects are extended with in Ruby, they are extended with suitable methods pertaining to the domain at hand. 'seconds' is a conversion method of one type into another. You might as well ask why do objects have a 'toString' method. Because it is convenient.
Re:From Rails to Clojure, Then to Java, Then Back to Rails by pavlov on Thursday March 22, 2018 @05:26AM (#16647097)
Why does one conversion method have the “to” prefix and the other doesn’t? I guess because the inconsistency looks somehow prettier.

But this is what drives me up the wall with the Ruby ecosystem: seems that whenever API designers had the choice between consistency and fun gimmick, they chose the latter. It makes some people feel happy, but often makes the next person who has to read and maintain the code miserable.

Re:From Rails to Clojure, Then to Java, Then Back to Rails by jashmatthews on Thursday March 22, 2018 @07:47AM (#16647736)
10.seconds is not part of the Ruby stdlib. It's Rails ActiveSupport. That's why it's different.
Re:From Rails to Clojure, Then to Java, Then Back to Rails by kureikain on Thursday March 22, 2018 @02:29AM (#16646256)
Thank you for explaining this.
Re:From Rails to Clojure, Then to Java, Then Back to Rails by kureikain on Thursday March 22, 2018 @02:29AM (#16646253)
If you think about it, that's just fluent API. And it's up to your to use it to design thing that's convenience and readable for you. When you design an API, you put it to the context. When in context:

10.seconds.from_now

is beautiful to me.

Let's do a quick google for "Fluent API in Java" and you will get something like this:

Invoice inv = Invoice.builder() .createdOn(new Date()) .withDocumentNumber("5150") .shippingOnTrailer("2112") .suppliedBy(InvoiceActor.builder() .named("101") .as(InvoiceActorType.DC) .build()) .beingSentTo(InvoiceActor.builder() .named("42") .as(InvoiceActorType.STORE) .build()) .with(InvoiceItem.builder() .as("TK421") .orderedQuantity(10) .shippedQuantity(8) .build()) .with(InvoiceItem.builder() .as("FN2187") .orderedQuantity(5) .shippedQuantity(5) .build()) .build();

The only difference here is that Ruby allow you to attach method to anything.

But then you have category in Objective C, extension in Swift/Kotlin.

The language gives you the tool, to make your job easier, that's why I wrote "I feel happy when writing Ruby" because I have that power.

To even protected you from changing everything, you can also use refinement to scope this, though it seems underestimated currently IMHO.

When using it the right way, it helps everyone write readable code.

Back to your example:

> 10.pixels.from.top_left_corner.to.southeast.using.red.stroke

It's up to your however you want to design it.

Re:From Rails to Clojure, Then to Java, Then Back to Rails by pimeys on Wednesday March 21, 2018 @05:05PM (#16642400)
It looks nice, but then again:

  irb(main):002:0> 10.methods.count
  => 133
Oh well...
Re:From Rails to Clojure, Then to Java, Then Back to Rails by kureikain on Thursday March 22, 2018 @02:29AM (#16646259)
What's wrong with this? What is your expecation?
Re:From Rails to Clojure, Then to Java, Then Back to Rails by pimeys on Thursday March 22, 2018 @05:57AM (#16647230)
Depends of course what kind of systems you're building. Just the footprint of an integer is too large in Ruby to do anything fast. I'd expect features such as `days` to be a part of Date library that holds an interface accepting certain kind of objects giving dates back. If I don't need these features, I don't want to hold the functionality at all inside an integer.
Re:From Rails to Clojure, Then to Java, Then Back to Rails by blackmagevivi9 on Friday March 23, 2018 @02:06AM (#16655505)
Exactly. Right tool for the job. Ruby doesn't give you fast. That's part of its trade-off. Want web apps and scripts? Go Ruby. Want fast? Control over everything? Go native: C/C++. But also become trench into the swamp of memory leaks, stomps, corruption. Fun times.
Re:From Rails to Clojure, Then to Java, Then Back to Rails by pimeys on Friday March 23, 2018 @03:06PM (#16661545)
Or use Rust/Haskell/OCaml/Clojure.