在简单的if语句中使用正则表达式吗?

问题描述 投票:0回答:1

代替执行:

puts "what type of input?"
input = gets.chomp
if %W[Int INT i I Ints ints].include?(input) 
puts "enter int"
i = gets.to_i

我想使用正则表达式解释字符串用户输入。例如,>

puts "are you entering in a string, an int or a float?"
case gets
when /\A(string|s)\z/i
puts "enter in a string"
gets.chomp
when /\A(int|i)\z/i
puts "enter an int"
gets.to_i
when /\A(float|f)\z/i
puts "enter a float"
gets.to_f
end

为了获得相同的结果但使用if语句而不是case语句的语法是什么?

而不是:输入“什么类型的输入?”如果%W [Int INT i I Ints ints] .include?(input)放入“ enter int”,则输入= gets.chomp i = gets.to_i我想使用正则表达式解释字符串用户输入。对于...

ruby
1个回答
0
投票

gets返回字符串with

© www.soinside.com 2019 - 2024. All rights reserved.