10.times do
puts “Hello”
end
or send_email if user.unconfirmed?
Compared to ES6 as an example: Array.from(Array(10)).forEach((_, _i) => {
console.log(“Hello”);
});
Ooph, not the strongest showing by comparison. There isn’t a huge issue with it, but it is having to use a lot more indirection to express the main idea. Languages like Python get closer to that expressiveness as well, but I think Ruby goes even farther in the direction of expressive choice.I don’t write Ruby anymore. But there were some nice things that I miss sometimes when using other languages. Sometimes I want to express a very simple idea that, while still readable, gets wrapped in brackets, parens, variable declarations, and type declarations to a point where it looks more intimidating and unapproachable than it actually is.
[...new Array(1000)].foreach(() => console.log('Hello'))
But yes, ruby is much nicer.for (var i = 0; i < 1000; i++) {
console.log("Hello world");
}In the for loop example, you need to name and explicitly initialize a variable, you need to explicitly write down the comparison, and you need to explicitly increment the variable.
In the Ruby example, the plumbing is hidden from view, and you can just tell the runtime how often you'd like the thing to happen.
On a tangential note, in Raku (the language formerly known as Perl6), you could have written that as
say("Hello world") xx 10;
That's pretty hard to beat, imo. import times from 'lodash.times'
[0] https://lodash.com/docs/4.17.15#times