在Ruby中,是否有相當於您可以在Python類上定義的__str__()
方法?Ruby - 相當於Python __str __()方法?
4
A
回答
14
4
關於核心類,它通常是「檢查」。
如:
irb(main):001:0> puts "array is: #{[1,2,3].inspect}"
array is: [1, 2, 3]
=> nil
irb(main):002:0> puts "array is: #{[1,2,3]}"
array is: 123
=> nil
irb(main):003:0>
6
FWIW,檢查可能更喜歡__repr__()
比__str__()
從圖書館參考...
再版(個體經營)
Called by the repr() built-in function and by string conversions (reverse quotes) to compute the ``official'' string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form "<...some useful description...>" should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an ``informal'' string representation of instances of that class is required.
相關問題
- 1. 相當於Ruby Array.each方法的Python
- 2. jQuery相當於Ruby的.send()方法
- 3. Ruby相當於php_sapi_name
- 4. Django的prefetch_related上__str __()方法
- 5. python和ruby相當於perls Template :: Declare?
- 6. Ruby在Python中相當於寶石
- 7. Ruby - 相當於foreach
- 8. python或js相當於ruby的ServiceManager
- 9. Ruby on Rails的相當於在python
- 10. Django中的__str __()方法無法調用
- 11. Javascript相當於Ruby的'send`
- 12. JavaScript相當於Ruby Time.parse
- 13. Ruby中相當於Thread.dumpStack
- 14. Scala相當於Ruby的map.each?
- 15. ,相當於date(String)方法?
- 16. 相當於Java Convert.FromBase64String方法
- 17. Zend_Form相當於ruby/rails
- 18. PHP相當於Ruby的@instance_variable?
- 19. python相當於用於Sinatra風格的URL路由的Ruby塊
- 20. Java MD5.stringify()Ruby相當於
- 21. 什麼是相當於「方法參考」 Ruby中
- 22. 相當於Ruby的在Javascript
- 23. FuckItJS相當於對Ruby
- 24. phpBB相當於Ruby on Rails
- 25. d3相當於Ruby on Rails?
- 26. Ruby on Rails相當於actionscript
- 27. ruby/rails相當於javascript decodeURIComponent?
- 28. Ruby相當於PHP的$ this
- 29. Python左()相當於?
- 30. JOptionPane相當於python?
`to_s `是`__str__`的正確等價物 – rfunduk 2008-09-30 14:15:21