我怎么能在红宝石中包裹两个命令?

时间:2017-04-22 20:53:02

标签: ruby

我试图将这两个命令包装在ruby中但不能正常工作

ruby -a -ne 'print $F[0].gsub(/=(.*?)&/," \"\\1\"  and ")' prueban > prueban2

ruby -a   -F';'  -ne    'puts $F[0].sub("less"," <")' prueban2  > prueban3

这是我的命令

File.open("text.txt", "r") do  |fi|
  fi.readlines.each do |line|
    parts = line.chomp.split(';')

     fx= puts parts[0].gsub(/=(.*?)&/," \"\\1\"  and ")



    end
   fx.readlines.each do |line|
    parts = line.chomp.split(';')

     fx= puts parts[0].gsub("less"," <")
end
end

这是我的档案

pricegreater=2&priceless=4&seleccionequal=pet&

这是我的预期输出

pricegreater "2"  and price < "4"  and seleccionequal "pet"  and 

我不知道什么是错的请帮帮我

2 个答案:

答案 0 :(得分:2)

这是核心功能的重写版本,以更加Ruby风格的方式展示:

# Define a lookup table of all substitutions
REWRITE = {
  'greater' => '>',
  'less' => '<',
  'equal' => '='
}

# Use the lookup table to create a regular expression that matches them all
REWRITE_RX = Regexp.new(Regexp.union(REWRITE.keys).to_s + '\z')

def rewrite(input)
  # Split up each main part of the input on &
  input.split('&').map do |pair|
    # Carve up each part into a var and value on =
    var, value = pair.split('=')

    # Replace terms found in the lookup table
    var.sub!(REWRITE_RX) do |m|
      ' ' + REWRITE[m]
    end

    # Combine these to get the result
    [ var, value ].join(' ')
  end.join(' and ')
end

付诸行动,你得到了这个:

rewrite("pricegreater=2&priceless=4&seleccionequal=pet&")
# => "price > 2 and price < 4 and seleccion = pet"

答案 1 :(得分:0)

我用这个解决了

    public void Fail()
    {
        var faultyService = ServiceProxy.Create<IException>(
            new Uri("fabric:/ExceptionApplication/ExceptionService"));

        try
        {
            faultyService.ThrowException().GetAwaiter().GetResult();
        }
        catch (CustomException cex)
        {
            // Will never be hit                
        }
        catch (AggregateException aex)
        {
            aex.Handle(ex =>
            {
                var cex = ex as CustomException;
                if (cex != null)
                {
                    // Will be hit by your code
                    return true;
                }
                return false;
            });
        }
        catch (Exception ex)
        {
            // Will never be hit
        }
    }