如何检查输入字符串?

时间:2022-12-12 07:11:27

Here is my code:

这是我的代码:

loop do
  print "Input word: "
  word = gets.chomp
  if word.nil? or word.empty?
    puts "Nothing to input."
  else
    if word.index(":") != -1
      puts "Illegal character ':'"
    else
      break
    end
  end
end

Is there a more elegant way to check the input string?

是否有更优雅的方法来检查输入字符串?

4 个解决方案

#1


7  

Something like this?

是这样的吗?

loop do
  print "Input word: "
  word = gets.chomp

  if word.empty?
    puts "No input."
  elsif word.include?(":")
    puts "Illegal character ':'"
  else
    break
  end
end

#2


2  

This separates the complex logic from the IO

这将复杂的逻辑与IO分离开来

def validation_message_for_word(word)
  case
  when (word.nil? or word.empty?) then "Nothing to input."
  when word.include?(":") then 'Illegal character ":".'
  else nil
  end
end

word = nil # To ensure word doesn't get thrown away after the loop
loop do
  print "Input word: "
  word = gets.chomp
  validation_message = validation_message_for_word(word)
  break if validation_message.nil?
  puts validation_message
end

Now, if you want to unit test it, you can give a variety of different strings to validation_message_for_word and test the return value.

现在,如果您想对它进行单元测试,您可以为validation_message_for_word提供各种不同的字符串,并测试返回值。

If you needed internationalization, you could do

如果需要国际化,可以这样做

def validation_type_for_word(word)
  case
  when (word.nil? or word.empty?) then :no_input
  when word.include?(":") then :illegal_character
  else nil
  end
end

def validation_message(language, validation_type)
  {:en => {:no_input => "No input", :illegal_character => 'Illegal character ":"'},
   :lolcat => {:no_input => "Invizibl input", :illegal_character => 'Character ":" DO NOT LIEK'}}.fetch(language).fetch(validation_type)
end

word = nil # To ensure word doesn't get thrown away after the loop
loop do
  print "Input word: "
  word = gets.chomp
  validation_type = validation_type_for_word(word)
  break if validation_type.nil?
  puts validation_message(:lolcat, validation_type)
end

#3


0  

I personally prefer avoiding nested if/else:

我个人更喜欢避免嵌套的if/else:

loop do
    print "Input word: "
    word = gets.chomp
    if word.nil? or word.empty?
        puts "Nothing to input."
        #break, exit, nothing, or whatever ....
    end
    if word.index(":") != 0
        puts "Illegal character ':'"
    else
        break
    end
end

#4


0  

Depends what "elegant" means to you, but as a fan of refactoring conditional statements to easily readable extracted methods, I'd look at something like:

这取决于“优雅”对您来说意味着什么,但是作为重构条件语句的爱好者,您可以轻松地阅读提取的方法,我将看到如下内容:

def has_valid_input(word)
  return true unless word.include?(":")
  puts "Illegal." 
  return false
end

def is_not_empty(word)
  return true unless word.empty?
  puts "empty"
  return false
end

loop do
  print "Input word: "
  word = gets.chomp
  break if is_not_empty(word) && has_valid_input(word)
end

#1


7  

Something like this?

是这样的吗?

loop do
  print "Input word: "
  word = gets.chomp

  if word.empty?
    puts "No input."
  elsif word.include?(":")
    puts "Illegal character ':'"
  else
    break
  end
end

#2


2  

This separates the complex logic from the IO

这将复杂的逻辑与IO分离开来

def validation_message_for_word(word)
  case
  when (word.nil? or word.empty?) then "Nothing to input."
  when word.include?(":") then 'Illegal character ":".'
  else nil
  end
end

word = nil # To ensure word doesn't get thrown away after the loop
loop do
  print "Input word: "
  word = gets.chomp
  validation_message = validation_message_for_word(word)
  break if validation_message.nil?
  puts validation_message
end

Now, if you want to unit test it, you can give a variety of different strings to validation_message_for_word and test the return value.

现在,如果您想对它进行单元测试,您可以为validation_message_for_word提供各种不同的字符串,并测试返回值。

If you needed internationalization, you could do

如果需要国际化,可以这样做

def validation_type_for_word(word)
  case
  when (word.nil? or word.empty?) then :no_input
  when word.include?(":") then :illegal_character
  else nil
  end
end

def validation_message(language, validation_type)
  {:en => {:no_input => "No input", :illegal_character => 'Illegal character ":"'},
   :lolcat => {:no_input => "Invizibl input", :illegal_character => 'Character ":" DO NOT LIEK'}}.fetch(language).fetch(validation_type)
end

word = nil # To ensure word doesn't get thrown away after the loop
loop do
  print "Input word: "
  word = gets.chomp
  validation_type = validation_type_for_word(word)
  break if validation_type.nil?
  puts validation_message(:lolcat, validation_type)
end

#3


0  

I personally prefer avoiding nested if/else:

我个人更喜欢避免嵌套的if/else:

loop do
    print "Input word: "
    word = gets.chomp
    if word.nil? or word.empty?
        puts "Nothing to input."
        #break, exit, nothing, or whatever ....
    end
    if word.index(":") != 0
        puts "Illegal character ':'"
    else
        break
    end
end

#4


0  

Depends what "elegant" means to you, but as a fan of refactoring conditional statements to easily readable extracted methods, I'd look at something like:

这取决于“优雅”对您来说意味着什么,但是作为重构条件语句的爱好者,您可以轻松地阅读提取的方法,我将看到如下内容:

def has_valid_input(word)
  return true unless word.include?(":")
  puts "Illegal." 
  return false
end

def is_not_empty(word)
  return true unless word.empty?
  puts "empty"
  return false
end

loop do
  print "Input word: "
  word = gets.chomp
  break if is_not_empty(word) && has_valid_input(word)
end