Project1

标题: %s在一段代码中的含义 [打印本页]

作者: fox1313304    时间: 2015-1-28 17:33
标题: %s在一段代码中的含义
  1. def debug(arr)
  2.     puts '一一一一一一一一一'
  3.     puts arr
  4. end

  5. h = [1,2,3,4,5]
  6. h1 = h
  7. h1.each{|v|puts sprintf('values is:%s',v)}

  8. h2 = h.collect{|x| [x,x*2]}
  9. debug h2

  10. h3 = h.map{|x| x*3 }
  11. debug h3

  12. h4 = h.inject{|sum,item| sum+item}
  13. debug h4  
复制代码
百度了一下%s
  1. %s
  2. 用于表示symbol, 但是不会对其中表达式等内容进行转化
  3. 代码如下:

  4. >> %s(foo)
  5. => :foo
  6. >> %s(foo bar)
  7. => :"foo bar"
  8. >> %s(#{foo} bar)
  9. => :"\#{foo} bar"
复制代码
所以我想问在第一段代码里%s是什么意思 为什么输出的是序号呢
作者: fox1313304    时间: 2015-1-28 17:34
是不是也有‘占位符’的感觉呢
作者: 喵呜喵5    时间: 2015-1-28 18:08
请参考F1附录部分sprintf格式字符串的内容




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1