Feliz 2020

Des-envolver das expectativas da vida, estar presente e em conexão consigo e os movimentos do planeta, do tarot, daquilo que se sente e dar o próximo passo, mesmo sem saber as consequências, mas pelo…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Enumerable avoids using temporary variables when looping

A class representing a collection, such as Array, Set or Hash, has the methods and features of Enumerable included. These methods allow you to loop through the members of that group and take an action using each member of the collection as an input.

…using a C-style loop with a temporary variable, which would be fine in many other languages.

…your deep love of one of Ruby’s Enumerable methods, and implement using #inject.

…there’s a convenient #sum method in Enumerable.

The longer I’ve used Ruby the more joy I find in the elegance enabled by its Enumerable methods. Using these constructions lead you write idiomatic Ruby, which is a smart sounding way of saying you’re writing Ruby in a Ruby-ish manner.

When dealing with larger loops there are often significant improvements to memory usage and speed when using built-in Enumerable methods.

If you ever see the “temporary variable setup” at the beginning of a loop there’s most likely an opportunity to express yourself more concisely with an Enumerable method.

Methods like #inject might seem confusing and be unfamiliar to newer Rubyists. They are a fundamental benefit of using Ruby, so you might as well dive in.

Add a comment

Related posts:

Indulging in plain sight

Once I realised that I was one of a secret group of addicts, I started to notice behaviors that were eerily familiar to me. Just like the woman desperate to get pregnant, surrounded everywhere by…

Token Dad x Design Club

Last month we teamed up with Token Dad to help them run their inaugural event. It was held on a Saturday afternoon in central London.

Kita.

Satu tahunan kita udah bareng-bareng. Ngelewatin suka dukanya. Berbagi cerita, seru-seruan, jalan sana jajan sini, random talk di motor, jatuh dari motor, dandan-dandanan, pokoknya banyak hal yang…