如何在 ruby​​ 中加密字符串

发布于 2025-01-19 07:27:49 字数 758 浏览 3 评论 0原文

下面的方法应该采用“加密”字符串并返回“未加密”字符串,但它返回错误“undefined method `-' for nil (NoMethodError)” 我已经尝试了一切,但我无法超越。 基本上代码是这样的:

ALPHABET = ('a'..'z').to_a
def decode(string, factor)
    string = string.split(//)
    string = string.each_with_index.map do |char, _|
      if char.match(/\w/).nil?
        char
      else
        ALPHABET[ALPHABET.index(char) - factor]
      end
    end
    string.join
  end


  • string is 'Zd tytxtrzd cpnflclx op Vlcwdcfsp, xld lelnlclx l ntolop gtktysl!'。
  • 因数是11。
  • 解码后的消息必须是“Os inimigos reuaram de Karlsruhe, mas atacaram a cidade vizinha!”


  • 但返回

    <块引用>

    nil:NilClass 的未定义方法“-”


    如何解决这个问题?

  • The method below should take an "encrypted" string and return the string "unencrypted", but it is returning the error "undefined method `-' for nil (NoMethodError)"
    I've tried everything but I can't get beyond that.
    basically the code is this:

    ALPHABET = ('a'..'z').to_a
    def decode(string, factor)
        string = string.split(//)
        string = string.each_with_index.map do |char, _|
          if char.match(/\w/).nil?
            char
          else
            ALPHABET[ALPHABET.index(char) - factor]
          end
        end
        string.join
      end
    

  • string is 'Zd tytxtrzd cpnflclx op Vlcwdcfsp, xld lelnlclx l ntolop gtktysl!'.

  • factor is 11.

  • decoded message must be 'Os inimigos recuaram de Karlsruhe, mas atacaram a cidade vizinha!'

  • But returns

    undefined method `-' for nil:NilClass

    How can I solve this?

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

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

    发布评论

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

    评论(2

    梦屿孤独相伴 2025-01-26 07:27:49

    您仅使用小写字母设置 ALPHABET (ALPHABET = ('a'..'z').to_a),但在字符串中,您使用了一些大写字母(“Z”、“V”)。由于这些字母不会出现在 ALPHABET 上,因此与它们相关的索引不存在并且将为 nil(例如 ALPHABET.index('Z'))。

    为了解决这个问题,考虑到输出中的字母大小写不相关,您可以对字符串进行预处理,将所有字母转换为小写,然后您将得到预期的结果:

    ALPHABET = ('a'..'z').to_a
    def decode(string, factor)
      string = string.downcase.split(//) # converting into downcase here
      string = string.each_with_index.map do |char, _|
        if char.match(/\w/).nil?
          char
        else
          ALPHABET[ALPHABET.index(char) - factor]
        end
      end
      string.join
    end
    
    string = "'Zd tytxtrzd cpnflclx op Vlcwdcfsp, xld lelnlclx l ntolop gtktysl!'"
    factor = 11
    puts decode(string, factor)
      # => 'os inimigos recuaram de karlsruhe, mas atacaram a cidade vizinha!'
    
    

    UPDATE

    在这种情况下,您可以定义两个字母表(一个用于小写,一个用于大写),然后相应地使用它们。像这样:

    LOWCASE_ALPHABET = ('a'..'z').to_a
    UPCASE_ALPHABET = ('A'..'Z').to_a
    
    def decode(string, factor)
      string = string.split(//) # converting into downcase here
      string = string.each_with_index.map do |char, _|
        if char.match(/\w/).nil?
          char
        else
          # if is a lower case letter, uses lowcase alphabet and, otherwise, uses upcase alphabet
          alphabet = /[a-z]/.match(char) ? LOWCASE_ALPHABET : UPCASE_ALPHABET
          alphabet[alphabet.index(char) - factor]
        end
      end
      string.join
    end
    
    string = "'Zd tytxtrzd cpnflclx op Vlcwdcfsp, xld lelnlclx l ntolop gtktysl!'"
    factor = 11
    puts decode(string, factor)
      # => 'Os inimigos recuaram de Karlsruhe, mas atacaram a cidade vizinha!'
    

    You are setting your ALPHABET only with lower case letters (ALPHABET = ('a'..'z').to_a), but in the string, you are using some upper case letters ('Z', 'V'). As these letters won't be on the ALPHABET, the index related to them does not exist and will be nil (e.g ALPHABET.index('Z') is nil).

    In order to solve that, considering that the letters case on the output is not relevant, you can just do a preprocessing on your string converting all the letters to lower case, and you will then get the expected result:

    ALPHABET = ('a'..'z').to_a
    def decode(string, factor)
      string = string.downcase.split(//) # converting into downcase here
      string = string.each_with_index.map do |char, _|
        if char.match(/\w/).nil?
          char
        else
          ALPHABET[ALPHABET.index(char) - factor]
        end
      end
      string.join
    end
    
    string = "'Zd tytxtrzd cpnflclx op Vlcwdcfsp, xld lelnlclx l ntolop gtktysl!'"
    factor = 11
    puts decode(string, factor)
      # => 'os inimigos recuaram de karlsruhe, mas atacaram a cidade vizinha!'
    
    

    UPDATE considering that the case is relevant:

    In this case, you can then define two alphabets (one for lower case and one for upper case), and then use them accordingly. Like this:

    LOWCASE_ALPHABET = ('a'..'z').to_a
    UPCASE_ALPHABET = ('A'..'Z').to_a
    
    def decode(string, factor)
      string = string.split(//) # converting into downcase here
      string = string.each_with_index.map do |char, _|
        if char.match(/\w/).nil?
          char
        else
          # if is a lower case letter, uses lowcase alphabet and, otherwise, uses upcase alphabet
          alphabet = /[a-z]/.match(char) ? LOWCASE_ALPHABET : UPCASE_ALPHABET
          alphabet[alphabet.index(char) - factor]
        end
      end
      string.join
    end
    
    string = "'Zd tytxtrzd cpnflclx op Vlcwdcfsp, xld lelnlclx l ntolop gtktysl!'"
    factor = 11
    puts decode(string, factor)
      # => 'Os inimigos recuaram de Karlsruhe, mas atacaram a cidade vizinha!'
    
    萌无敌 2025-01-26 07:27:49

    另一种只有一个字母的版本:

    ALPHABET = ('a'..'z').to_a
    def decode(string, factor)
      string.each_char.map do |char|
        index = ALPHABET.index(char.downcase)
        decoded = index.nil? ? char : ALPHABET[index - factor]
        char == char.downcase ? decoded : decoded.upcase
      end.join
    end
    

    代码的优先级是可读性/简洁性而不是速度。

    Another version with one alphabet:

    ALPHABET = ('a'..'z').to_a
    def decode(string, factor)
      string.each_char.map do |char|
        index = ALPHABET.index(char.downcase)
        decoded = index.nil? ? char : ALPHABET[index - factor]
        char == char.downcase ? decoded : decoded.upcase
      end.join
    end
    

    Priority of the code is readability/brevity over speed.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文