我为" elsif"收到了意外的关键字错误

时间:2017-05-09 06:50:49

标签: ruby

def letter_case_count(string)
  char = new Hash
  char[:lower] = 0
  char[:upper] = 0
  char[:neither] = 0
  string.split("").each do |x| 
    if ('A'..'Z').include?(x) 
      char[:upper]++ 
    elsif ('a'..'z').include?(x) 
      char[:lower]++
    else
      char[:neither]++
    end
  end
end

puts letter_case_count('abCdef 123') == { lowercase: 5, uppercase: 1, neither: 4 }
puts letter_case_count('AbCd +Ef') == { lowercase: 3, uppercase: 3, neither: 2 }
puts letter_case_count('123') == { lowercase: 0, uppercase: 0, neither: 3 }
puts letter_case_count('') == { lowercase: 0, uppercase: 0, neither: 0 }

我收到此错误。

(repl):9: syntax error, unexpected keyword_elsif
    elsif ('a'..'z').include?(x) 
         ^
(repl):11: syntax error, unexpected keyword_else
(repl):13: syntax error, unexpected keyword_end
(repl):20: syntax error, unexpected end-of-input, expecting keyword_end
...: 0, uppercase: 0, neither: 0 }
...    

                       ^

有许多意想不到的关键字和意外的输入结束。不知道为什么,我几乎没有用Ruby编程,我也看不出问题所在。

2 个答案:

答案 0 :(得分:1)

Ruby没有pre-incrementpost-increment个运算符。使用+=1,它应该有效。

def letter_case_count(string)
  char = {}
  char[:lower] = 0
  char[:upper] = 0
  char[:neither] = 0
  string.split('').each do |x| 
    if ('A'..'Z').include?(x) 
      char[:upper] += 1 
    elsif ('a'..'z').include?(x) 
      char[:lower] += 1
    else
      char[:neither] += 1
    end
  end
  char #returning the char is also important.
end

使用不同的键进行比较也会返回错误的结果,应该是..

puts letter_case_count('abCdef 123') == { lower: 5, upper: 1, neither: 4 }
puts letter_case_count('AbCd +Ef') == { lower: 3, upper: 3, neither: 2 }
puts letter_case_count('123') == { lower: 0, upper: 0, neither: 3 }
puts letter_case_count('') == { lower: 0, upper: 0, neither: 0 }

更好的方法:

def letter_case_count(string)
  {
    lower: string.scan(/[a-z]/).count,
    upper: string.scan(/[A-Z]/).count,
    neither: string.scan(/[^a-z]/i).count
  }
end

答案 1 :(得分:0)

您的代码中存在大量语法错误。

def letter_case_count(string)
  char = Hash.new # not new Hash
  char[:lower] = 0
  char[:upper] = 0
  char[:neither] = 0
  string.split("").each do |x| 
    if ('A'..'Z').include?(x) 
      char[:upper]+=1 # var++ is not valid ruby code
    elsif ('a'..'z').include?(x) 
      char[:lower]+=1 #same here
    else
      char[:neither]+=1 # same here
    end
  end
end

puts letter_case_count('abCdef 123') == { lowercase: 5, uppercase: 1, neither: 4 }
puts letter_case_count('AbCd +Ef') == { lowercase: 3, uppercase: 3, neither: 2 }
puts letter_case_count('123') == { lowercase: 0, uppercase: 0, neither: 3 }
puts letter_case_count('') == { lowercase: 0, uppercase: 0, neither: 0 }

<强>更新

对于这类任务,请使用Ruby的stdlib中的minitest。

所有文件示例(所有这些都将失败)

require 'minitest/autorun'

class String
  def letter_case_count
    char = Hash.new # not new Hash
    char[:lower] = 0
    char[:upper] = 0
    char[:neither] = 0
    self.split("").each do |x| 
      if ('A'..'Z').include?(x) 
        char[:upper]+=1 # var++ is not valid ruby code
      elsif ('a'..'z').include?(x) 
        char[:lower]+=1 #same here
      else
        char[:neither]+=1 # same here
      end
    end
    return char
  end
end

class TestFoo < MiniTest::Test

  def setup
    @w1, @w2, @w3, @w4 = ["abCdef 123", "AbCd +Ef", "123", ""].map {|e| String.new(e)}
  end

  def test_some
    assert_equal @w1.letter_case_count, { lowercase: 3, uppercase: 1, neither: 4 }
    end

  def test_some_other
    assert_equal @w2.letter_case_count, { lowercase: 3, uppercase: 3, neither: 2 }
  end
  def test_other
    assert_equal @w3.letter_case_count, { lowercase: 0, uppercase: 0, neither: 3 }
  end

  def test_definitely_other
    assert_equal @w4.letter_case_count, { lowercase: 0, uppercase: 0, neither: 0 }
  end
end