Ruby Proc返回数字和数字字符串乘以2作为未受骗的类型和文本strng只是返回

问题描述:

我想做我的作业,我想问你是否可以帮助我写一个Ruby Proc返回数字(Integer ,Float ...)和数字字符串(“22”)乘以2和文本字符串(“sos”)只是返回不变。我试图用一元运算符做到这一点,但我不使用Float。Ruby Proc返回数字和数字字符串乘以2作为未受骗的类型和文本strng只是返回

def make_double 
    proc = Proc.new { |x| (x.is_a? Integer or x !~ /\D/) ? ((x.to_i)*2).to_s : x } 
end 

有没有可能如何在proc中编写更复杂的条件?谢谢

+0

对不起,我有点失去了,究竟是什么你期望这个来做。也许有几个示例输入/输出可能有帮助?另外,你是否有理由把它压缩到一行?我认为问题肯定在你的x!〜/ \ D /中。 '.to_i'也需要从它的声音中成为'.to_f'。我认为你应该检查出http://rubular.com/的正则表达式,但可能像'\ d +(?:\。\ d +)?'? – user3334690

+0

我只需要这个例如 –

+0

p = make_double ;;; p.call(42) - > 84 ;;; p.call(42.0) - > 84.0 ;;; p.call(“42”) - >“84.0”;;; p.call(“Universe”) - >“Universe”;;; 并且必须使用Proc –

易读

puts '-- do block --' 

def make_double 
    Proc.new do | x | 
     case x 
     when Integer, Float then x * 2 
     when String 
      if x =~ /\d/ 
      then (x.to_i * 2).to_s 
      else x 
      end 
     else 
      "Invalid class #{x.class.name} for x." 
     end 
    end 
end 

puts "42 -> #{make_double.call(42)}  #{make_double.call(42).class}" 
puts "42.0 -> #{make_double.call(42.0)} #{make_double.call(42.0).class}" 
puts "'42' -> #{make_double.call('42')}  #{make_double.call('42').class}" 
puts "'xyz' -> #{make_double.call('xyz')} #{make_double.call('xyz').class}" 
puts "4..8 -> #{make_double.call(4..8)}" 

执行:

$ ruby -w t_a.rb 
-- do block -- 
42 -> 84  Integer 
42.0 -> 84.0 Float 
'42' -> 84  String 
'xyz' -> xyz String 
4..8 -> Invalid class Range for x. 

难以阅读

puts '-- brace block --' 

def make_double 
    Proc.new { | x | x.is_a?(Integer) || x.is_a?(Float) ? x * 2 : \ 
     x.is_a?(String) ? x =~ /\d/ ? (x.to_i * 2).to_s : x : 'invalid' } 
end 

puts "42 -> #{make_double.call(42)}  #{make_double.call(42).class}" 
puts "42.0 -> #{make_double.call(42.0)} #{make_double.call(42.0).class}" 
puts "'42' -> #{make_double.call('42')}  #{make_double.call('42').class}" 
puts "'xyz' -> #{make_double.call('xyz')} #{make_double.call('xyz').class}" 
puts "4..8 -> #{make_double.call(4..8)}" 

执行:

$ ruby -w t.rb 
-- brace block -- 
42 -> 84  Integer 
42.0 -> 84.0 Float 
'42' -> 84  String 
'xyz' -> xyz String 
4..8 -> invalid 
+0

是的,这工作正常,谢谢! –

+0

'make_double.call(“42.5”)怎么样'不确定是否这种情况。我想你也可以节省一些时间,把所有事情都做成一个字符串。就像:'(x.to_s =〜/ \d+(?:\.\d+)?/)? x.to_f * 2:x' – user3334690

+1

@ user3334690对!由于OP没有问,我没有测试过,我的'如果x =〜/ \ d /'不够。添加了更完整的答案。 – BernardK

这个答案考虑从@ user3334690的评论,并纠正错误的结果

puts "'42xyz' -> #{make_double.call('42xyz')} #{make_double.call('42xyz').class}" 
puts "'x42y' -> #{make_double.call('x42y')}  #{make_double.call('x42y').class}" 
puts "'42.5' -> #{make_double.call('42.5')} #{make_double.call('42.5').class}" 

这是

'42xyz' -> 84 String 
'x42y' -> 0  String 
'42.5' -> 84 String 

一个班轮

puts '-- brace block 2 --' 

def make_double 
    Proc.new { | x | (x.to_s =~ /\d+(?:\.\d+)?/) ? x.to_f * 2 : x rescue puts "Error" } 
end 

puts "42 -> #{make_double.call(42)}   #{make_double.call(42).class}" 
puts "42.0 -> #{make_double.call(42.0)}   #{make_double.call(42.0).class}" 
puts "'42' -> #{make_double.call('42')}   #{make_double.call('42').class}" 
puts "'42xyz' -> #{make_double.call('42xyz')}  #{make_double.call('42xyz').class}" 
puts "'x42y' -> #{make_double.call('x42y')}  #{make_double.call('x42y').class}" 
puts "'42.5' -> #{make_double.call('42.5')}  #{make_double.call('42.5').class}" 
puts "'42.5xyz' -> #{make_double.call('42.5xyz')} #{make_double.call('42.5xyz').class}" 
puts "'xyz' -> #{make_double.call('xyz')}   #{make_double.call('xyz').class}" 
puts "4..8 -> #{make_double.call(4..8)}" 

执行:

$ ruby -w t_short2.rb 
-- brace block 2 -- 
42 -> 84.0   Float 
42.0 -> 84.0   Float 
'42' -> 84.0   Float 
'42xyz' -> 84.0  Float 
'x42y' -> 0.0  Float 
'42.5' -> 85.0  Float 
'42.5xyz' -> 85.0 Float 
'xyz' -> xyz   String 
Error 
4..8 -> 

请注意,包含数字的所有数字和字符串均转换为浮点数,'x42y'转换为0.0。

保持原班

字符串混合数字和字母不会转换(使用锚^和$的正则表达式)。

puts '-- do block 2 --' 

def make_double 
    Proc.new do | x | 
     case x 
     when Integer, Float then x * 2 
     when String 
      case 
      when x =~ /^\d+\.\d+$/ then (x.to_f * 2).to_s 
      when x =~ /^\d+$/  then (x.to_i * 2).to_s 
      else x 
      end 
     else 
      "Invalid class #{x.class.name} for x." 
     end 
    end 
end 

puts "42 -> #{make_double.call(42)}    #{make_double.call(42).class}" 
puts "42.0 -> #{make_double.call(42.0)}   #{make_double.call(42.0).class}" 
puts "'42' -> #{make_double.call('42')}   #{make_double.call('42').class}" 
puts "'42xyz' -> #{make_double.call('42xyz')}  #{make_double.call('42xyz').class}" 
puts "'x42y' -> #{make_double.call('x42y')}  #{make_double.call('x42y').class}" 
puts "'42.5' -> #{make_double.call('42.5')}  #{make_double.call('42.5').class}" 
puts "'42.5xyz' -> #{make_double.call('42.5xyz')} #{make_double.call('42.5xyz').class}" 
puts "'xyz' -> #{make_double.call('xyz')}   #{make_double.call('xyz').class}" 
puts "4..8 -> #{make_double.call(4..8)}" 

执行:

$ ruby -w t_b.rb 
-- do block 2 -- 
42 -> 84    Fixnum 
42.0 -> 84.0   Float 
'42' -> 84   String 
'42xyz' -> 42xyz  String 
'x42y' -> x42y  String 
'42.5' -> 85.0  String 
'42.5xyz' -> 42.5xyz String 
'xyz' -> xyz   String 
4..8 -> Invalid class Range for x.