Hacker News new | ask | show | jobs
by gorgoiler 1793 days ago
Well I never: today I learned about break expressions:

    nine = while 1 do break 9 end
    puts nine #=> “nine”
I love Ruby. I am not sure how I feel about this. It looks useful for the odd thing or two, maybe.
2 comments

Ruby makes me want to become a programmer.
My dislike for Ruby was one of the main reasons I left my last job... but I think that opinion will probably get an angry mob after me here on Hacker News haha. And honestly, it could have just been the way they were writing Ruby there that drove me to leave, but the language has left a sour taste in my mouth.
What do you dislike about ruby and what do you prefer against it?
There was too much "magic" going on. I could never figure out where variables or functions were coming from. Sometimes I would have to sit down with our local Ruby guru (a principal engineer who has been working with Ruby for years and years) and it would take us the better part of an afternoon to track down what a function call was doing.

Again, that all could have been a side effect of horribly written Rails code where I was working. The Ruby guru I mention above would often lament about how bad our Ruby codebase was. To me, it just showed "wow it's really possible to write code this bad in this language" and left a sour taste in my mouth.

I usually prefer TypeScript for scripting things. Being able to run it via node and in the browser is a killer feature and I have many small libraries I've written that I reuse in a lot of personal projects. Sometime I use Python but I'm not a huge fan of Python's version and dependency management.

For anything that's not destined for the browser that require speed or a larger codebase (i.e. backend services) I don't think you can go wrong with the JVM. I prefer just plain Java, but also have used Scala and Kotlin extensively and they're all great for speed, collaboration, readability, testability, and have robust ecosystems.

Does nine really end up containing a string like that? I would have expected it to be the number 9.
The example is syntactically incorrect Crystal. Should be

  nine = while 1; break 9; end
  puts nine
And yes, it prints "9".
What is going on with the upvotes here? The incorrect comment by OP is upvoted, while this correct comment is downvoted.

  irb(main):001:0> nine = while 1 do break 9 end
  => 9

  irb(main):002:0> nine.class
  => Integer

  irb(main):003:0> puts nine
  9
  => nil
puts might be calling .to_s on the variable, which is why you see a string, but the loop does return an integer.
And just to be clear, to_s will return a string, but it will definitely not do the craziness of converting an integer into an English representation of the number!

    9.to_s
    => "9"
Sorry. Yes, you are correct.

I over-edited my comment to fit a narrow screen, breaking the example.