在Ruby中打印非字符串的最简单方法

在Ruby中打印非字符串的最简单方法,ruby,syntax,Ruby,Syntax,我经常这样做 puts “The temperature is “ + String(temperature) + “.” 在我的调试代码中,另一个选项是使用插值 puts “The temperature is #{temperature}.” 有没有比这更麻烦的方法 编辑:这只是为了调试,如果有必要的话。对于这样的小案例,没有一个是值得的 不过,您应该更喜欢插值,因为它比串联便宜。将动态变量插入字符串的最佳方法是 #interpolation "foo #{my_var} bar" 它

我经常这样做

puts “The temperature is “ + String(temperature) + “.”
在我的调试代码中,另一个选项是使用插值

puts “The temperature is #{temperature}.”
有没有比这更麻烦的方法


编辑:这只是为了调试,如果有必要的话。

对于这样的小案例,没有一个是值得的


不过,您应该更喜欢插值,因为它比串联便宜。

将动态变量插入字符串的最佳方法是

#interpolation
"foo #{my_var} bar"
它将对表达式返回的任何对象调用to_s方法并插入该字符串。这真的和

#concatenation
"foo " + my_var.to_s + " bar"

但是,正如wfarr所说,插值速度更快。也更容易阅读。

始终可以使用printf样式格式:

"The temperature is %s" % temperature

这将允许更精细的数字格式,等等。但是老实说,你能指望比使用{}插值少多少麻烦呢

另一种方法是做这样愚蠢的事:

温度为%s.%温度。至


就我个人而言,我会使用插值

一种稍微不同的方法是在自动化测试中使用断言

puts “The temperature is #{temperature}.”
例如,使用Test::Unit:-

assert_equal 25, temperature
我发现使用自动化测试可以大大减少我必须编写的调试代码量。

使用

在调试时,我通常只通过复制行,并使用插入冒号,将变量转换为符号来标记这些语句

p :attributes #=> :attributes
p attributes  #=> { :mood => "happy", 5 => [] }


请注意,Kernelp在其参数上调用inspect,而不是调用s,但这通常会提供更有用的调试信息。

我强烈建议使用irbtools gem,它包括awesome\u print或只是awesome\u print。 我个人觉得在dev中使用它更快,也不那么麻烦,然后使用插值字符串,有时候这是唯一的方法

您可以在任何对象上执行此操作,它将为您提供一个格式良好的otput,可以是数组、字符串或哈希,甚至是您可能拥有的任何其他复杂对象,比如打印为树结构的三维数组。为了让它在rails环境中可用,只需将其包含在开发组的GEM文件中,或将其添加到.irbrc中,即可将其始终保存在irb控制台中。那就做吧

require "awesome_print"
ap  MyGreatObject
下面是我的一个项目的输出示例

ap Address
class Address < ActiveRecord::Base {
                  :id => :integer,
      :address_line_1 => :string,
      :address_line_2 => :string,
      :address_line_3 => :string,
                :city => :string,
               :state => :string,
                 :zip => :string,
             :country => :string,
                :attn => :string,
         :category_id => :integer,
      :addressable_id => :integer,
    :addressable_type => :string,
          :created_at => :datetime,
          :updated_at => :datetime
}


 ap Address.first
  Address Load (1.0ms)  SELECT `addresses`.* FROM `addresses` LIMIT 1
#<Address:0x7bc5a00> {
                  :id => 1,
      :address_line_1 => "1 Sample Drive",
      :address_line_2 => nil,
      :address_line_3 => nil,
                :city => "Chicago",
               :state => "IL",
                 :zip => "60625",
             :country => "USA",
                :attn => nil,
         :category_id => 1,
      :addressable_id => 1,
    :addressable_type => "Warehouse",
          :created_at => Tue, 10 Jan 2012 14:42:20 CST -06:00,
          :updated_at => Tue, 17 Jan 2012 15:03:20 CST -06:00
}

看来你倒过来了。插值是第一个例子。连接是你给出的第二个例子。也许它被误解了。我用标签说明:我忘记了baout的.to_方法。不错的选择。。。取决于你的打字习惯。我不是在实际代码中使用这些东西,只是为了弄清楚发生了什么。to_通常是将对象转换为字符串的方法。它是在所有ruby对象继承的对象类上定义的,因此它总是可用的。如果你编写自己的类,它鼓励你用一个合适的实现来覆盖它。老实说,我用西班牙语键盘打字,但有一半的时间我会切换到英语格式,所以找到数字键和括号并不像Java中的thinger+thinger那样容易。但是是的,插值语法是可行的。非常有趣,我将检查这种可能性。这样的答案让我愚蠢的问题变得值得。那么,我如何才能让这些代码从Ruby程序中运行呢?TestCase.assert_not_nilblah我知道我需要一些requires或其他东西。require'test/unit'class MyTestap Address class Address < ActiveRecord::Base { :id => :integer, :address_line_1 => :string, :address_line_2 => :string, :address_line_3 => :string, :city => :string, :state => :string, :zip => :string, :country => :string, :attn => :string, :category_id => :integer, :addressable_id => :integer, :addressable_type => :string, :created_at => :datetime, :updated_at => :datetime } ap Address.first Address Load (1.0ms) SELECT `addresses`.* FROM `addresses` LIMIT 1 #<Address:0x7bc5a00> { :id => 1, :address_line_1 => "1 Sample Drive", :address_line_2 => nil, :address_line_3 => nil, :city => "Chicago", :state => "IL", :zip => "60625", :country => "USA", :attn => nil, :category_id => 1, :addressable_id => 1, :addressable_type => "Warehouse", :created_at => Tue, 10 Jan 2012 14:42:20 CST -06:00, :updated_at => Tue, 17 Jan 2012 15:03:20 CST -06:00 }