ruby 重复方法给“字符串”一个“x”次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14795428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repeat Method to Give "string" an "x" amounts of times
提问by Justin Phillip
I'm trying to write a method that will take two arguments, one for the string, and the other the number of times it will be repeated. here is the code of i have:
我正在尝试编写一个方法,该方法将采用两个参数,一个用于字符串,另一个用于重复的次数。这是我的代码:
def repeat(text,c=2)
c.times do print text end
end
repeat ("hi")
problem here is, I want to have the result to be "hi hi" i tried "puts" but that starts a new line... [ print text " + " text ] doesn't work as well...
这里的问题是,我想让结果是“嗨嗨”,我试过“puts”,但它开始了一个新行......[打印文本“+”文本]也不起作用......
thanks for the help!
谢谢您的帮助!
回答by dbenhur
Your question is unclear. If all you want is to print the text repeated n times, use String#*
你的问题不清楚。如果您只想打印重复 n 次的文本,请使用String#*
def repeat(text, n=2)
print text * n
end
Your example result says you want "hi hi"implying you would like spaces between each repetition. The most concise way to accomplish that is to use Array#*
您的示例结果表示您想"hi hi"暗示您希望每次重复之间有空格。最简洁的方法是使用Array#*
def repeat(text, n=2)
print [text] * n * ' '
end
回答by Alex D
Or you could do something like:
或者你可以这样做:
def repeat(text, c=2)
print c.times.collect { text }.join(' ')
end
回答by Catnapper
Enumerator#cyclereturns an enumerator:
Enumerator#cycle返回一个枚举器:
puts ['hi'].cycle(3).to_a.join(' ')
# => hi hi hi
Breaking down the code:
分解代码:
['hi']creates an array containing a string
['hi']创建一个包含字符串的数组
cycle(3)creates an enumerator from the array that repeats the elements 3 times
cycle(3)从数组中创建一个重复元素 3 次的枚举器
.to_acreates an array from the enumerator so that the joinmethod of Arraycan create the final output string.
.to_a从枚举器创建一个数组,以便 的join方法Array可以创建最终输出字符串。
回答by grant zukowski
I am new to ruby, but I thought this solution worked well for me and I came up with it myself.
我是 ruby 的新手,但我认为这个解决方案对我很有效,我自己想出了它。
def repeat(word, i=2)
word + (" #{word}" * (i-1))
end
回答by AlphaCarinae
Simply multiply the string by the number, Ruby is smart enough to know what you mean ;)
只需将字符串乘以数字,Ruby 就足够聪明,知道您的意思;)
pry(main)> "abcabcabc" * 3
=> "abcabcabcabcabcabcabcabcabc"
回答by Peter Kay
def repeat(text, c=2)
print ([text]*c).join(' ')
end
Perhaps easier to read. Unless, is there any reason to use the .collect method instead?
也许更容易阅读。除非,是否有任何理由改用 .collect 方法?
回答by markeissler
I don't see the point in creating an array (with or without collect()) and then calling join(). This works too:
我没有看到创建数组(使用或不使用 collect())然后调用 join() 的意义。这也有效:
def repeat(text, c=2)
c.times { |i| print text; print ' ' unless i+1 == c }
end
Although, it is a little more verbose (which is arguably un-ruby like) it does less work (which maybe makes more sense).
虽然,它有点冗长(可以说是非红宝石般的),但它的工作量更少(这可能更有意义)。
回答by Mark Swardstrom
def repeat(text, c=2)
print Array.new(c, text).join(' ')
end
回答by Rahul Tapali
You can try this:
你可以试试这个:
def repeat(text, c=2)
print ((text + ' ')*c).strip
end

