M
M
Magic Code2020-07-14 19:29:44
ruby
Magic Code, 2020-07-14 19:29:44

How to simplify code using block?

How to make the following code less verbose:

# frozen_string_literal: true

vowels = %w[a e i o u]
found = {}
founded = 0

puts 'Puts the word: '
words = gets.chomp.chars
words.each do |n|
  next unless vowels.include?(n)

  found.store(n, 0)
  puts "We have #{founded + 1} vowel(s)."
  found[n] += 1
end

Is another way possible?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-07-14
@Panda_Code

vowelsCount = Hash['aeiou'.chars.map{|n| [ n, 0 ]}]
gets.chomp.chars.each{|n| vowelsCount[n] += 1 if vowelsCount.key?(n)}

print(vowelsCount)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question