I'm trying to write a method that capitalizes the first letter of each word in a string.
我正在尝试编写一个方法,将字符串中每个单词的第一个字母大写。
def capitalize(string)
arr1 = string.split(" ")
arr2 = []
arr1.each do |i|
arr2 << i.split(//)
end
arr2.each do |i|
i[0].upcase!
i = i.join
end
arr2.join(" ")
end
In my second each
statement, the first letter of each word is successfully capitalized and I get something like this:
在我的第二个声明中,每个单词的第一个字母成功大写,我得到这样的结果:
[["A", "a", "a"], ["A", "a", "a", "a"]]
I want to join each word together, but i = i.join
does nothing and i.join!
throws an error, undefined method. I tested ["A", "a", "a"].join
, and this produces the desired result. I'm confused as to why this isn't working in my method, especially when upcase
works and this doesn't.
我想把每个单词加在一起,但是i = i.join什么也没做,我就加入!抛出一个错误,未定义的方法。我测试了[“A”,“a”,“a”]。join,这产生了期望的结果。我很困惑为什么这在我的方法中不起作用,特别是当upcase工作时,这没有。
Can you explain how my thinking is wrong in this case and how to fix it?
你能解释一下我的想法在这种情况下是怎么回事以及如何解决吗?
2 个解决方案
#1
3
The key here is you're trying to modify i
while inside an each
block, but as i
is a local variable, it has no effect on the original.
这里的关键是你试图在每个块内部修改i,但由于我是一个局部变量,它对原始变量没有影响。
What you want is:
你想要的是:
arr2.collect! do |i|
i[0].upcase!
i.join
end
This rewrites the contents of the array.
这会重写数组的内容。
What you can do, though, is roll this up into a simple gsub
:
但是,你可以做的是把它变成一个简单的gsub:
def capitalize(string)
string.gsub(/\b([a-z])\B/) do |m|
$1.upcase
end
end
#2
0
I believe in this code here:
我相信这里的代码:
arr1.each do |i|
arr2 << i.split(//)
end
You are splitting each character of each word from the first array. This is essentially going to give you an array of array's. If you put this block in after your second loop, you can see the structure:
您正在从第一个数组中分割每个单词的每个字符。这基本上会给你一个数组的数组。如果在第二次循环后放入此块,则可以看到结构:
arr2.each do |i|
puts("[")
i.each do |x|
puts(x)
end
puts("]")
end
For a solution, can do something like so:
对于解决方案,可以这样做:
def self.capitalizeB(string)
return string.split(" ").each { |i| i[0] = i[0].upcase }.join(" ")
end
#1
3
The key here is you're trying to modify i
while inside an each
block, but as i
is a local variable, it has no effect on the original.
这里的关键是你试图在每个块内部修改i,但由于我是一个局部变量,它对原始变量没有影响。
What you want is:
你想要的是:
arr2.collect! do |i|
i[0].upcase!
i.join
end
This rewrites the contents of the array.
这会重写数组的内容。
What you can do, though, is roll this up into a simple gsub
:
但是,你可以做的是把它变成一个简单的gsub:
def capitalize(string)
string.gsub(/\b([a-z])\B/) do |m|
$1.upcase
end
end
#2
0
I believe in this code here:
我相信这里的代码:
arr1.each do |i|
arr2 << i.split(//)
end
You are splitting each character of each word from the first array. This is essentially going to give you an array of array's. If you put this block in after your second loop, you can see the structure:
您正在从第一个数组中分割每个单词的每个字符。这基本上会给你一个数组的数组。如果在第二次循环后放入此块,则可以看到结构:
arr2.each do |i|
puts("[")
i.each do |x|
puts(x)
end
puts("]")
end
For a solution, can do something like so:
对于解决方案,可以这样做:
def self.capitalizeB(string)
return string.split(" ").each { |i| i[0] = i[0].upcase }.join(" ")
end