在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. jQuery相当于Ruby的.send()方法
- 2. python和ruby相当于perls Template :: Declare?
- 3. Ruby在Python中相当于宝石
- 4. Django的prefetch_related上__str __()方法
- 5. python或js相当于ruby的ServiceManager
- 6. Django中的__str __()方法无法调用
- 7. 相当于Java Convert.FromBase64String方法
- 8. ,相当于date(String)方法?
- 9. PHP相当于Ruby的@instance_variable?
- 10. python相当于用于Sinatra风格的URL路由的Ruby块
- 11. 什么是相当于“方法参考” Ruby中
- 12. d3相当于Ruby on Rails?
- 13. FuckItJS相当于对Ruby
- 14. phpBB相当于Ruby on Rails
- 15. Python左()相当于?
- 16. Python相当于phpinfo()
- 17. python相当于sprintf
- 18. python - strtotime相当于?
- 19. python相当于jstack?
- 20. 相当于用于Ruby,Perl的Perl Modulino?
- 21. MATLAB ksdensity相当于在Python
- 22. pybind11相当于boost :: python :: extract?
- 23. 相当于C++的Python strtod
- 24. python相当于java Matcher.quoteReplacement()
- 25. Elixir的Ruby的__method__相当于什么?
- 26. 的Python相当于猛砸$()
- 27. elisp相当于python shlex.split?
- 28. 从Ruby到Python - 有没有相当于“try”的东西?
- 29. Java相当于Python字典
- 30. Haskell相当于python -m http.server?
`to_s `是`__str__`的正确等价物 – rfunduk 2008-09-30 14:15:21