loading...

.all? method of Enumerable for Array, Hash in Ruby is awesome!

twitter logo github logo ・1 min read

What is .all?

https://ruby-doc.org/core-2.7.0/Enumerable.html

🤔 Situations1: Stop the short circuit evaluation ( && )

Because .valid? has side effect and they want all of forms to evaluate validation, they can't use &&.

@form_a.valid? & @form_b.valid?  # 🤔 evaluate both of valid? but using & is not natual
@form_a.valid? && @form_b.valid? # 🤔 evaluate only forward if it is false

👍 Solve it

forms = [@form_a.valid?, @form_b.valid?]
(forms.map(&:valid?)).all?

🤔 Situation2: isn't there any errors?

valid = true
users.each do |user|
  valid = false unless user.valid?
end
valid

👍 Solve it

users.map{ |user| user.valid? }.all?
twitter logo DISCUSS
Classic DEV Post from Aug 29 '19

How can open source contributors and maintainers engage in a respectful ongoing relationship?

n350071 profile image
Ruby Web Developer. Freelancer.

👋 Hey dev.to reader.

Do you prefer sans serif over serif?

You can change your font preferences in the "misc" section of your settings. ❤️