Ruby 不识别“^=”操作员

发布于 2024-12-22 04:23:42 字数 509 浏览 0 评论 0原文

我有 ruby​​ 1.9.2dev (2010-07-02) [i486-linux]。

我知道 ^= 是一个 ixor 运算符,如官方文档所示。

这是我从 shell 运行脚本时的输出。

root@desk:~/Desktop/cha03# ./cha03
./cha03:35:in `block in <main>': undefined method `^' for "\x1C":String (NoMethodError)
    from ./cha03:35:in `each_byte'
    from ./cha03:35:in `each_with_index' 
    from ./cha03:35:in `<main>'

这是产生错误的行:

key.each_byte.each_with_index { |b,i| key[i] ^= subkey[i % 4] }

I have ruby 1.9.2dev (2010-07-02) [i486-linux].

I know that the ^= is a ixor operator as shown in the official documentation.

This is the output when I run the script from the shell.

root@desk:~/Desktop/cha03# ./cha03
./cha03:35:in `block in <main>': undefined method `^' for "\x1C":String (NoMethodError)
    from ./cha03:35:in `each_byte'
    from ./cha03:35:in `each_with_index' 
    from ./cha03:35:in `<main>'

And this is the line that makes the error:

key.each_byte.each_with_index { |b,i| key[i] ^= subkey[i % 4] }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-12-29 04:23:42

在 ruby​​ 1.9 string[some_index] 不再是字节值,它将是一个字符串

ruby-1.9.2-p180 :001 > '123'[2] => "3" 

String 没有 ^ 方法,所以你会得到你所描述的错误。在 ruby​​ 1.8.7 中,您将获得字节的值,并且一切都会起作用。我认为使用 unpack 将字符串转换为字节数组,对其进行操作然后将其全部放回一起会更幸运,类似于

bytes = key.unpack('C*')
bytes.each_with_index {|b,i| ... }
key = bytes.pack('C*')

In ruby 1.9 string[some_index] is no longer the byte value, it will instead be a string

ruby-1.9.2-p180 :001 > '123'[2] => "3" 

String doesnt have a ^ method, so you get the error you've described. In ruby 1.8.7 you would have got the byte's value instead and it would all have worked. I think you'll have better luck using unpack to turn the string into an array of bytes, manipulate that then put it all back together, something along the lines of

bytes = key.unpack('C*')
bytes.each_with_index {|b,i| ... }
key = bytes.pack('C*')
且行且努力 2024-12-29 04:23:42

扩展类 String 以便能够轻松地与另一个字符串或数字进行异或运算
使用示例:

#irb -E binary
ruby-1.9.3-p0 :021 > "foo".xor('bar')
 => "\x04\x0E\x1D" 
ruby-1.9.3-p0 :022 > "foo".xor(1)
 => "gnn"

class String
  def xor x
    if x.is_a?(String)
      r = ''
      j = 0
      0.upto(self.size-1) do |i|
        r << (self[i].ord^x[j].ord).chr
        j+=1
        j=0 if j>= x.size
      end
      r
    else
      r = ''
      0.upto(self.size-1) do |i|
        r << (self[i].ord^x).chr
      end
      r
    end
  end
end

extending class String to be able to easy xor-ed with another string or number
example use:

#irb -E binary
ruby-1.9.3-p0 :021 > "foo".xor('bar')
 => "\x04\x0E\x1D" 
ruby-1.9.3-p0 :022 > "foo".xor(1)
 => "gnn"

class String
  def xor x
    if x.is_a?(String)
      r = ''
      j = 0
      0.upto(self.size-1) do |i|
        r << (self[i].ord^x[j].ord).chr
        j+=1
        j=0 if j>= x.size
      end
      r
    else
      r = ''
      0.upto(self.size-1) do |i|
        r << (self[i].ord^x).chr
      end
      r
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文