- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在学习 Ruby,我看到了一些让我有点困惑的方法,特别是 to_s
与 to_str
(同样,to_i
/to_int
, to_a
/to_ary
, & to_h
/to_hash
).我读到的内容解释说,较短的形式(例如 to_s
)用于显式转换,而较长的形式用于隐式转换。
我真的不明白 to_str
实际是如何使用的。 to_str
会定义字符串以外的东西吗?你能给出这个方法的实际应用吗?
最佳答案
首先请注意,所有这些都适用于每对“短”(例如 to_s
/to_i
/to_a
/to_h
) 与“long”(例如 to_str
/to_int
/to_ary
/to_hash
)强制方法在 Ruby 中(对于它们各自的类型),因为它们都具有相同的语义。
它们有不同的含义。你不应该实现 to_str
除非你的对象表现像一个字符串,而不是仅仅被一个字符串表示。实现 to_str
的唯一核心类是 String 本身。
来自 Programming Ruby(引自 this blog post,值得一读):
[
to_i
andto_s
] are not particularly strict: if an object has some kind of decent representation as a string, for example, it will probably have ato_s
method… [to_int
andto_str
] are strict conversion functions: you implement them only if [your] object can naturally be used every place a string or an integer could be used.
Older Ruby documentation from the Pickaxe有这样的话:
Unlike
to_s
, which is supported by almost all classes,to_str
is normally implemented only by those classes that act like strings.
例如,除了Integer , 两者 Float & Numeric实现 to_int
(to_i
相当于 to_str
),因为它们都可以很容易地替换为 Integer(它们实际上都是数字)。除非你的类与 String 有类似的紧密关系,否则你不应该实现 to_str
。
关于ruby - Ruby 中的 to_s 与 to_str(以及 to_i/to_a/to_h 与 to_int/to_ary/to_hash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182052/
我觉得如果 "".to_i 和 "".to_f 为 nil 或引发异常会很自然,而且确实如此定义 nil.to_i 或 nil.to_f 对我来说意义不大。 nil.to_i #=> 0, "".to
我正在尝试为我的 Rack 应用程序创建一个子域路由器,但我遇到了一个问题。这是我的 config.ru 代码: require './controllers/subdomain' require '
我需要在 Javascript 中重现以下 Ruby 代码,但我找不到 to_i 的等效代码。 beginning_of_last_full_hour = (Time.now.to_s.split(/
对于一项作业,我需要编写一个名为 get_integer_from_string 的方法,它将输入字符串转换为整数。我已经完成了,但有一个要求: 请不要使用隐式或自动类型转换来解决这个问题问题,即 C
在类上定义 to_s 的效果与我预期的一样: class Foo def to_s "Joe" end end f = Foo.new "hello #{f}" # => "hello
我第一次尝试设计。我创建了一个基本的登录/注销系统,并试图从控制台创建一些对象并注意到一些奇怪的事情: rails c --sandbox Loading development environmen
我的 Controller 中有以下代码: @raw_data = ["124324/12", "345346/15"] @data_set = [] @raw_data.each do |data|
to_i String#method 的基本参数实际上是做什么的? 一些例子: "2".to_i(2) == 0 "2".to_i(36) == 2 "2".to_i(4) == 2 "ff".to_
Ruby 允许使用 Integer.to_s(base) 和 String.to_i(base) 在基数之间进行转换。 我希望使用 Integer.to_i(base) 将二进制整数转换为十进制整数,
我正在运行这条线: 并且输出是一个 fixnum 而不是一个 int。这是为什么? 来自 Controller : @date = Lesson.find(:all, :order => 'crea
我需要转换一些字符串,并取出前两个整数,例如: unkowntext60moreunknowntext25something 收件人: @width = 60 @height = 25 如果我执行 s
我从文件中读取了一个数字 "98",并尝试使用 input.to_i(2) 将其转换为二进制,但我得到了 0 这样。 input=File.read("input.dat") puts "Input
我对这行 ruby 代码感到困惑。为什么赋值给 x, y 而不是单个变量? x,y = gets.split.map &:to_i 最佳答案 这会将数组的第一个条目分配给 x,将第二个条目分配给
知道如何获得与在 PHP 中使用 Ruby 相同的结果吗? Unix 纪元时间。 timeint = Time.now.to_i puts timeint 结果: 1443253948 我尝试了 ht
我正在执行 Chris Pine 的“学习编程”中的任务。该程序询问一个人最喜欢的号码,加上 +1,然后显示结果。我写了这个: puts 'Hello, what\' your favorite nu
我得出的结论是 ActiveRecord 会根据其参数自动调用 to_i,但如果有人可以确认,尤其是通过对某些文档的引用,我将不胜感激。 下面的代码示例最好地说明了我得出结论的方式: dest_tas
我正在尝试将参数中传递的邮政编码保存为 "07306" 和 "03452",但是 to_i 似乎在验证之前将这些字符串值转换为 7306 和 3452,因此验证一直失败。 如何防止 Ruby 删除前导
我看到人们使用以下代码: gets.chomp.to_i 或 gets.chomp.to_f 我不明白为什么,当这些行的结果总是与 gets 之后没有 chomp 时的结果相同。 gets.chomp
给定一个像这样的字符串对象: twohundred = "200" 做和做有什么区别: Integer(twohundred) #=> 200 和: twohundred.to_i #=>
Time.now.to_i 有不同的输出: 本地:1416844833 生产(heroku):1416848333 我认为它忽略了时区,我也尝试了 Time.now.utc.to_i 并且正如预期的那
我是一名优秀的程序员,十分优秀!