Ruby - 为什么 for_each 不执行小写请求?收益率/自发行?

发布于 2025-01-03 14:11:42 字数 1002 浏览 1 评论 0原文

其他转换工作正常,我需要做什么才能使 D) 工作正常。最终 D) 将是一个密码,但我想让它最初适用于任何函数的每个字符(小写是一个例子)。分组为 5 个字符桶是我正在开发的密码的一部分。

def keystream_converter(message, conversion)
  case conversion.downcase
    when 'lower_case'
      message.upcase
    when 'upper_case'
      message.downcase
    when 'special'
      message.each_char { |ltr| ltr.downcase }
    else
      'invalid_conversion'
  end
end

initial_src =  "I see Ruby going 100 years!!"
test_string = (initial_src.delete('^a-zA-Z') +"X"*(initial_src.length % 5)).scan(/.{5}/).to_s.upcase
lower = keystream_converter(test_string, 'lower_case')
upper = keystream_converter(test_string, 'upper_case')
special = keystream_converter(test_string, 'special')
#
puts "A) - " + initial_src
puts "B) - " + upper
puts "C) - " + lower
puts "D) - " + special

输出:

A) - I see Ruby going 100 years!!
B) - ["iseer", "ubygo", "ingye", "arsxx"]
C) - ["ISEER", "UBYGO", "INGYE", "ARSXX"]
D) - ["ISEER", "UBYGO", "INGYE", "ARSXX"]

The other converts work, what do I need to do to get D) working ok. Ultimately D) will be a cipher but I want to just get it working for each character for any function (downcase being an example) initially. The grouping into 5 character buckets is part of the cipher code I am developing.

def keystream_converter(message, conversion)
  case conversion.downcase
    when 'lower_case'
      message.upcase
    when 'upper_case'
      message.downcase
    when 'special'
      message.each_char { |ltr| ltr.downcase }
    else
      'invalid_conversion'
  end
end

initial_src =  "I see Ruby going 100 years!!"
test_string = (initial_src.delete('^a-zA-Z') +"X"*(initial_src.length % 5)).scan(/.{5}/).to_s.upcase
lower = keystream_converter(test_string, 'lower_case')
upper = keystream_converter(test_string, 'upper_case')
special = keystream_converter(test_string, 'special')
#
puts "A) - " + initial_src
puts "B) - " + upper
puts "C) - " + lower
puts "D) - " + special

Output:

A) - I see Ruby going 100 years!!
B) - ["iseer", "ubygo", "ingye", "arsxx"]
C) - ["ISEER", "UBYGO", "INGYE", "ARSXX"]
D) - ["ISEER", "UBYGO", "INGYE", "ARSXX"]

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

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

发布评论

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

评论(3

晨曦÷微暖 2025-01-10 14:11:42

String#each_char 生成原始字符串长度为 1 的子字符串。如果您更改这些子字符串(即使使用 downcase! 而不是像您的情况那样的非变异 downcase),原始字符串也不会受到影响。如果你想将 each_char 与块一起使用,你可以这样做:

message.each_char.map(&:downcase).join

String#each_char yields substrings of length 1 of the original string. If you change these substrings (even with downcase! and not a non-mutating downcase like in your case), original string will not be affected. If you want to use each_char with block, you can do something like this:

message.each_char.map(&:downcase).join
七七 2025-01-10 14:11:42

each_char 的调用返回原始输入字符串,而不是块的结果。

例如,如果您在 irb 中执行此操作:

"A STRING".each_char { |ltr| ltr.downcase }
# => "A STRING"

如果您想继续使用 each_char,您应该将每个结果附加到随后可以返回的某个变量,即

# ...
when 'special'
  new_message = ""
  message.each_char { |ltr| new_message += ltr.downcase }
  new_message
else
# ...

Your call to each_char returns the original input string and not the result of the block.

For example, if you did this in irb:

"A STRING".each_char { |ltr| ltr.downcase }
# => "A STRING"

If you want to continue using each_char, you should append each of the results to some variable that you can return afterwards, i.e.,

# ...
when 'special'
  new_message = ""
  message.each_char { |ltr| new_message += ltr.downcase }
  new_message
else
# ...
鸢与 2025-01-10 14:11:42

这很奇怪,但无论如何。您可以将“特殊”情况更改为:

message.each_char.map(&:downcase) * ''

It's strange but anyway. You can change 'special' case to this:

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