Ruby - If Elsif Else Error

I am getting an error here with a simple if else chain, and I cannot figure out what is going on. The other day, I started to study ruby, I already know some java and just tried to rewrite programs to quickly find out ruby. I am trying to count the vowels and consonants. Anyways here is my code ...

#!/usr/bin/ruby/
alphabet = 'abcdefghijklmnopqrstuvwxyz'

array = alphabet.chars.to_a
vowel = 0
cons = 0
puts array.at(1)
for i in 0...26 
    if array.at(i) == "a"
        vowel++   
    elsif array.at(i) == 'e'
        vowel++
        elsif array.at(i) == 'i'
        vowel++
    elsif array.at(i) == 'o'
        vowel++
    elsif array.at(i) == 'u'
        vowel++
    else
        cons++
    end#end if else chain
end#end for loop

puts 'Vowel: ' + vowel.to_s
puts 'Consonants: ' + cons.to_s

Here is the error I get:

C: / Users / Kelan / Documents / Programming / ruby ​​Files / Small programs /Alphabet.rb: 11: syntax error, unexpected keyword_elsif elsif array.at (i) == 'e' ^

C: / Users / Kelan / Documents / Programming / ruby ​​Files / Small programs /Alphabet.rb: 13: syntax error, unexpected keyword_elsif elsif array.at (i) == 'i' ^

C:/Users/Kelan/// / /Alphabet.rb: 15: , keyword_elsif     elsif array.at(i) == 'o'          ^

C:/Users/Kelan/// / /Alphabet.rb: 17: , keyword_elsif     elsif array.at(i) == 'u'          ^

C:/Users/Kelan/// / /Alphabet.rb: 19: , keyword_else

C:/Users/Kelan/// / /Alphabet.rb: 21: , keyword_end

C:/Users/Kelan/// / /Alphabet.rb: 25: , $end, : + cons.to_s                                ^

[ 0.203 ]

, - , , , , ,

Kelan

+5
5

Ruby ++. += 1 case:

alphabet = 'abcdefghijklmnopqrstuvwxyz'

26.times do |i|
    case alphabet[i]
        when 'a' then vowel += 1
        when 'e' then vowel += 1
        when 'i' then vowel += 1
        when 'o' then vowel += 1
        when 'u' then vowel += 1
        else cons += 1
    end#end case
end#end times

puts 'Vowel: ' + vowel.to_s
puts 'Consonants: ' + cons.to_s

, , count String, :

alphabet = 'abcdefghijklmnopqrstuvwxyz'
vowels = 'aeiou'
vowel_count = alphabet.count vowels
cons_count = alphabet.length - vowel_count
puts "Vowels: #{vowel_count}"
puts "Consonants: #{cons_count}"
+15

, Java/PHP/C. . foo += 1.

, Ruby ?

# use a range to define your alphabet
alphabet = ('a'..'z').entries  #=> ['a', 'b', 'c', ...]

# define vowels as members of an array. it more flexible, which
# is great for things that change (what if you decide to use 'y'?)
vowels = %w{ a e i o u }  #=> ['a', 'e', 'i', 'o', 'u']

# keep counts all together in a hash, which I personally find cleaner
counts = { :vowels => 0, :consonants => 0 }

# even the `for` loops in ruby use the iterators, so you actually
# get better performance out of using the more user-friendly `.each`
alphabet.each do |letter|
  if vowels.include? letter
    counts[:vowels] += 1
  else 
    counts[:consonants] += 1
  end
end

puts "There were #{counts[:vowels]} vowels and #{counts[:consonants]} consonants."
+5

, vowel++ con++, vowel+=1 con+=1.

Ruby pre/post C-.

+3

:

puts("%d vowels & %d consonants" % ('a'..'z').inject([0,0]) do |m, e|
    m[/[aeiou]/.match(e) ? 0:1] += 1; m 
  end)
+2
  • Range.
  • ruby, . for .
  • case . , .
  • ruby ​​ ++ --. += 1.
  • Use the notation "#{ }". It is much better and faster than using +. In fact, you can omit to_sit if you use it.

I would say:

vowel = 0
cons = 0
('a'..'z').each do |c|
  case c
  when 'a', 'e', 'i', 'o', 'u'; vowel += 1
  else                          cons += 1
  end
end

puts "Vowel: #{vowel}"
puts "Consonants: #{cons}"

If I wanted a shorter one, I could go with this:

partition = ('a'..'z').group_by{|c| c =~ /[aeiou]/}
puts "Vowel: #{partition[0].length}"
puts "Consonants: #{partition[nil].length}"
+2
source

All Articles