Ruby 生成一个方法,该方法生成字符串大小写
我正在努力解决这个问题 将名为Add_index的方法添加到数组类。该方法应获取元素位置的索引值,并将其添加到同一位置的字符串中 提示:each_with_index方法应该可以帮助您解决此问题。尝试在Ruby文档中自己找到each_with_index方法,并学习如何使用它 这是我当前的代码:*我不是在创建完整的类或使用self,我只是在测试self如何处理变量a。在测试中,self通过了“这是一个测试” 最终,我需要“这是一个测试”看起来像“这是一个测试”。我想弄清楚这件事已经有一段时间了。如果有人能提供一些见解,我将不胜感激。谢谢 我的一个想法是这样做:Ruby 生成一个方法,该方法生成字符串大小写,ruby,Ruby,我正在努力解决这个问题 将名为Add_index的方法添加到数组类。该方法应获取元素位置的索引值,并将其添加到同一位置的字符串中 提示:each_with_index方法应该可以帮助您解决此问题。尝试在Ruby文档中自己找到each_with_index方法,并学习如何使用它 这是我当前的代码:*我不是在创建完整的类或使用self,我只是在测试self如何处理变量a。在测试中,self通过了“这是一个测试” 最终,我需要“这是一个测试”看起来像“这是一个测试”。我想弄清楚这件事已经有一段时间了。
a.split.each do |num| num.to_s.downcase
2.0.0p247 :010?> end
#I know this isn't right but from the syntax I know I think doing something like this is a step in the right direction.
“这是一个测试”是通过测试运行的:
describe "String" do
describe "camel_case" do
it "leaves first word lowercase" do
"test".camel_case.should eq("test")
end
it "should lowercase first letter if it isn't" do
"Test".camel_case.should eq("test")
end
it "should combine words using camel case" do
"This is a test".camel_case.should eq("thisIsATest")
end
end
end
我排除了我的方法的类字符串和def camel\u案例。我只是想测试我的方法的代码块。这就是我解决camelcase问题的方法
a = "This is a test"
a.split.map(&:capitalize}.join
"ThisIsATest"
使现代化
或
这就是我解决这个问题的方法
a = "This is a test"
a.split.map(&:capitalize}.join
"ThisIsATest"
使现代化
或
a.split.map(&:capitalize.join
@编辑:使用gsub
a.gsub(/\w+/){w | w.strip.capitalize}.gsub(/^\w/){w | w.downcase}
a.split.map(&:capitalize).join
@编辑:使用gsub
a.gsub(/\w+/){w | w.strip.capitalize}.gsub(/^\w/){w | w.downcase}
这里有一个方法
s_original = "This is a test"
s_converted = ""
s_original.downcase.split(" ").each_with_index {|word, i| s_converted = "#{s_converted}#{if i > 0 then word.capitalize! else word end}"}
puts s_original #=> "This is a test"
puts s_converted #=> "thisIsATest"
这里有一个方法
s_original = "This is a test"
s_converted = ""
s_original.downcase.split(" ").each_with_index {|word, i| s_converted = "#{s_converted}#{if i > 0 then word.capitalize! else word end}"}
puts s_original #=> "This is a test"
puts s_converted #=> "thisIsATest"
您需要
每个带有索引的\u
来区分第一个单词和其他单词。比如:
"This is a test".split.each_with_index.map { |word, index|
if index == 0
# transform first word
else
# transform other words
end
}.join
第一个转换可以是
word.downcase
,另一个word.capitalize
,您需要每个单词都带有索引
,以区分第一个单词和其他单词。比如:
"This is a test".split.each_with_index.map { |word, index|
if index == 0
# transform first word
else
# transform other words
end
}.join
第一个转换可能是
word.downcase
,另一个是word.capitalize
,这应该可以实现
a=“这是一个测试”。split.map!(&:大写)。join.sub!(/^\w{1}/){| first | first.downcase}
这应该可以解决问题
a=“这是一个测试”。split.map!(&:大写)。join.sub!(/^\w{1}/){first | first.downcase}
还有另一种方法:
def camel_case(str)
ary = str.split
ary[0].downcase + ary[1..-1].map(&:capitalize).join
end
用法:
参考:
- 还有另一种方法:
def camel_case(str)
ary = str.split
ary[0].downcase + ary[1..-1].map(&:capitalize).join
end
用法:
参考:
def camel_case(str)
words = str.downcase.split
words.shift + words.map(&:capitalize).join
end
puts camel_case('This is a test')
输出
thisIsATest
或
def camel_case(str)
words = str.downcase.split
words.shift + words.map(&:capitalize).join
end
puts camel_case('This is a test')
输出
thisIsATest
最简单的方法是:
"This is a test".tr(' ','_').camelize(:lower)
# "thisIsATest"
最简单的方法是:
"This is a test".tr(' ','_').camelize(:lower)
# "thisIsATest"
从OP:
最终,我需要“这是一个测试”看起来像“ThisisTest”。
-他不希望第一个字母大写。我如何挑出第一个单词,使其全部小写?我想我必须在“this”的某个地方添加一个.downcase。谢谢你的第一部分。我根本没用过地图,现在查一下,谢谢!从OP:最终,我需要“这是一个测试”看起来像“ThisisTest”。
-他不希望第一个字母大写。我如何挑出第一个单词,使其全部小写?我想我必须在“this”的某个地方添加一个.downcase。谢谢你的第一部分。我根本没用过地图,现在查一下,谢谢!几乎,我只需要第一个单词全部小写。几乎,我只需要第一个单词全部小写。