这几乎肯定是重复的,但我找不到原文 - 我不知道要使用的搜索条件。这就是我使用Stackoverflow而不是Google的原因:)
无论如何,这是我的代码:
def titleize(say)
index = 0
words = say.split
words.each do |word|
unless word == "and" || "or" || "over" || "the" || "for"
word.capitalize!
end
if index == 0
word.capitalize!
end
index += 1
end
say = words.join(" ")
end
因为索引是在循环之前声明的,所以if index == 0
无效。
我如何让Ruby知道并使用我的对象索引?还有:这叫什么?
答案 0 :(得分:8)
使用index == 0
完全没问题,因为索引可以在循环中访问。你真正的问题可能就在这一行:
word == "and" || "or" || "over" || "the" || "for"
这总是true
- 就像!你的意思是:
["and", "or", "over", "the", "for"].include? word
除了表示有一个名为each_with_index
的方法,您可以这样使用:
words.each_with_index do |word, index|
答案 1 :(得分:2)
我认为你想使用with_index
。你的单词比较也被破坏了。
def titleize(say)
words = say.split
l = ["and", "or", "over", "the", "for"]
words.each.with_index do |word, index|
word.capitalize! if index == 0 || !(l.include? word)
end
say = words.join(" ")
end
puts(titleize("hello there for you"))
puts(titleize("hi"))
puts(titleize("for"))
答案 2 :(得分:1)
这不是布尔人的工作方式。评估方法是:
x == 'a' || 'b'
变为:
(x == 'a') || 'b'
相当于:
'b'
你想要的,翻译成更惯用的Ruby,是:
def titleize(say)
say.split.each_with_index do |word, index|
if (index == 0)
word.capitalize!
else
case (word)
when "a", "and", "or", "over", "the", "for"
# Leave lower-case
else
word.capitalize!
end
end
end.join(' ')
end
titleize('the time this is a test for the things!')
# => "The Time This Is a Test for the Things!"
答案 3 :(得分:0)
我会这样做,它更灵活,更红宝石般的
def titleize(sentence,exclusions=[])
sentence.split.map.with_index do |word,index|
(index == 0 || !exclusions.include?(word)) ? word.capitalize : word
end.join(' ')
end
对于这种情况,我使用了'大写'如果任何单词已经大写,则没有爆炸。
"Hello".capitalize! #=> nil
"Hello".capitalize #=> "Hello"
它还允许您重复使用相同的排除列表或根据需要更改它们
呼叫
exclude = ["and", "or", "over", "the", "for"]
titleize("hello there you are over there", exclude)
#=> "Hello There You Are over There"
答案 4 :(得分:0)
您的代码返回say
的修改,但确实会更改变量的内容。您似乎想要修改参数,但我不确定。我将首先建议一种方法来返回say
的修改值(但不改变say
的值,然后将显示如何更改代码以修改参数。
请注意,我没有使用索引,并使用case
语句来确定第一个之后的单词是否应该大写。
<强>代码强>
def titleize(say)
words = say.split
return "" if words.empty?
words.first.capitalize!
return words.first if words.size == 1
words[1..-1].each do |word|
case word
when "and", "or", "over", "the", "for"
else
word.capitalize!
end
end
words.join(' ')
end
<强>实施例强>
say = "and now is the time for all Rubyists to hunker down and code"
titleize(say)
#=> "And Now Is the Time for All Rubyists To Hunker Down and Code"
say
#=> "and now is the time for all Rubyists to hunker down and code"
say = " "
titleize(say)
#=> ""
say = " and "
titleize(say)
#=> "And"
修改参数
如果您想修改参数say
,请使用String#replace:
def titleize_and_modify_arg(say)
words = say.split
str =
case words.size
when 0
""
when 1
words.first.capitalize
else
words.first.capitalize!
words[1..-1].each do |word|
case word
when "and", "or", "over", "the", "for"
else
word.capitalize!
end
end
words.join(' ')
end
say.replace(str)
end
say = "and now is the time for all Rubyists to hunker down and code"
titleize_and_modify_arg(say)
#=> "And Now Is the Time for All Rubyists To Hunker Down and Code"
say
#=> "And Now Is the Time for All Rubyists To Hunker Down and Code"
say = " and "
titleize_and_modify_arg(say)
#=> nil
say
#=> " and "
请注意,在第二个示例中,titleize_and_modify_arg
正确修改了say
,但返回nil
。当然,如果需要,可以轻松更改方法以返回say
的值,并更改它。
另请注意,在case
语句中,words.siz => 1
时,它是capitalize
,而不是capitalize!
,因为后者会返回nil
如果这个词已经大写了。但是,capitalize!
案件需要else
。
答案 5 :(得分:-3)
我建议使用each_index
代替each
。请参阅here。
试试这个:
def titleize (say)
words = say.split
words.each_index do |index|
word = words[i]
unless word == "and" || "or" || "over" || "the" || "for"
word.capitalize!
end
if index == 0
word.capitalize!
end
end
say = words.join(" ")
end