在 codewars 上看到这样一道题目:
You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item. Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples: likes [] // must be "no one likes this" likes ["Peter"] // must be "Peter likes this" likes ["Jacob", "Alex"] // must be "Jacob and Alex like this" likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this" likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
题目本身没什么难度,但是看到有一个实现是这样的:
def likes(names)
case names.length
when 0
"no one likes this"
when 1
"%s likes this" % names
when 2
"%s and %s like this" % names
when 3
"%s, %s and %s like this" % names
else
"%s, %s and %d others like this" %
[names[0], names[1], names.length - 2]
end
end
麻烦问下大神这里的%s和%是怎么用的,从来没有接触过这样的写法。。。