Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

On that side, as for many others, Ruby captures my love. You can use `{…}` or `do … end` as you see best fit. And enforcing some indentation convention is always an option with some upper level tools. What makes perfect sense for integration in a large codebase is ridiculous while interacting with a REPL.

Now Ruby as his own defects of course. Static type can be integrated through Sorbet, but that still something out of the core for now. On that side, PHP had during the last years shown a far more remarkable pace in integrating many advanced feature for OOP (and functional stuffs are also progressing well). That said, the foundation of Ruby still win my preference in term of elegance and feeling of coherence.

I didn’t touch that much Python over the last few years. The only (significant) reason I would see it as opportune is its heavy use in ML and the like domains, and I didn’t have an opportunity to dig this domain much.



> You can use `{…}` or `do … end` as you see best fit.

I don't use Ruby, so I don't know what happens in the Ruby world, but...

Doesn't this create a Holy War akin to C/C++'s fighting over where the opening brace goes (Current line vs next line)?


It doesn't, because

  3.times do
    print 'Welcome '
  end
this works, but

  3.times
  do
    print 'Welcome '
  end
does not work. Same with curly braces.


I don't mean where to put the opening "do", I mean, a fight between:

    3.times {
        print 'Welcome '
    }
and

    3.times do
        print 'Welcome '
    end


Ah!

So, Ruby is a bit weird: the Ruby community itself tends to be much closer to Perl than Python, that is, the language doesn't mind embracing many ways of doing the same thing. However, the Rails community tends to curb this attitude a bit by being really strongly about conventions, and following them.

In practice, this means that a significant amount of the community uses one of the two rules:

> Use {} for one line blocks, use do/end for multiline blocks.

or

> Use {} for blocks that produce a value, use do/end for side effects.

The latter is called "Weirich style," after a very prominent Rubyist using that style.

Hilariously, your example would be code that could go either way, depending on which style you choose. Though I would think that people who subscribe to the first rule would write it

   3.times { print 'Welcome ' }
as the "one line" really kinda leans this way.

Anyway that's as far as I know, my Ruby knowledge is about 10 years old at this point, so maybe things have evolved since then, but I haven't really heard of any changes here.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: