当前位置:  开发笔记 > 编程语言 > 正文

Ruby - 相当于Python __str __()方法?

如何解决《Ruby-相当于Python__str__()方法?》经验,为你挑选了2个好方法。

在Ruby中,是否有相当于__str__()您可以在Python类上定义的方法?



1> GEOCHET..:

你可以使用to_s.

http://briancarper.net/2006/09/26/ruby-to_s-vs-to_str/



2> 小智..:

FWIW,检查可能更喜欢__repr__()__str__()

来自图书馆参考...

repr(个体经营)

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.

推荐阅读
TXCWB_523
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有