代码之家  ›  专栏  ›  技术社区  ›  BvuRVKyUVlViVIc7

二进制或ruby中的“|”

  •  0
  • BvuRVKyUVlViVIc7  · 技术社区  · 16 年前

    为什么这行不通:

    >> s = "hi"                                                             
    => "hi"                                                                 
    >> s == ("hi"|"ho")                                                     
    NoMethodError: undefined method `|' for "hi":String                     
     from (irb):2                                                           
    >>
    

    我不明白。这种语法有解决方案吗?因为

    s == ("hi"|"ho") 
    #is shorther than 
    s == "hi" || s == "ho"
    
    7 回复  |  直到 16 年前
        1
  •  9
  •   Aaron Fi    16 年前

    是的,String类中没有定义位运算符|: http://ruby-doc.org/core/classes/String.html

    考虑一下表现力:

    ["hi", "ho"].include? myStr
    
    irb(main):001:0> s = "hi"
    => "hi"
    irb(main):002:0> ["hi", "ho"]
    => ["hi", "ho"]
    irb(main):003:0>  ["hi", "ho"].include? s
    => true
    irb(main):004:0> s = "foo"
    => "foo"
    irb(main):005:0>  ["hi", "ho"].include? s
    => false
    
        2
  •  5
  •   AlbertoPL    16 年前

    s==“hi”||s==”ho“

    请注意,|是位或,而||是正则或

        3
  •  3
  •   Shadwell    16 年前

    你可以用 include? 数组上的方法(如果有多个) ==

    ["hi", "ho"].include?(s)
    

        4
  •  1
  •   muusbolla    16 年前

    据我所知,这种语法在任何语言中都不存在。

    你所说的

    s == ("hi"|"ho")
    

        5
  •  1
  •   rampion    16 年前

    你可以这样做:

    irb> class Pair
           def initialize(strA,strB)
             @strA,@strB = strA,strB
           end
           def ==(string)
             string == @strA || string == @strB
           end
           def |(other)
             Pair.new(self,other)
           end
         end
    #=> nil
    irb> class String
           def |(other)
             Pair.new(self,other)
           end
           alias old_equals :==
           def ==(other)
             if other.kind_of? Pair
               other == self
             else
               old_equals other
             end
           end
         end
    #=> nil
    irb> ("one"|"two") == "one"
    #=> true
    irb> ("one"|"two") == "two"
    #=> true
    irb> ("one"|"two") == "three"
    #=> false
    irb> "one" == ("one"|"two")
    #=> true
    irb> "three" == ("one"|"two"|"three")
    #=> true
    

        6
  •  1
  •   Jesse Millikan    16 年前

    Ruby支持二进制“or”和 other binary operations 在Fixnum和Bignum类型的值上,表示任何整数。据我所知,字符串或任何其他类型都不支持位操作。

    正如其他人提到的,你可能想要一些二进制操作之外的东西。但是,您可以很容易地获得字符的整数表示,因此您可以这样比较字符:

    a = "Cake"
    b = "Pie"
    puts a[0] | b[0] # Prints "83" - C is 67 and P is 80.
    

    通过一些转换,您可以轻松地获得一个比较数组。

    a = "Cake"
    b = "Pie " # Strings of uneven length is trivial but more cluttered.
    
    a_arr = a.split(//)
    b_arr = b.split(//)
    c_arr = []
    
    a.each_with_index { |char, i| c.push(a[i].to_i | b[i].to_i) }
    # If you *really* want an ASCII string back...
    c = c_arr.collect(&:chr).join
    
        7
  •  0
  •   John Schank III    16 年前

    你可以使用正则表达式:

    像这样:

    regex = /hi|ho/
    s = "hi"
    t = "foo"
    
    s =~ regex
    #=> 0
    
    t =~ regex
    #=> nil
    
    推荐文章