inspect method of ruby_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > inspect method of ruby

inspect method of ruby

 2011/9/5 8:11:16  peryt  http://peryt.iteye.com  我要评论(0)
  • 摘要:inspectisamethodthatwillreturnastringwithliteralrepresentationoftheobjectitiscalledon.puts(1..5).to_a.inspect=>[1,2,3,4,5]puts:name.inspect=>:namebytheway,usinginspecttoprintanobjectissocommonthatthereisashortcutforit,thepfunction:p
  • 标签:Ruby

inspect is a method that will return a string with literal representation of the object it is called on.

?

puts (1..5).to_a.inspect

=> [1, 2, 3, 4, 5]

?

puts :name.inspect

=> :name

?

by the way, using inspect to print an object is so common that there is a shortcut for it, the p function:

?

p :name ? ?#this will first exec :name.inspect, then puts the returning string.

?

this is equivalent with:

?

puts :name.inspect?

发表评论
用户名: 匿名