Ruby - 为什么 for_each 不执行小写请求?收益率/自发行?
其他转换工作正常,我需要做什么才能使 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
String#each_char 生成原始字符串长度为 1 的子字符串。如果您更改这些子字符串(即使使用
downcase!
而不是像您的情况那样的非变异downcase
),原始字符串也不会受到影响。如果你想将each_char
与块一起使用,你可以这样做:String#each_char yields substrings of length 1 of the original string. If you change these substrings (even with
downcase!
and not a non-mutatingdowncase
like in your case), original string will not be affected. If you want to useeach_char
with block, you can do something like this:对
each_char
的调用返回原始输入字符串,而不是块的结果。例如,如果您在 irb 中执行此操作:
如果您想继续使用
each_char
,您应该将每个结果附加到随后可以返回的某个变量,即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:
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.,这很奇怪,但无论如何。您可以将“特殊”情况更改为:
It's strange but anyway. You can change 'special' case to this: