vert-x3 / vertx-lang-ruby

Vert.x Ruby support
Apache License 2.0
14 stars 12 forks source link

The Ruby API is not idiomatic #41

Open eregon opened 5 years ago

eregon commented 5 years ago

Example from https://vertx.io/:

$vertx.create_http_server().request_handler() { |req|
  req.response()
    .put_header("content-type", "text/plain")
    .end("Hello from Vert.x!")
}.listen(8080)

Global variables like $vertx are basically discouraged and deprecated (https://github.com/rubocop-hq/ruby-style-guide#instance-vars). Global functionality is idiomatically stored in Constants like Vertx or VertX.

Using () after a method call is not idiomatic Ruby (https://github.com/rubocop-hq/ruby-style-guide#method-calls-with-no-arguments).

With that, the code looks like this:

VertX.create_http_server.request_handler { |req|
  req.response
    .put_header("content-type", "text/plain")
    .end("Hello from Vert.x!")
}.listen(8080)

Which already feels much closer to idiomatic Ruby code.

vietj commented 5 years ago

can you make a PR on the vertx-web-site repo with these improvements ?

eregon commented 5 years ago

For the (), fixing it in the documentation would be indeed enough, but for replacing $vertx by VertX, I would guess one would need to modify how the Ruby part of Vert.x is initialized, unless there is already a Vertx or VertX constant?

vietj commented 5 years ago

that makes sense to me.

verticles have access to $vertx for the vertx instance: see https://vertx.io/docs/vertx-core/ruby/#_writing_verticles https://vertx.io/docs/vertx-core/ruby/#_writing_verticles

should it changed to something else to be more idiomatic ?

On 23 Jul 2019, at 20:39, Benoit Daloze notifications@github.com wrote:

For the (), fixing it in the documentation would be indeed enough, but for replacing $vertx by VertX, I would guess one would need to modify how the Ruby part of Vert.x is initialized, unless there is already a Vertx or VertX constant?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vert-x3/vertx-lang-ruby/issues/41?email_source=notifications&email_token=AABXDCTOI4TLQH36AIDXNK3QA5F7TA5CNFSM4IGGBHFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2UBRKY#issuecomment-514332843, or mute the thread https://github.com/notifications/unsubscribe-auth/AABXDCR5ZKDL7EVBUXYXDRDQA5F7TANCNFSM4IGGBHFA.

eregon commented 5 years ago

should it changed to something else to be more idiomatic ?

I guess it's direct translation of the vertx global variable from JavaScript.

I think a constant would be more idiomatic, if the value never changes for the whole program. However, Vertx seems already used as a module.

Vertx::Vertx.vertx seems to currently be the same as $vertx, but that's very long.

Maybe VertX (but it's quite subtle about the case) or Vert.x (a method call x on a Vert constant)?